SPB Git

Repository Public

NextComposeRichTextTests.swift 21.3 KB 321 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/NextComposeRichTextTests.swift3// Description: Unit tests of the composer's rich text conversions: model text → HTML + plain text, HTML → model text (round trips, nested4// lists, quotes, links, inline images, emoji / CJK / right-to-left text), hostile pasted HTML reduced to the allowed subset, and the output5// passing the core sanitiser unchanged.6import Foundation7import Testing8@testable import SPMailCore910/// Builds model text from small pieces: `("text", [marks], link)` runs joined into paragraphs.11private struct Doc {12    let text = NSMutableAttributedString()13    @discardableResult func p(_ block: RichBlock = .body, _ runs: (String, [RichMark], String?)...) -> Doc {14        if text.length > 0 || started.value { text.append(NSAttributedString(string: "\n", attributes: last.value)) }15        started.value = true16        for (s, marks, link) in runs {17            var a: [NSAttributedString.Key: Any] = [RichKey.block: block.rawValue]18            for m in marks { a[m.key] = true }19            if let link { a[RichKey.link] = link }20            text.append(NSAttributedString(string: s, attributes: a))21        }22        last.value = [RichKey.block: block.rawValue]23        return self24    }25    @discardableResult func plain(_ block: RichBlock = .body, _ s: String) -> Doc { p(block, (s, [], nil)) }26    @discardableResult func image(_ image: RichImage) -> Doc {27        if text.length > 0 || started.value { text.append(NSAttributedString(string: "\n", attributes: last.value)) }28        started.value = true29        text.append(NSAttributedString(string: RichText.attachmentCharacter, attributes: [RichKey.block: RichBlock.body.rawValue, RichKey.image: image.rawValue]))30        last.value = [RichKey.block: RichBlock.body.rawValue]31        return self32    }33    final class Box<T>: @unchecked Sendable { var value: T; init(_ v: T) { value = v } }34    let started = Box(false)35    let last = Box<[NSAttributedString.Key: Any]>([:])36}3738/// Two model texts are the same when their characters and their semantic attributes are.39private func same(_ a: NSAttributedString, _ b: NSAttributedString) -> Bool {40    guard a.string == b.string else { return false }41    var equal = true42    a.enumerateAttributes(in: NSRange(location: 0, length: a.length)) { attrs, range, stop in43        for i in range.location..<NSMaxRange(range) {44            let other = b.attributes(at: i, effectiveRange: nil)45            for key in RichKey.all where "\(attrs[key] ?? "∅")" != "\(other[key] ?? "∅")" { equal = false; stop.pointee = true; return }46        }47    }48    return equal49}50private func describe(_ a: NSAttributedString) -> String {51    var out = ""52    a.enumerateAttributes(in: NSRange(location: 0, length: a.length)) { attrs, range, _ in53        let keys = RichKey.all.compactMap { k in attrs[k].map { "\(k.rawValue.dropFirst(4))=\($0)" } }.joined(separator: ",")54        out += "[\((a.string as NSString).substring(with: range).debugDescription) \(keys)]"55    }56    return out57}58private func roundTrip(_ d: Doc, sourceLocation: SourceLocation = #_sourceLocation) -> String {59    let html = RichHTML.html(from: d.text)60    let back = RichHTML.parse(html, mode: .own)61    #expect(!back.lossy, "own output reported lossy: \(html)", sourceLocation: sourceLocation)62    #expect(same(d.text, back.text), "\(html)\n  in:  \(describe(d.text))\n  out: \(describe(back.text))", sourceLocation: sourceLocation)63    #expect(RichHTML.html(from: back.text) == html, sourceLocation: sourceLocation)64    // the core sanitiser has nothing to change in it65    let cids = Set(RichText.cids(in: d.text))66    #expect(HTMLSanitizer.sanitizeForQuoting(html, availableCIDs: cids).html == html, "sanitiser changed: \(html)", sourceLocation: sourceLocation)67    return html68}6970@Suite struct RichHTMLOutputTests {71    @Test func paragraphsAndInlineMarks() {72        let d = Doc()73        d.p(.body, ("Bonjour ", [], nil), ("gras", [.bold], nil), (" ", [], nil), ("italique", [.italic], nil), (" ", [], nil), ("souligné", [.underline], nil), (" ", [], nil), ("barré", [.strike], nil), (" ", [], nil), ("code()", [.code], nil))74        d.plain(.body, "")75        d.p(.body, ("tout", [.bold, .italic, .underline, .strike], nil))76        let html = roundTrip(d)77        #expect(html == "<div>Bonjour <b>gras</b> <i>italique</i> <u>souligné</u> <s>barré</s> <code>code()</code></div><div><br></div><div><b><i><u><s>tout</s></u></i></b></div>")78        #expect(RichHTML.plainText(from: d.text) == "Bonjour gras italique souligné barré code()\n\ntout")79    }8081    @Test func headingsAndEscaping() {82        let d = Doc()83        d.plain(RichBlock(kind: .heading(1)), "Titre <1> & co")84        d.plain(RichBlock(kind: .heading(2)), "Sous-titre")85        d.plain(RichBlock(kind: .heading(3)), "Intertitre")86        d.plain(.body, "a < b > c & d \"e\" 'f'")87        let html = roundTrip(d)88        #expect(html == "<h1>Titre &lt;1&gt; &amp; co</h1><h2>Sous-titre</h2><h3>Intertitre</h3><div>a &lt; b &gt; c &amp; d \"e\" 'f'</div>")89    }9091    @Test func listsNestAndSwitchKind() {92        let d = Doc()93        d.plain(RichBlock(kind: .bullet, level: 1), "un")94        d.plain(RichBlock(kind: .bullet, level: 2), "un-a")95        d.plain(RichBlock(kind: .bullet, level: 3), "un-a-i")96        d.plain(RichBlock(kind: .bullet, level: 2), "un-b")97        d.plain(RichBlock(kind: .bullet, level: 1), "deux")98        d.plain(RichBlock(kind: .ordered, level: 1), "premier")99        d.plain(RichBlock(kind: .ordered, level: 2), "premier-a")100        d.plain(RichBlock(kind: .ordered, level: 1), "second")101        d.plain(.body, "fin")102        let html = roundTrip(d)103        #expect(html == "<ul><li>un<ul><li>un-a<ul><li>un-a-i</li></ul></li><li>un-b</li></ul></li><li>deux</li></ul><ol><li>premier<ol><li>premier-a</li></ol></li><li>second</li></ol><div>fin</div>")104        #expect(RichHTML.plainText(from: d.text) == "• un\n  • un-a\n    • un-a-i\n  • un-b\n• deux\n1. premier\n  1. premier-a\n2. second\nfin")105    }106107    @Test func mixedListKindsAtTheSameDepthAndLevelJumps() {108        let d = Doc()109        d.plain(RichBlock(kind: .bullet, level: 1), "a")110        d.plain(RichBlock(kind: .ordered, level: 2), "1")111        d.plain(RichBlock(kind: .bullet, level: 2), "x")112        #expect(roundTrip(d) == "<ul><li>a<ol><li>1</li></ol><ul><li>x</li></ul></li></ul>")113        // a level that skips one is written one level deeper than its parent — and read back that way114        let jump = Doc()115        jump.plain(RichBlock(kind: .bullet, level: 1), "a")116        jump.plain(RichBlock(kind: .bullet, level: 3), "b")117        let html = RichHTML.html(from: jump.text)118        #expect(html == "<ul><li>a<ul><li>b</li></ul></li></ul>")119        #expect(RichText.block(ofParagraph: RichText.paragraphRanges(in: RichHTML.parse(html, mode: .own).text.string as NSString)[1], in: RichHTML.parse(html, mode: .own).text) == RichBlock(kind: .bullet, level: 2))120    }121122    @Test func blockQuotesWithListsInside() {123        let d = Doc()124        d.plain(.body, "avant")125        d.plain(RichBlock(kind: .paragraph, quote: 1), "cité")126        d.plain(RichBlock(kind: .bullet, level: 1, quote: 1), "point cité")127        d.plain(RichBlock(kind: .paragraph, quote: 2), "cité deux fois")128        d.plain(.body, "après")129        let html = roundTrip(d)130        let open = "<blockquote style=\"margin:0 0 0 .8ex;border-left:2px solid #999;padding-left:1ex\">"131        #expect(html == "<div>avant</div>\(open)<div>cité</div><ul><li>point cité</li></ul>\(open)<div>cité deux fois</div></blockquote></blockquote><div>après</div>")132        #expect(RichHTML.plainText(from: d.text) == "avant\n> cité\n> • point cité\n> > cité deux fois\naprès")133    }134135    @Test func linksKeepTheirMarksAndAreSafe() {136        let d = Doc()137        d.p(.body, ("voir ", [], nil), ("le ", [], "https://example.org/a?b=1&c=\"2\""), ("site", [.bold], "https://example.org/a?b=1&c=\"2\""), (" ou ", [], nil), ("écrire", [], "mailto:a@b.co"))138        let html = roundTrip(d)139        #expect(html == "<div>voir <a href=\"https://example.org/a?b=1&amp;c=&quot;2&quot;\" rel=\"noopener noreferrer\">le <b>site</b></a> ou <a href=\"mailto:a@b.co\" rel=\"noopener noreferrer\">écrire</a></div>")140        #expect(RichHTML.plainText(from: d.text) == "voir le site (https://example.org/a?b=1&c=\"2\") ou écrire (mailto:a@b.co)")141        // a link the sanitiser would refuse is never written142        let bad = Doc()143        bad.p(.body, ("clic", [], "javascript:alert(1)"))144        #expect(RichHTML.html(from: bad.text) == "<div>clic</div>")145        // a link whose text is its address is not repeated in the plain text146        let bare = Doc()147        bare.p(.body, ("https://spboucher.ai", [], "https://spboucher.ai"), (" ", [], nil), ("a@b.co", [], "mailto:a@b.co"), (" ", [], nil), ("www.SPBoucher.ai", [], "https://spboucher.ai/"), (" ", [], nil), ("spboucher.ai/x", [], "https://spboucher.ai/y"))148        #expect(RichHTML.plainText(from: bare.text) == "https://spboucher.ai a@b.co www.SPBoucher.ai spboucher.ai/x (https://spboucher.ai/y)")149    }150151    @Test func spacesSurviveTheRoundTrip() {152        let d = Doc()153        d.plain(.body, "  deux  espaces   trois ")154        d.plain(.body, "prix\u{00A0}: 10\u{00A0}$") // real non-breaking spaces between words stay non-breaking155        d.plain(.body, "-- ")156        let html = roundTrip(d)157        #expect(html == "<div>&nbsp; deux &nbsp;espaces &nbsp; trois&nbsp;</div><div>prix&nbsp;: 10&nbsp;$</div><div>--&nbsp;</div>")158        #expect(RichHTML.plainText(from: d.text) == "  deux  espaces   trois \nprix\u{00A0}: 10\u{00A0}$\n-- ")159    }160161    @Test func emojiCJKAndRightToLeftText() {162        let d = Doc()163        d.p(.body, ("Bravo 👩🏽‍💻🇨🇦 ", [], nil), ("日本語のテキスト", [.bold], nil), (" e\u{301}te\u{301}", [.italic], nil))164        d.p(RichBlock(kind: .bullet, level: 1), ("مرحبا بالعالم", [.underline], "https://example.org/مرحبا"))165        d.plain(RichBlock(kind: .ordered, level: 1), "שלום עולם 🕊️")166        let html = roundTrip(d)167        #expect(html.contains("👩🏽‍💻🇨🇦") && html.contains("<b>日本語のテキスト</b>") && html.contains("<u>مرحبا بالعالم</u>") && html.contains("<ol><li>שלום עולם 🕊️</li></ol>"))168        #expect(RichHTML.plainText(from: d.text) == "Bravo 👩🏽‍💻🇨🇦 日本語のテキスト e\u{301}te\u{301}\n• مرحبا بالعالم (https://example.org/مرحبا)\n1. שלום עולם 🕊️")169    }170171    @Test func inlineImagesAreCidReferences() {172        let d = Doc()173        d.plain(.body, "Voici la photo :")174        d.image(RichImage(cid: "img-1@spmail", name: "photo \"été\".jpg", width: 320))175        d.image(RichImage(cid: "img-2@spmail", name: "plan.png"))176        d.plain(.body, "fin")177        let html = roundTrip(d)178        #expect(html == "<div>Voici la photo :</div><div><img src=\"cid:img-1@spmail\" alt=\"photo &quot;été&quot;.jpg\" width=\"320\"></div><div><img src=\"cid:img-2@spmail\" alt=\"plan.png\"></div><div>fin</div>")179        #expect(RichText.cids(in: d.text) == ["img-1@spmail", "img-2@spmail"])180        #expect(RichHTML.plainText(from: d.text) == "Voici la photo :\n[image: photo \"été\".jpg]\n[image: plan.png]\nfin")181        // an image whose part is gone is not kept when reading back, and that is a loss the caller hears about182        let partial = RichHTML.parse(html, mode: .own, allowedCIDs: ["img-2@spmail"])183        #expect(partial.lossy && partial.droppedImages == 1 && RichText.cids(in: partial.text) == ["img-2@spmail"])184    }185186    @Test func signatureBlockIsWrappedAndReadBack() {187        let d = Doc()188        d.plain(.body, "Bonjour")189        d.plain(.body, "")190        let sig = RichSignature.block(SignatureChoice(id: "sig-1", html: "<div><b>Simon-Pierre</b></div><div>UQO</div>", text: "Simon-Pierre\nUQO"))191        d.text.append(NSAttributedString(string: "\n", attributes: [RichKey.block: RichBlock.body.rawValue]))192        d.text.append(sig)193        let html = RichHTML.html(from: d.text)194        #expect(html == "<div>Bonjour</div><div><br></div><div class=\"spm-signature\"><div>--&nbsp;</div><div><b>Simon-Pierre</b></div><div>UQO</div></div>")195        let back = RichHTML.parse(html, mode: .own)196        #expect(!back.lossy && back.text.string == d.text.string)197        #expect(RichSignature.range(in: back.text).map { (back.text.string as NSString).substring(with: $0) } == "-- \nSimon-Pierre\nUQO")198        #expect(RichHTML.plainText(from: d.text) == "Bonjour\n\n-- \nSimon-Pierre\nUQO")199        #expect(HTMLSanitizer.sanitizeForQuoting(html).html == html)200        // the tag alone does not make a message « rich »201        #expect(!RichText.isRich(RichSignature.block(SignatureChoice(id: "s", text: "Simon"))))202    }203204    @Test func emptyAndTrailingParagraphs() {205        #expect(RichHTML.html(from: NSAttributedString(string: "")) == "<div><br></div>")206        let d = Doc()207        d.plain(.body, "a")208        d.plain(.body, "")209        d.plain(.body, "")210        #expect(roundTrip(d) == "<div>a</div><div><br></div><div><br></div>")211        #expect(RichText.paragraphRanges(in: "a\n\n") == [NSRange(location: 0, length: 1), NSRange(location: 2, length: 0), NSRange(location: 3, length: 0)])212        #expect(RichText.attributed(fromPlain: "a\r\nb\rc\u{2028}d").string == "a\nb\nc\nd")213    }214}215216@Suite struct RichHTMLInputTests {217    @Test func draftsWrittenByVersion110ReopenAsRichText() {218        // 1.1.0 wrote one <div> with <br> line breaks and b / i / s / code / a219        let old = "<div>Bonjour <b>Camille</b>,<br><br>voir <a href=\"https://example.org\">ce lien</a> <i>vite</i>.<br><s>ancien</s> <code>x = 1</code></div>"220        let r = RichHTML.parse(old, mode: .own)221        #expect(!r.lossy)222        #expect(r.text.string == "Bonjour Camille,\n\nvoir ce lien vite.\nancien x = 1")223        #expect(RichHTML.html(from: r.text) == "<div>Bonjour <b>Camille</b>,</div><div><br></div><div>voir <a href=\"https://example.org\" rel=\"noopener noreferrer\">ce lien</a> <i>vite</i>.</div><div><s>ancien</s> <code>x = 1</code></div>")224    }225226    @Test func webmailStyleMarkupIsUnderstood() {227        let html = """228        <p>Bonjour,</p>229        <p>Voici <strong>trois</strong> <em>points</em>&nbsp;:</p>230        <ul>231          <li>un</li>232          <li>deux233            <ol><li>deux-a</li></ol>234          </li>235        </ul>236        <blockquote type="cite"><p>cité</p></blockquote>237        <p><span style="font-weight: 700; font-style: italic">gras italique</span> <span style="text-decoration: underline">souligné</span></p>238        """239        let r = RichHTML.parse(html, mode: .own)240        #expect(!r.lossy)241        #expect(r.text.string == "Bonjour,\nVoici trois points\u{00A0}:\nun\ndeux\ndeux-a\ncité\ngras italique souligné")242        let blocks = RichText.paragraphRanges(in: r.text.string as NSString).map { RichText.block(ofParagraph: $0, in: r.text).rawValue }243        #expect(blocks == ["p", "p", "ul1", "ul1", "ol2", ">p", "p"])244        #expect(RichHTML.html(from: r.text).hasSuffix("<div><b><i>gras italique</i></b> <u>souligné</u></div>"))245    }246247    @Test func whatTheComposerCannotRepresentIsReportedNotFlattened() {248        for html in ["<table><tr><td>a</td></tr></table>", "<div style=\"color:red\">a</div>", "<p align=\"center\">a</p>", "<div>a<img src=\"https://example.org/x.png\"></div>",249                     "<div>a</div><hr><div>b</div>", "<style>p{color:red}</style><p>a</p>", "<div><font color=\"#f00\">a</font></div>", "<ul><li>a<br>b</li></ul>", "<h5>a</h5>", "<pre>a\n b</pre>",250                     "<div class=\"autre\">a</div>", "<ol start=\"3\"><li>a</li></ol>", "<span style=\"font-size:20px\">a</span>", "<div>a<sup>2</sup></div>"] {251            #expect(RichHTML.parse(html, mode: .own).lossy, "\(html)")252        }253        for html in ["<div>a</div>", "<p>a</p><p>b</p>", "a<br>b", "<div><b>a</b></div><!-- c -->", "<blockquote type=\"cite\" style=\"margin:0\"><div>q</div></blockquote>", "<a href=\"https://a.b\" target=\"_blank\">x</a>"] {254            #expect(!RichHTML.parse(html, mode: .own).lossy, "\(html)")255        }256    }257258    @Test func hostilePastedHTMLIsReducedToTheAllowedSubset() {259        let hostile = """260        <html><head><title>t</title><style>body{background:url(https://evil.example/x)}</style><script>alert(1)</script></head>261        <body onload="steal()"><h1 onclick="x()">Titre</h1>262        <p style="font-weight:bold;position:fixed;background:url(javascript:alert(1))">gras <a href="javascript:alert(1)">piège</a> <a href="https://ok.example/p?q=1" onclick="x()">lien</a></p>263        <img src="https://tracker.example/p.gif" width="1" height="1"><img src="data:image/png;base64,AAAA"><img src=x onerror=alert(1)>264        <iframe src="https://evil.example"></iframe><object data="x"></object><svg><script>alert(2)</script></svg>265        <form action="https://evil.example"><input name="a" value="secret"><button>ok</button></form>266        <table><tr><td>cellule un</td><td>cellule deux</td></tr></table>267        <ul><li>point <b>fort</b></li></ul><div><div><span><u>imbriqué</u></span></div></div>268        <b style="font-weight:normal"><span style="font-weight:700">Docs</span> normal</b>269        </body></html>270        """271        let r = RichHTML.parse(hostile, mode: .paste)272        let html = RichHTML.html(from: r.text)273        for forbidden in ["script", "alert", "onclick", "onload", "onerror", "javascript", "evil.example", "tracker", "iframe", "object", "svg", "form", "input", "secret", "style=\"font", "position", "data:image", "<table", "<td", "<img", "<title", "<html", "<body"] {274            #expect(!html.contains(forbidden), "\(forbidden) survived: \(html)")275        }276        #expect(r.droppedImages == 3)277        #expect(html.contains("<h1>Titre</h1>") && html.contains("<b>gras piège </b>") && html.contains("<a href=\"https://ok.example/p?q=1\" rel=\"noopener noreferrer\"><b>lien</b></a>"))278        #expect(html.contains("<div>cellule un</div><div>cellule deux</div>") && html.contains("<ul><li>point <b>fort</b></li></ul>") && html.contains("<div><u>imbriqué</u></div>") && html.contains("<div><b>Docs</b> normal</div>"))279        // only tags of the subset, and the sanitiser agrees280        let tags = Set(html.matches(of: /<\/?([a-z0-9]+)/).map { String($0.1) })281        #expect(tags.isSubset(of: ["div", "br", "b", "i", "u", "s", "code", "a", "h1", "h2", "h3", "ul", "ol", "li", "blockquote", "img"]))282        #expect(HTMLSanitizer.sanitizeForQuoting(html).html == html)283        #expect(!RichHTML.parse(html, mode: .own).lossy)284    }285286    @Test func malformedMarkupNeverTraps() {287        for html in ["", "<", "<<<>>>", "<b", "</b>", "<a href=", "<a href=\"x", "<img src=cid:>", "&#x110000;&#0;&bogus;&", "<ul><li><ul><li><li></ul>", "<div><b><i>a</div></i></b>b", "<!-- x", "<script>never closed",288                     "<blockquote><blockquote><blockquote><blockquote><blockquote><blockquote>profond", "\u{FFFC}objet\u{2028}ligne", String(repeating: "<div>", count: 400) + "x"] {289            let r = RichHTML.parse(html, mode: .paste)290            _ = RichHTML.html(from: r.text)291            _ = RichHTML.plainText(from: r.text)292            #expect(!r.text.string.contains(RichText.attachmentCharacter))293        }294        #expect(RichHTML.parse("<div><b><i>a</div></i></b>b", mode: .paste).text.string == "a\nb")295        #expect(RichText.block(ofParagraph: NSRange(location: 0, length: 7), in: RichHTML.parse("<blockquote><blockquote><blockquote><blockquote><blockquote><blockquote>profond", mode: .paste).text).quote == RichBlock.maxQuote)296    }297298    @Test func entitiesWhitespaceAndPreformattedText() {299        #expect(RichHTML.parse("<p>  a \n\t b&amp;c &lt;d&gt; &eacute;&#233;&#x1F600; </p>\n\n<p>e</p>", mode: .paste).text.string == "a b&c <d> éé😀\ne")300        let pre = RichHTML.parse("<pre>let a = 1\n  let b = 2</pre>", mode: .paste).text301        #expect(pre.string == "let a = 1\n  let b = 2")302        #expect(pre.attribute(RichKey.code, at: 0, effectiveRange: nil) != nil)303    }304305    @Test func blockModelHelpers() {306        for raw in ["p", "h1", "h3", "ul1", "ol6", ">p", ">>ul2", ">>>>ol3"] { #expect(RichBlock(rawValue: raw).rawValue == raw) }307        #expect(RichBlock(rawValue: "ul99").level == RichBlock.maxLevel && RichBlock(rawValue: "h9").rawValue == "h3" && RichBlock(rawValue: "?").rawValue == "p")308        let image = RichImage(cid: "a@b", name: "n.png", width: 160)309        #expect(RichImage(rawValue: image.rawValue) == image && RichImage(rawValue: RichImage(cid: "c").rawValue) == RichImage(cid: "c") && RichImage(rawValue: "") == nil)310        #expect(RichImage.makeCID(domain: "stg.spboucher.ai").range(of: "^[A-Za-z0-9._@-]{1,100}$", options: .regularExpression) != nil)311        #expect(RichImage.scaledSize(width: 4000, height: 3000, maxPixel: 1600) == (1600, 1200) && RichImage.scaledSize(width: 800, height: 600, maxPixel: 1600) == (800, 600))312        #expect(RichImage.Preset.matching(width: 320) == .medium && RichImage.Preset.matching(width: nil) == .original && RichImage.Preset.matching(width: 77) == .original)313        // a paragraph whose characters disagree about their block takes the first character's314        let mixed = NSMutableAttributedString(string: "ab\ncd")315        mixed.addAttribute(RichKey.block, value: "ul1", range: NSRange(location: 0, length: 1))316        mixed.addAttribute(RichKey.block, value: "h1", range: NSRange(location: 1, length: 3))317        RichText.normalizeBlocks(mixed, in: NSRange(location: 0, length: 5))318        #expect(RichHTML.html(from: mixed) == "<ul><li>ab</li></ul><h1>cd</h1>")319    }320}321
Files main
Menu

Theme