SPB Git

Repository Public

HTMLTests.swift 37.0 KB 527 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocFormatsTests/HTMLTests.swift3// Description: HTML import (tolerant tree building, formatting, lists, tables with spans, images, CSS subset, active content4//              removal) and export (semantic markup, style sheet, footnotes, MathML hook), asserted on the model and the markup.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Testing9import Foundation10import DocModel11import DocContainer12@testable import DocFormats1314@Suite struct HTMLImportTests {15    private func blocks(_ html: String) -> [String] { Describe.blocks(HTMLImporter.importDocument(html).document) }1617    @Test func tokenizerAndEntities() {18        var tokenizer = HTMLTokenizer("<!DOCTYPE html><P Class=\"a b\" data-x='1 > 0' hidden id=q>A &amp; B&nbsp;&#x1F600;&bogus;</p ><br/><!-- c --><script>if (a < b) { '</p>' }</script><x")19        var seen: [String] = []20        while let token = tokenizer.next() {21            switch token {22            case .start(let name, let attributes, let selfClosing): seen.append("<\(name) \(attributes.sorted { $0.key < $1.key }.map { "\($0.key)=\($0.value)" }.joined(separator: ","))\(selfClosing ? " /" : "")>")23            case .end(let name): seen.append("</\(name)>")24            case .text(let text): seen.append("T:\(text)")25            case .rawText(let element): seen.append("RAW:\(element)")26            case .comment: seen.append("COMMENT")27            case .doctype: seen.append("DOCTYPE")28            }29        }30        #expect(seen == ["DOCTYPE", "<p class=a b,data-x=1 > 0,hidden=,id=q>", "T:A & B\u{A0}😀&bogus;", "</p>", "<br  />", "COMMENT", "<script >", "RAW:script", "</script>", "<x >"])31        #expect(HTMLEntities.named.count == 253)32        #expect(HTMLEntities.decodeAll("&lt;&gt;&quot;&apos;&euro;&Omega;&omega;&yuml;&nbsp;&#128;&#x80;&#65&#xD800;&#9999999999;") == "<>\"'€Ωωÿ\u{A0}€€A\u{FFFD}\u{FFFD}")33        #expect(HTMLEntities.decodeAll("AT&T &amp &;") == "AT&T &amp &;")34    }3536    @Test func blocksAndInlineFormatting() throws {37        let result = HTMLImporter.importDocument("""38        <!DOCTYPE html>39        <html lang="pt-BR"><head><meta charset="utf-8"><title> My   Title </title>40        <meta name="author" content="Ada"><meta name="keywords" content="a, b ,c"><meta name="description" content="About"></head>41        <body>42        <h1>Main <em>heading</em></h1>43        <p>Plain <b>bold</b> <strong>strong</strong> <i>i</i><em>em</em> <u>under</u> <s>strike</s> <del>del</del> H<sub>2</sub>O x<sup>2</sup>44        <code>code</code> <a href="https://example.org/?a=1&amp;b=2">link</a> <mark>marked</mark> <q>quoted</q> first<br>second</p>45        <h2>Two</h2><h3>Three</h3><h4>Four</h4><h5>Five</h5><h6>Six</h6>46        <blockquote><p>Quoted paragraph.</p><p>Second.</p></blockquote>47        <pre><code>line 148          line 2 &lt;tag&gt;49        </code></pre>50        <hr>51        <div>Loose text in a div<p>then a paragraph</p>trailing text</div>52        <center>centered</center>53        <dl><dt>Term</dt><dd>Definition</dd></dl>54        </body></html>55        """)56        #expect(Describe.blocks(result.document) == [57            "Heading1: Main {i:heading}",58            "Normal: Plain {b:bold} {b:strong} {i:iem} {u:under} {s:strike} {s:del} H{_:2}O x{^:2} {c:code} [link](https://example.org/?a=1&b=2) {h:marked} \u{201C}quoted\u{201D} first⏎second",59            "Heading2: Two", "Heading3: Three", "Heading4: Four", "Heading5: Five", "Heading6: Six",60            "Quote: Quoted paragraph.", "Quote: Second.",61            "Code: line 1⏎  line 2 <tag>",62            "RULE",63            "Normal: Loose text in a div", "Normal: then a paragraph", "Normal: trailing text",64            "Normal: centered",65            "Normal: {b:Term}", "Normal: Definition",66        ])67        let doc = result.document68        #expect(doc.meta.title == "My Title")69        #expect(doc.meta.author == "Ada")70        #expect(doc.meta.keywords == ["a", "b", "c"])71        #expect(doc.meta.subject == "About")72        #expect(doc.meta.language == "pt-BR")73        #expect(doc.body[14].paragraph?.props.alignment == .center)74        #expect(doc.body[16].paragraph?.props.indentLeft == 36)75        #expect(doc.body[1].paragraph?.runs.first { $0.props.link != nil }?.charStyle == StyleID.hyperlink)76        #expect(result.warnings.isEmpty)77    }7879    @Test func whitespaceIsCollapsedLikeABrowser() {80        #expect(blocks("<p>  a \n\t b  <b> c </b>  d  </p>\n\n  <p>\n</p><p>e</p>  loose  ") == ["Normal: a b {b:c }d", "Normal: e", "Normal: loose"])81        #expect(blocks("<p>a<span> </span>b</p>") == ["Normal: a b"])82        #expect(blocks("<p>a</p> \n <p>b</p>") == ["Normal: a", "Normal: b"])83        #expect(blocks("<p>x<br>  y<br><br>z</p>") == ["Normal: x⏎y⏎⏎z"])84        #expect(blocks("<pre>\n  keep   spaces\n\ttab\n</pre>") == ["Code:   keep   spaces⏎\ttab"])85        #expect(blocks("<p style=\"white-space: pre-wrap\">a   b</p>") == ["Normal: a   b"])86        #expect(blocks("") == ["Normal: "])87        #expect(blocks("just text, no tags") == ["Normal: just text, no tags"])88    }8990    @Test func inlineStyleSubset() throws {91        let doc = HTMLImporter.importDocument("""92        <p style="text-align: right; margin-left: 24pt; text-indent: -12pt; margin-right: 0.5in; direction: rtl; background-color: #eee">93        <span style="font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 16px; color: rgb(255, 0, 128); background-color: #ff0; font-weight: 700; font-style: italic; text-decoration: underline line-through">all</span>94        <span style="FONT-WEIGHT: normal; font-size: 150%; color: #abc">b</span><span style="font-size: 2em; color: RED; font-variant: small-caps">c</span>95        <span style="font-size: huge; color: nonsense; font-weight: 300; text-decoration: none; vertical-align: super">d</span>96        <font color="#00ff00" face="Courier New">e</font><b><span style="font-weight: normal">f</span></b></p>97        <p align="center" dir="rtl">legacy</p><div style="text-align:justify"><p>inherited</p></div>98        """).document99        let p = try #require(doc.body[0].paragraph)100        #expect(p.props == ParaProps(alignment: .right, indentLeft: 24, indentRight: 36, firstLineIndent: -12, shading: RGBA(hexString: "eeeeee"), rightToLeft: true))101        func run(_ text: String) -> CharProps? { p.runs.first { $0.text.contains(text) }?.props }102        #expect(run("all") == CharProps(fontFamily: "Helvetica Neue", fontSize: 12, bold: true, italic: true, underline: true, strikethrough: true,103                                        color: RGBA(r: 1, g: 0, b: 128.0 / 255), highlight: RGBA(hexString: "ffff00")))104        #expect(run("b") == CharProps(fontSize: 18, bold: false, color: RGBA(hexString: "aabbcc")))105        #expect(run("c") == CharProps(fontSize: 24, color: RGBA(hex: 0xFF0000), smallCaps: true))106        #expect(run("d") == CharProps(bold: false, underline: false, strikethrough: false, vertical: .superscript))107        #expect(run("e") == CharProps(fontFamily: "Courier New", color: RGBA(hex: 0x00FF00)))108        #expect(run("f") == CharProps(bold: false))109        #expect(doc.body[1].paragraph?.props == ParaProps(alignment: .center, rightToLeft: true))110        #expect(doc.body[2].paragraph?.props.alignment == .justified)111        // Colour syntaxes.112        #expect(CSSSupport.colour("#F0a") == RGBA(hexString: "FF00AA"))113        #expect(CSSSupport.colour("rgba(0, 128, 255, 0.5)") == RGBA(r: 0, g: 128.0 / 255, b: 1, a: 0.5))114        #expect(CSSSupport.colour("rgb(100% 0% 0% / 50%)") == RGBA(r: 1, g: 0, b: 0, a: 0.5))115        #expect(CSSSupport.colour("#12345678")?.a == Double(0x78) / 255)116        #expect(CSSSupport.colour("url(http://evil/x.png)") == nil && CSSSupport.colour("#12") == nil && CSSSupport.colour("expression(alert(1))") == nil)117        #expect(CSSSupport.length("12pt", base: 10) == 12 && CSSSupport.length("16px", base: 10) == 12 && CSSSupport.length("1.5em", base: 10) == 15)118        #expect(CSSSupport.length("1in", base: 10) == 72 && CSSSupport.length("large", base: 10) == nil && CSSSupport.length("1e999pt", base: 10) == nil)119    }120121    @Test func lists() throws {122        let result = HTMLImporter.importDocument("""123        <ul><li>one</li><li>two<ol start="3" type="a"><li>c</li><li>d<ul><li>deep</li></ul></li></ol>after nested</li><li><p>para item</p><p>second para</p></li></ul>124        <ol><li>first<li>second, unclosed<li>third</ol>125        <ol type="I"><li>roman</li></ol>126        <li>orphan item</li>127        <ul><li><ul><li>only nested</li></ul></li></ul>128        """)129        #expect(Describe.blocks(result.document) == [130            "ListParagraph L0(•): one",131            "ListParagraph L0(•): two",132            "ListParagraph L1(c.): c",133            "ListParagraph L1(d.): d",134            "ListParagraph L2(▪): deep",135            "ListParagraph: after nested",136            "ListParagraph L0(•): para item",137            "ListParagraph: second para",138            "ListParagraph L0(1.): first", "ListParagraph L0(2.): second, unclosed", "ListParagraph L0(3.): third",139            "ListParagraph L0(I.): roman",140            "ListParagraph L0(•): orphan item",141            "ListParagraph L0(•): ", "ListParagraph L1(◦): only nested",142        ])143        let ids = result.document.body.compactMap { $0.paragraph?.list?.listID }144        #expect(Set(ids).count == 5)145        #expect(result.document.body[5].paragraph?.props.indentLeft == 36)146        let nested = try #require(result.document.list(ids[2]))147        #expect(nested.levels[1].start == 3 && nested.levels[1].format == .lowerAlpha)148    }149150    @Test func tablesWithSpans() throws {151        let result = HTMLImporter.importDocument("""152        <table>153          <caption>Results <b>2026</b></caption>154          <thead><tr><th>Name</th><th colspan="2">Scores</th></tr></thead>155          <tbody>156            <tr><td rowspan="2" bgcolor="#ffeeaa">Ada</td><td>1</td><td style="background-color: #00ff00; text-align: right">2</td></tr>157            <tr><td colspan="2"><p>merged</p><p>two paragraphs</p></td></tr>158            <tr><td>Bob</td><td rowspan="5">tall past the end</td></tr>159          </tbody>160        </table>161        """)162        #expect(Describe.blocks(result.document) == [163            "Caption: Results {b:2026}",164            "TABLE 3 cols",165            "  H TableText: Name | <span 2>TableText: Scores",166            "  R <restart>TableText: Ada | TableText: 1 | TableText: 2",167            "  R <continue>TableText:  | <span 2>TableText: merged / TableText: two paragraphs",168            "  R TableText: Bob | TableText: tall past the end | TableText: ",169        ])170        let table = try #require(result.document.body[1].table)171        #expect(table.columnWidths == [156, 156, 156])172        #expect(table.rows.allSatisfy { $0.cells.reduce(0) { $0 + $1.colSpan } == 3 })      // every row fills the grid173        #expect(table.rows[1].cells[0].shading == RGBA(hexString: "ffeeaa"))174        #expect(table.rows[1].cells[2].shading == RGBA(hex: 0x00FF00))175        #expect(table.rows[1].cells[2].paragraphs[0].props.alignment == .right)176        #expect(table.rows[2].cells[0].shading == RGBA(hexString: "ffeeaa"))                 // the continued cell keeps the shading177        #expect(table.rows.map(\.isHeader) == [true, false, false, false])178        #expect(table.rows[3].cells[1].vMerge == .none)                                      // a span running past the last row is closed179    }180181    @Test func brokenTables() {182        // Cells without rows, rows without a table, text directly inside the table, unclosed everything.183        #expect(blocks("<table><td>a<td>b<tr><td>c</table>after") == ["TABLE 2 cols", "  R TableText: a | TableText: b", "  R TableText: c | TableText: ", "Normal: after"])184        #expect(blocks("<tr><td>no table</td></tr>") == ["Normal: no table"])185        #expect(blocks("<table></table><table><tr></tr></table>x") == ["Normal: x"])186        #expect(blocks("<table><tr><td>unclosed") == ["TABLE 1 cols", "  R TableText: unclosed"])187        #expect(blocks("<table><tr><td>a</td></tr></tbody></table></td></tr>b") == ["TABLE 1 cols", "  R TableText: a", "Normal: b"])188        // Overlapping spans are resolved without losing a cell.189        let overlapping = blocks("<table><tr><td rowspan=2>A</td><td rowspan=2>B</td></tr><tr><td colspan=3>C</td></tr><tr><td>D</td></tr></table>")190        #expect(overlapping.joined().contains("A") && overlapping.joined().contains("B") && overlapping.joined().contains("C") && overlapping.joined().contains("D"))191        // A nested table is flattened into the cell, with a warning.192        let nested = HTMLImporter.importDocument("<table><tr><td>outer<table><tr><td>inner 1</td><td>inner 2</td></tr></table>tail</td></tr></table>")193        #expect(Describe.blocks(nested.document) == ["TABLE 1 cols", "  R TableText: outer / TableText: inner 1 / TableText: inner 2 / TableText: tail"])194        #expect(nested.warnings.contains { $0.feature == "Nested tables" && $0.severity == .approximated })195        // Absurd spans are clamped.196        let wide = HTMLImporter.importDocument("<table><tr><td colspan=999999999>w</td><td>dropped?</td></tr></table>")197        #expect(wide.document.body[0].table?.columnCount == FormatLimits.maxTableColumns)198        #expect(wide.warnings.contains { $0.feature == "Large tables" })199    }200201    @Test func activeContentIsDroppedAndReported() {202        var requested: [String] = []203        let result = HTMLImporter.importDocument("""204        <html><head><script src="https://evil.example/x.js"></script><script>document.write('<p>injected</p>')</script>205        <style>p { background: url(https://evil.example/track.png) } </style><link rel="stylesheet" href="https://evil.example/s.css">206        <meta http-equiv="refresh" content="0; url=https://evil.example/"><base href="https://evil.example/"></head>207        <body onload="steal()"><p onclick="x()" onmouseover="y()">Visible <a href="JaVaScRiPt:alert(1)">js link</a> <a href=" vbscript:x">vb</a> <a href="data:text/html,<script>1</script>">data</a></p>208        <iframe src="https://evil.example/frame">frame fallback</iframe>209        <object data="movie.swf"><param name="x" value="y"><embed src="movie.swf">object fallback</object>210        <applet code="A.class">applet</applet>211        <svg onload="alert(1)"><text>svg text</text><script>alert(2)</script></svg>212        <form action="https://evil.example/post"><input type="text" value="secret"><textarea>typed</textarea><button>Send</button><select><option>opt</option></select>form text</form>213        <video src="https://evil.example/v.mp4">video fallback</video><audio>audio</audio><canvas>canvas</canvas>214        <img src="https://evil.example/pixel.gif" alt="tracker" onerror="alert(3)"><img src="x.png" alt="local">215        <noscript>noscript text</noscript><template><p>template</p></template>216        <p hidden>hidden attr</p><p style="display:none">hidden css</p><span style="display: NONE">x</span>217        <p>End</p></body></html>218        """) { path in requested.append(path); return nil }219        #expect(Describe.blocks(result.document) == ["Normal: Visible js link vb data", "Normal: form text", "Normal: trackerlocal noscript text", "Normal: End"])220        #expect(requested == ["x.png"])                    // the only source that may be resolved; nothing remote221        var links = 0222        DocumentModel.forEachParagraph(in: result.document.body) { p, _ in links += p.runs.filter { $0.props.link != nil }.count }223        #expect(links == 0)224        let byDetail = Dictionary(uniqueKeysWithValues: result.warnings.map { ($0.detail, $0) })225        #expect(byDetail["Scripts were removed and never executed."]?.severity == .dropped)226        #expect(byDetail["Scripts were removed and never executed."]?.count == 3)       // external, inline, and the one inside the SVG227        #expect(byDetail["Event handler attributes were removed."]?.count == 4)228        #expect(byDetail["A link with a script or data scheme was removed; its text was kept."]?.count == 3)229        #expect(byDetail["Frames were removed; embedded pages are never loaded."] != nil)230        #expect(byDetail["Embedded objects and plug-ins were removed."]?.count == 3)231        #expect(byDetail["Inline SVG, canvas, audio and video elements were removed."]?.count == 4)232        #expect(byDetail["Form controls were removed."]?.count == 4)233        #expect(byDetail["A page refresh / redirect instruction was removed."] != nil)234        #expect(byDetail["External style sheets are never loaded; only inline style attributes are read."] != nil)235        #expect(byDetail["Embedded style sheets are ignored; only inline style attributes are read."] != nil)236        #expect(byDetail["Remote images are never downloaded; the alternative text was kept."] != nil)237        #expect(byDetail["Elements hidden with CSS or the hidden attribute were not imported."]?.count == 3)238        #expect(result.warnings.filter { $0.feature == "Active content" }.allSatisfy { $0.severity == .dropped })239        // Nothing from a script or a style ever reaches the text.240        let all = Describe.texts(result.document).joined()241        for forbidden in ["injected", "alert", "steal", "evil", "secret", "svg text", "frame fallback", "object fallback", "template", "hidden"] { #expect(!all.contains(forbidden), "\(forbidden) leaked") }242    }243244    @Test func imagesAndSizes() throws {245        let png = Fixture.png(width: 200, height: 100)246        let uri = "data:image/png;base64," + png.base64EncodedString()247        let result = HTMLImporter.importDocument("""248        <p><img src="\(uri)" alt="natural"><img src="\(uri)" alt="attrs" width="80" height="20"><img src="\(uri)" alt="width only" width="100">249        <img src="\(uri)" alt="css" style="width: 2in; height: 36pt"><img src="pic.png" alt="loaded"><img alt="no source"><a href="https://e.org"><img src="\(uri)" alt="linked"></a></p>250        """) { $0 == "pic.png" ? Fixture.jpeg() : nil }251        let images = result.document.body[0].paragraph!.runs.compactMap { r -> (ImageObject, String?)? in if case .image(let i)? = r.inline { return (i, r.props.link) } else { return nil } }252        #expect(images.map(\.0.altText) == ["natural", "attrs", "width only", "css", "loaded", "linked"])253        #expect(images.map { [$0.0.width, $0.0.height] } == [[150, 75], [60, 15], [75, 37.5], [144, 36], [7.5, 7.5], [150, 75]])254        #expect(images[5].1 == "https://e.org")255        #expect(result.document.assets.count == 2)256        #expect(result.document.body[0].paragraph?.visibleText.contains("no source") == true)257        #expect(result.warnings.contains { $0.detail == "An image without a source was skipped." })258    }259260    @Test func footnotesFromPandocStyleMarkup() throws {261        let result = HTMLImporter.importDocument("""262        <p>Text<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> more<a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a> lost<a href="#fn9" class="footnote-ref"><sup>9</sup></a>.</p>263        <section id="footnotes" class="footnotes footnotes-end-of-document" role="doc-endnotes"><hr /><ol>264        <li id="fn1"><p>First note with <em>emphasis</em>.<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>265        <li id="fn2"><p>Second.</p><ul><li>with a list</li></ul><p>End.<a href="#fnref2" class="footnote-back">↩︎</a></p></li>266        <li id="fn3"><p>Orphan note.</p></li>267        </ol></section>268        """)269        let doc = result.document270        #expect(Describe.blocks(doc) == [271            "Normal: Text<footnote FootnoteText: First note with {i:emphasis}.> more<footnote FootnoteText: Second. / FootnoteText L0: with a list / FootnoteText: End.> lost{^:9}.",272            "FootnoteText: Orphan note.",273        ])274        #expect(doc.notes.count == 2)275        #expect(result.warnings.map(\.detail).sorted() == ["Footnote references without a matching note are kept as superscript text.", "Notes that no reference points to are appended as ordinary paragraphs."])276    }277278    @Test func mathFromSpansAndMathML() {279        let result = HTMLImporter.importDocument("""280        <p>Inline <span class="math inline">\\(a^2\\)</span>, data <span class="math inline" data-latex="b_1"><math><mi>b</mi></math></span>,281        MathML <math display="block"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">\\frac{x}{2}</annotation></semantics></math> and bare <math><mi>y</mi><mo>+</mo><mn>1</mn></math>.</p>282        <p><span class="math display">\\[E = mc^2\\]</span></p>283        """)284        #expect(Describe.blocks(result.document) == ["Normal: Inline <eq a^2>, data <eq b_1>, MathML <EQ \\frac{x}{2}> and bare y+1.", "Normal: <EQ E = mc^2>"])285        #expect(result.warnings.contains { $0.feature == "Equations" && $0.severity == .approximated })286    }287288    @Test func anchorsBecomeBookmarksOnlyWhenUsed() {289        let doc = HTMLImporter.importDocument("""290        <h1 id="used">Used</h1><h2 id="unused">Unused</h2><p><a id="explicit"></a>Anchor <a name="named">named</a> <a href="#used">go</a> <span id="inline-unused">s</span></p>291        """).document292        #expect(Describe.blocks(doc) == ["Heading1: <bm used>Used", "Heading2: Unused", "Normal: <bm explicit>Anchor <bm named>named [go](#used) s"])293    }294295    @Test func unclosedAndMisnestedMarkup() {296        #expect(blocks("<p>one<p>two<div>three<h2>four<p>five") == ["Normal: one", "Normal: two", "Normal: three", "Heading2: four", "Heading2: five"])    // <p> inside an unclosed heading stays heading content, as in a browser297        #expect(blocks("<b>bold <i>both</b> rest</i> end") == ["Normal: {b:bold }{bi:both} rest end"])298        #expect(blocks("</p></div></b>text</nothing><p>a</p></p></body></html>b") == ["Normal: text", "Normal: a", "Normal: b"])299        #expect(blocks("<p>a < b and c > d <3 <-- arrow</p>") == ["Normal: a < b and c > d <3 <-- arrow"])300        #expect(blocks("<p>unterminated <b attr=\"value") == ["Normal: unterminated"])301        #expect(blocks("<p>comment <!-- never closed") == ["Normal: comment"])302        #expect(blocks("<title>Only a title") == ["Normal: "])303        #expect(HTMLImporter.importDocument("<title>Only a title</title>body").document.meta.title == "Only a title")304        #expect(blocks("<ul><li>a<p>b</ul>c") == ["ListParagraph L0(•): a", "ListParagraph: b", "Normal: c"])305        #expect(blocks("<a href='x'>link <p>block inside link</p> after</a>") == ["Normal: [link](x)", "Normal: block inside link", "Normal: [after](x)"])306        #expect(blocks("<![CDATA[raw <b> text]]><?php echo 1 ?>tail") == ["Normal: raw <b> texttail"])307    }308309    @Test func oversizedInputIsRefused() {310        let big = String(repeating: "<p>x</p>", count: FormatLimits.maxTextBytes / 8 + 1)311        #expect(throws: FormatError.self) { _ = try HTMLImporter.read(big) }312        #expect(HTMLImporter.importDocument(big).warnings.first?.feature == "Unreadable Web Page (HTML)")313    }314}315316@Suite struct HTMLExportTests {317    private func html(_ doc: DocumentModel, _ options: ExportOptions = ExportOptions()) -> String { String(decoding: HTMLExporter.export(doc, options: options).data, as: UTF8.self) }318    private func body(_ doc: DocumentModel, _ options: ExportOptions = ExportOptions()) -> String {319        let text = html(doc, options)320        let start = text.range(of: "<body>\n")!.upperBound, end = text.range(of: "\n</body>")!.lowerBound321        return String(text[start..<end])322    }323324    @Test func documentShell() {325        var doc = DocumentModel(meta: DocumentMetadata(title: "A <b> & \"title\"", author: "Ada & Bob", subject: "S", keywords: ["k1", "k<2>"], language: "ar"))326        doc.body = [.paragraph(Paragraph("x"))]327        let text = html(doc)328        #expect(text.hasPrefix("<!DOCTYPE html>\n<html lang=\"ar\">\n<head>\n<meta charset=\"utf-8\">"))329        #expect(text.contains("<title>A &lt;b&gt; &amp; \"title\"</title>"))330        #expect(text.contains("<meta name=\"author\" content=\"Ada &amp; Bob\">"))331        #expect(text.contains("<meta name=\"keywords\" content=\"k1, k&lt;2&gt;\">"))332        #expect(text.contains("<meta name=\"description\" content=\"S\">"))333        #expect(text.hasSuffix("<body>\n<p>x</p>\n</body>\n</html>\n"))334        // Standalone: no external resource of any kind.335        #expect(!text.contains("http://") && !text.contains("https://") && !text.contains("<script") && !text.contains("<link") && !text.contains("@import") && !text.contains("url("))336        // The embedded style sheet derives from the document styles.337        #expect(text.contains("body { font-family: \"New York\", ui-serif, serif; font-size: 11.5pt; color: #16181d; line-height: 1.5;"))338        #expect(text.contains("h1 { font-family: \"SF Pro Display\", system-ui, sans-serif; font-size: 20pt; font-weight: bold; color: #4f46e5;"))339        var themed = doc340        themed.styles = .standard(theme: .classic)341        #expect(html(themed).contains("body { font-family: \"Times New Roman\", ui-serif, serif; font-size: 12pt; color: #16181d;"))342    }343344    @Test func semanticMarkup() {345        var doc = DocumentModel()346        doc.lists = [.bullets(id: "b"), .numbered(id: "n")]347        doc.lists[1].levels[0].start = 4348        doc.lists[1].levels[1].format = .upperRoman349        func item(_ text: String, _ list: String, _ level: Int = 0) -> Block { .paragraph(Paragraph(style: StyleID.listParagraph, runs: [Run(text)], list: ListItem(listID: list, level: level))) }350        doc.body = [351            .paragraph(Paragraph("The <Title>", style: StyleID.title)),352            .paragraph(Paragraph("Sub", style: StyleID.subtitle)),353            .paragraph(Paragraph("Abstract text", style: StyleID.abstract)),354            .paragraph(Paragraph(style: StyleID.heading(1), runs: [Run("One & only")])),355            .paragraph(Paragraph(style: StyleID.heading(1), runs: [Run("One & only")])),356            .paragraph(Paragraph(style: StyleID.heading(3), runs: [Run(inline: .bookmark("custom-id")), Run("Three")])),357            .paragraph(Paragraph("Nine", style: StyleID.heading(9))),358            .paragraph(Paragraph(props: ParaProps(alignment: .center, indentLeft: 10.5, spaceBefore: 6), runs: [359                Run("a "), Run("b", props: CharProps(bold: true)), Run("bi", props: CharProps(bold: true, italic: true)), Run("i", props: CharProps(italic: true)),360                Run(" c\u{2028}d\te"), Run("code<>", charStyle: StyleID.inlineCode),361                Run("sized", props: CharProps(fontFamily: "Menlo", fontSize: 9, color: RGBA(hex: 0x112233), highlight: RGBA(r: 1, g: 1, b: 0, a: 0.5))),362            ])),363            item("b1", "b"), item("b2", "b", 1), item("b3", "b", 1),364            .paragraph(Paragraph(style: StyleID.listParagraph, props: ParaProps(indentLeft: 54), runs: [Run("continued")])),365            item("b4", "b"), item("four", "n"), item("sub", "n", 1), item("five", "n"),366            .paragraph(Paragraph("q1", style: StyleID.quote)), .paragraph(Paragraph("q2", style: StyleID.quote)),367            .paragraph(Paragraph("if a < b {", style: StyleID.code)), .paragraph(Paragraph("  x &= 1\u{2028}}", style: StyleID.code)),368            .paragraph(ModelIdiom.rule()),369            .paragraph(Paragraph()),370            .pageBreak(BreakBlock()),371            .paragraph(Paragraph("end")),372        ]373        #expect(body(doc) == """374        <h1 class="title" id="the-title">The &lt;Title&gt;</h1>375        <p class="subtitle">Sub</p>376        <p class="abstract">Abstract text</p>377        <h1 id="one-only">One &amp; only</h1>378        <h1 id="one-only-1">One &amp; only</h1>379        <h3 id="custom-id">Three</h3>380        <h6 id="nine" aria-level="9">Nine</h6>381        <p style="text-align: center; margin-left: 10.5pt; margin-top: 6pt">a <strong>b<em>bi</em></strong><em>i</em> c<br>382        d<span style="white-space: pre">&#9;</span>e<code>code&lt;&gt;</code><span style="font-family: &quot;Menlo&quot;, ui-monospace, monospace; font-size: 9pt; color: #112233; background-color: rgba(255, 255, 0, 0.5)">sized</span></p>383        <ul>384        <li>b1<ul>385        <li>b2</li>386        <li>b3387        <p>continued</p></li>388        </ul>389        </li>390        <li>b4</li>391        </ul>392        <ol start="4">393        <li>four<ol type="I">394        <li>sub</li>395        </ol>396        </li>397        <li>five</li>398        </ol>399        <blockquote>400        <p>q1</p>401        <p>q2</p>402        </blockquote>403        <pre><code>if a &lt; b {404          x &amp;= 1405        }</code></pre>406        <hr>407        <div class="page-break"></div>408        <p>end</p>409        """)410    }411412    @Test func tablesWithSpansAndHeader() {413        let doc = Fixture.rich()414        let text = body(doc)415        #expect(text.contains("""416        <table>417        <colgroup><col style="width: 30.3%"><col style="width: 45.45%"><col style="width: 24.24%"></colgroup>418        <thead>419        <tr><th colspan="2" style="background-color: #ddeeff; vertical-align: middle">Header A+B</th><th rowspan="2" style="background-color: #4f46e5">Tall</th></tr>420        </thead>421        <tbody>422        <tr><td>a1</td><td>b1</td></tr>423        <tr><td>a2</td><td style="vertical-align: bottom"><p>line one</p>424        <p>line two</p></td><td>42</td></tr>425        </tbody>426        </table>427        """))428    }429430    @Test func richDocumentRoundTrip() throws {431        let source = Fixture.rich()432        let result = HTMLExporter.export(source)433        let text = String(decoding: result.data, as: UTF8.self)434        #expect(text.contains("<html lang=\"fr-CA\">"))435        #expect(text.contains("alt=\"A blue box\" width=\"160\" height=\"120\" style=\"float: right;"))436        #expect(text.contains("<img src=\"data:image/jpeg;base64,/9j/"))437        #expect(text.contains("<span class=\"math inline\" data-latex=\"e^{i\\pi}+1=0\">e^{i\\pi}+1=0</span>"))438        #expect(text.contains("<section class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn1\"><p>A footnote with <em>emphasis</em>. <a href=\"#fn1-ref\" class=\"footnote-back\" role=\"doc-backlink\">↩</a></p></li>"))439        #expect(text.contains("<li class=\"toc-1\"><a href=\"#intro\">Introduction</a></li>\n<li class=\"toc-2\"><a href=\"#second-section\">Second section</a></li>"))440        #expect(text.contains("<p class=\"ps-Custom-Style-1\">Custom styled</p>") && text.contains(".ps-Custom-Style-1 { font-family: \"Menlo\""))441        let features = Set(result.warnings.map(\.feature))442        for expected in ["Tracked changes", "Comments", "Headers and footers", "Sections", "Columns", "Page decoration", "Shapes", "Page-dependent fields", "Fields",443                         "Cross-references", "Preserved DOCX content", "Citations", "Bibliography", "Page setup", "Page breaks", "Floating objects", "Character effects",444                         "Table formulas and borders", "Equations"] {445            #expect(features.contains(expected), "missing warning: \(expected)")446        }447        #expect(!features.contains("Fonts") && !features.contains("Colours"))          // HTML carries those448449        let back = HTMLImporter.importDocument(text)450        // Our own style sheet is not read back (only inline styles are): the importer says so, and nothing else.451        #expect(back.warnings.map(\.feature) == ["Style sheets"])452        let doc = back.document453        #expect(doc.meta.title == source.meta.title && doc.meta.author == source.meta.author && doc.meta.language == "fr-CA" && doc.meta.subject == "Testing")454        #expect(Describe.blocks(doc) == [455            "Title: Rich fixture",456            "TOC contents",457            "Heading1: <bm intro>Introduction",458            "Normal: Plain {b:bold} {i:italic} [link](https://example.org/a?b=1&c=2) {us^h:styled} inserted<img 120x90 alt=A blue box data=\(source.assets["img/1 é"]!.data.count)><img 10x10 alt=Photo data=\(source.assets["photo"]!.data.count)>Note<eq e^{i\\pi}+1=0><footnote FootnoteText: A footnote with {i:emphasis}.><endnote FootnoteText: An endnote. / FootnoteText: Second paragraph.><cite see|knuth1984|p. 4|;|-doe:2020||passim>[Figure 1](#caption-figcap)[?](#intro)2026Rich « fixture »Ada Lovelace end⏎second line\ttabbed",459            "Normal: <EQ \\int_0^1 x^2\\,dx = \\frac{1}{3}>",460            "Caption: <bm caption-figcap>Figure 1: A caption",461            "ListParagraph L0(•): First bullet", "ListParagraph L1(◦): Nested bullet", "ListParagraph L0(1.): One", "ListParagraph L1(1.): One point one",462            "Normal: Custom styled",463            "TABLE 3 cols",464            "  H <span 2>TableText: Header A+B | <restart>TableText: Tall",465            "  R TableText: a1 | TableText: b1 | <continue>TableText: ",466            "  R TableText: a2 | TableText: line one / TableText: line two | TableText: 42",467            "PAGEBREAK", "PAGEBREAK",468            "Heading2: Second section", "Quote: A quotation.", "Code: let x = 1⏎print(x)",469            "TOC figures", "TOC tables", "BIBLIOGRAPHY",470        ])471        // Direct formatting survives through inline styles.472        let mixed = try #require(doc.body[3].paragraph)473        #expect(mixed.props == ParaProps(alignment: .justified, indentLeft: 12, firstLineIndent: 18, spaceBefore: 4, lineSpacing: .exact(15)))     // tabs and the border are not read back474        let styled = try #require(mixed.runs.first { $0.text == "styled" })475        #expect(styled.props.fontFamily == "Georgia" && styled.props.fontSize == 14 && styled.props.underline == true && styled.props.strikethrough == true)476        #expect(styled.props.highlight == RGBA(hex: 0xFFFF00) && styled.props.vertical == .superscript && styled.props.smallCaps == true)477        #expect(styled.props.color == RGBA(r: 26.0 / 255, g: 51.0 / 255, b: 77.0 / 255, a: 0.5))478        // Generated blocks come back as blocks, with their settings.479        guard case .toc(let toc) = doc.body[1] else { Issue.record("no TOC"); return }480        #expect(toc.kind == .contents && toc.title == "Sommaire" && toc.maxLevel == 2)481        guard case .bibliography(let bibliography) = doc.body[19] else { Issue.record("no bibliography"); return }482        #expect(bibliography.title == "Références")483        let table = try #require(doc.body[11].table)484        #expect(table.rows[0].cells[0].shading == RGBA(hexString: "ddeeff"))485        // And a second generation is identical to the first: export(import(export(x))) == export(x) for the body.486        let second = HTMLExporter.export(doc)487        #expect(Describe.blocks(HTMLImporter.importDocument(String(decoding: second.data, as: UTF8.self)).document) == Describe.blocks(doc))488    }489490    @Test func mathMLHookAndFallback() {491        let doc = DocumentModel(body: [.paragraph(Paragraph(runs: [Run("a "), Run(inline: .equation(Equation(latex: "x<y"))), Run(inline: .equation(Equation(latex: "\\bad")))])),492                                       .paragraph(Paragraph(runs: [Run(inline: .equation(Equation(latex: "E=mc^2", display: true)))]))])493        var options = ExportOptions()494        options.mathML = { latex, display in latex == "\\bad" ? nil : "<math display=\"\(display ? "block" : "inline")\"><mtext>\(latex.count)</mtext></math>" }495        let converted = HTMLExporter.export(doc, options: options)496        let text = String(decoding: converted.data, as: UTF8.self)497        #expect(text.contains("<span class=\"math inline\" data-latex=\"x&lt;y\"><math display=\"inline\"><mtext>3</mtext></math></span>"))498        #expect(text.contains("<span class=\"math inline\" data-latex=\"\\bad\">\\bad</span>"))499        #expect(text.contains("<p class=\"equation\"><span class=\"math display\" data-latex=\"E=mc^2\"><math display=\"block\"><mtext>6</mtext></math></span></p>"))500        #expect(converted.warnings.contains { $0.feature == "Equations" && $0.detail.contains("could not be converted") })501        #expect(!converted.warnings.contains { $0.detail.contains("No MathML converter") })502        // The LaTeX source survives the round trip whatever the MathML looks like.503        #expect(Describe.blocks(HTMLImporter.importDocument(text).document) == ["Normal: a <eq x<y><eq \\bad>", "Normal: <EQ E=mc^2>"])504        let fallback = HTMLExporter.export(doc)505        #expect(String(decoding: fallback.data, as: UTF8.self).contains("<span class=\"math inline\" data-latex=\"x&lt;y\">x&lt;y</span>"))506        #expect(fallback.warnings.contains { $0.feature == "Equations" && $0.severity == .approximated && $0.detail.contains("No MathML converter") })507    }508509    @Test func citationsAndBibliographyWithFormatter() {510        var options = ExportOptions()511        options.citations = StubCitations()512        let text = String(decoding: HTMLExporter.export(Fixture.rich(), options: options).data, as: UTF8.self)513        #expect(text.contains("<span class=\"citation\" data-cite-items=\"see @knuth1984, p. 4; -@doe:2020, passim\">(KNUTH1984 p. 4; DOE:2020)</span>"))514        #expect(text.contains("<h2>Références</h2>\n<p class=\"bib-entry\">[1] <em>The TeXbook</em></p>\n<p class=\"bib-entry\">[2] <em>On Things</em></p>"))515    }516517    @Test func scriptSchemesNeverReachTheOutput() {518        let doc = DocumentModel(body: [.paragraph(Paragraph(runs: [Run("bad", props: CharProps(link: "javascript:alert(1)")), Run("</p><script>alert(2)</script>"),519                                                                   Run("attr", props: CharProps(fontFamily: "x\"; } body { background: url(http://evil) } .a { \"", link: "https://ok.example/\"><script>"))]))])520        let text = String(decoding: HTMLExporter.export(doc).data, as: UTF8.self)521        #expect(!text.contains("<script>") && !text.contains("javascript:"))522        #expect(text.contains("bad&lt;/p&gt;&lt;script&gt;alert(2)&lt;/script&gt;"))523        #expect(text.contains("href=\"https://ok.example/&quot;&gt;&lt;script&gt;\""))524        #expect(!text.contains("style=\"font-family: \"x"))525    }526}527
Files main
Menu

Theme