// // StripHTMLTests.swift // RSCoreTests // // Created by Brent Simmons on 2025-10-20. // Copyright Β© 2025 Ranchero Software, LLC. All rights reserved. // import Foundation import Testing struct StripHTMLTests { @Test func basic() { #expect("

Hello world!

".strippingHTML() == "Hello world!") } @Test func withScript() { #expect("

Before

After

".strippingHTML() == "Before After") } @Test func withStyle() { #expect("

Content

More

".strippingHTML() == "Content More") } @Test func withMaxCharacters() { let html = "

This is a long piece of text that should be truncated at some point.

" let result = html.strippingHTML(maxCharacters: 20) #expect(result.count <= 20) #expect(result == "This is a long piece") } @Test func withUTF8() { #expect("

Hello δΈ–η•Œ 🌍

".strippingHTML() == "Hello δΈ–η•Œ 🌍") } @Test func whitespaceCollapsing() { let result = "

Too many\n\n\nspaces

".strippingHTML() #expect(!result.contains(" ")) #expect(result == "Too many spaces") } @Test func scriptAndStyleAreCaseInsensitive() { #expect("

Before

After

".strippingHTML() == "Before After") #expect("

x

y

".strippingHTML() == "x y") #expect("visible".strippingHTML() == "visible") } @Test func plainText() { #expect(" hello world ".strippingHTML() == "hello world") #expect("no html here".strippingHTML() == "no html here") #expect("\n\ttabs and\nnewlines\n".strippingHTML() == "tabs and newlines") } @Test func doesNotDecodeEntities() { #expect("

A & B

".strippingHTML() == "A & B") #expect("

<3 >9 "q"

".strippingHTML() == "<3 >9 "q"") #expect("

© —

".strippingHTML() == "© —") } @Test func emptyString() { #expect("".strippingHTML() == "") #expect("".strippingHTML(maxCharacters: 10) == "") #expect("".strippingHTML(maxCharacters: 0) == "") } @Test func blockTagsInjectSpaces() { #expect("a
b".strippingHTML() == "a b") #expect("a
b".strippingHTML() == "a b") #expect("a
b".strippingHTML() == "a b") #expect("
a
b
".strippingHTML() == "a b") #expect("
a
b
".strippingHTML() == "a b") #expect("".strippingHTML() == "a b") #expect("

a

b

".strippingHTML() == "a b") // Adjacent block-tag-injected spaces don't double up. #expect("

a

b
".strippingHTML() == "a b") } @Test func maxCharactersBoundaries() { let source = "hello world" #expect(source.strippingHTML(maxCharacters: nil) == "hello world") #expect(source.strippingHTML(maxCharacters: 0) == "hello world") #expect(source.strippingHTML(maxCharacters: 1) == "h") #expect(source.strippingHTML(maxCharacters: 5) == "hello") #expect(source.strippingHTML(maxCharacters: 100) == "hello world") } @Test func nestedTags() { #expect("
x
".strippingHTML() == "x") #expect("deeply nested".strippingHTML() == "deeply nested") #expect("link".strippingHTML() == "link") } @Test func greaterThanInsideQuotedAttribute() { // Issue #5339: a '>' inside a quoted attribute value (a CSS media query) // must not be treated as the end of the tag. let html = "= 2x) and (width >= 700px)\" type=\"image/avif\">

Hello

" #expect(html.strippingHTML() == "Hello") #expect(" b\">link".strippingHTML() == "link") #expect("
text
".strippingHTML() == "text") } @Test(arguments: ["daringfireball", "apple", "inessential", "scripting", "unsung"]) func withRealWorldHTML(testFile: String) throws { let url = try #require(Bundle.module.url(forResource: testFile, withExtension: "html", subdirectory: "Resources")) let html = try String(contentsOf: url, encoding: .utf8) let result = html.strippingHTML(maxCharacters: 300) #expect(!result.isEmpty, "Result should not be empty") #expect(result.count <= 300, "Result should respect maxCharacters") #expect(!result.contains("<"), "Result should not contain HTML tags") #expect(!result.contains("//"), "Should fully remove script content") } @Test(arguments: ["apple", "daringfireball", "inessential", "scripting", "unsung"]) func matchesExpectedOutput(testFile: String) throws { let htmlURL = try #require(Bundle.module.url(forResource: testFile, withExtension: "html", subdirectory: "Resources")) let txtURL = try #require(Bundle.module.url(forResource: testFile, withExtension: "txt", subdirectory: "Resources")) let html = try String(contentsOf: htmlURL, encoding: .utf8) let expectedOutput = try String(contentsOf: txtURL, encoding: .utf8) #expect(html.strippingHTML() == expectedOutput, "Implementation should match expected output") } // Un-comment this to regenerate the .txt files containing expected stripped-HTML results. // @Test func regenerateExpectedOutputFiles() throws { // let testFiles = ["apple", "daringfireball", "inessential", "scripting"] // for testFile in testFiles { // let htmlURL = try #require(Bundle.module.url(forResource: testFile, withExtension: "html", subdirectory: "Resources")) // let html = try String(contentsOf: htmlURL, encoding: .utf8) // let result = html.strippingHTML() // print("\n=== \(testFile) ===") // print("Result length: \(result.count) characters") // print("First 200 chars: \(String(result.prefix(200)))") // let tmpPath = "/tmp/\(testFile).txt" // try result.write(toFile: tmpPath, atomically: true, encoding: .utf8) // print("Wrote to: \(tmpPath)") // } // print("\n\nTo update the expected output files, run:") // print("cp /tmp/apple.txt Tests/RSCoreTests/Resources/") // print("cp /tmp/daringfireball.txt Tests/RSCoreTests/Resources/") // print("cp /tmp/inessential.txt Tests/RSCoreTests/Resources/") // print("cp /tmp/scripting.txt Tests/RSCoreTests/Resources/") // } }