2025-04-21 20:23:41 -07:00
|
|
|
|
//
|
|
|
|
|
|
// EntityDecodingTests.swift
|
|
|
|
|
|
// RSParserTests
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Brent Simmons on 12/30/17.
|
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
2026-04-19 11:02:42 -07:00
|
|
|
|
import Testing
|
2025-04-21 20:23:41 -07:00
|
|
|
|
import RSParser
|
|
|
|
|
|
|
2026-04-19 11:02:42 -07:00
|
|
|
|
@Suite struct EntityDecodingTests {
|
2025-04-21 20:23:41 -07:00
|
|
|
|
|
2026-04-19 11:02:42 -07:00
|
|
|
|
@Test("Decimal entity ' (single-quote bug from micro.blog)")
|
|
|
|
|
|
func decimalEntity39() {
|
|
|
|
|
|
// Bug found by Manton Reece — the ' entity was not getting decoded
|
|
|
|
|
|
// by NetNewsWire in JSON Feeds from micro.blog.
|
2025-04-21 20:23:41 -07:00
|
|
|
|
let s = "These are the times that try men's souls."
|
2026-04-20 11:50:23 -07:00
|
|
|
|
let decoded = s.decodingHTMLEntities()
|
2026-04-19 11:02:42 -07:00
|
|
|
|
#expect(decoded == "These are the times that try men's souls.")
|
2025-04-21 20:23:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 11:02:42 -07:00
|
|
|
|
@Test("Decimal and hex entities decode to the same characters",
|
|
|
|
|
|
arguments: [
|
|
|
|
|
|
("…", "…"),
|
|
|
|
|
|
("…", "…"),
|
|
|
|
|
|
("'", "'"),
|
|
|
|
|
|
("§", "§"),
|
|
|
|
|
|
("£", "£")
|
|
|
|
|
|
])
|
|
|
|
|
|
func entityPair(_ input: String, _ expected: String) {
|
2026-04-20 11:50:23 -07:00
|
|
|
|
#expect(input.decodingHTMLEntities() == expected)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Predefined XML entities (required by every parser without a DTD)
|
|
|
|
|
|
|
|
|
|
|
|
@Test("All five predefined XML entities decode individually",
|
|
|
|
|
|
arguments: [
|
|
|
|
|
|
("&", "&"),
|
|
|
|
|
|
("<", "<"),
|
|
|
|
|
|
(">", ">"),
|
|
|
|
|
|
(""", "\""),
|
|
|
|
|
|
("'", "'")
|
|
|
|
|
|
])
|
|
|
|
|
|
func predefinedXMLEntity(_ input: String, _ expected: String) {
|
|
|
|
|
|
#expect(input.decodingHTMLEntities() == expected)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test("Predefined XML entities decode when embedded in surrounding text") func predefinedXMLEntitiesInContext() {
|
|
|
|
|
|
#expect("Tom & Jerry".decodingHTMLEntities() == "Tom & Jerry")
|
|
|
|
|
|
#expect("<script>alert(1)</script>".decodingHTMLEntities() == "<script>alert(1)</script>")
|
|
|
|
|
|
#expect("He said "hi"".decodingHTMLEntities() == "He said \"hi\"")
|
|
|
|
|
|
#expect("it's".decodingHTMLEntities() == "it's")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-04 17:16:10 -07:00
|
|
|
|
// MARK: - HTML named entities
|
|
|
|
|
|
|
|
|
|
|
|
@Test("Zero-width joiner/non-joiner named entities decode (issue #3220)",
|
|
|
|
|
|
arguments: [
|
|
|
|
|
|
("‍", "\u{200D}"),
|
|
|
|
|
|
("‌", "\u{200C}")
|
|
|
|
|
|
])
|
|
|
|
|
|
func zeroWidthEntity(_ input: String, _ expected: String) {
|
|
|
|
|
|
#expect(input.decodingHTMLEntities() == expected)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func zeroWidthJoinerInContext() {
|
|
|
|
|
|
#expect("a‍b".decodingHTMLEntities() == "a\u{200D}b")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 11:50:23 -07:00
|
|
|
|
// MARK: - Malformed entities pass through literally
|
|
|
|
|
|
|
|
|
|
|
|
// "Pass through" here means: the raw characters must appear verbatim in
|
|
|
|
|
|
// the output, exactly as they were in the input. libxml2 behavior; our
|
|
|
|
|
|
// Swift decoder is intentionally the same.
|
|
|
|
|
|
|
|
|
|
|
|
@Test func unknownNamedEntityPassesThrough() {
|
|
|
|
|
|
// Unknown entity: the entity spelling stays literal.
|
|
|
|
|
|
#expect("&foo;".decodingHTMLEntities() == "&foo;")
|
|
|
|
|
|
#expect("hello &bogus; world".decodingHTMLEntities() == "hello &bogus; world")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func entityWithoutSemicolonPassesThrough() {
|
|
|
|
|
|
// Malformed (no `;`): the whole chunk stays as-is.
|
|
|
|
|
|
#expect("&".decodingHTMLEntities() == "&")
|
|
|
|
|
|
#expect("a& b".decodingHTMLEntities() == "a& b")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func emptyEntityReferencePassesThrough() {
|
|
|
|
|
|
// `&;` is not a valid entity — leave it alone.
|
|
|
|
|
|
#expect("&;".decodingHTMLEntities() == "&;")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func lonelyAmpersandPassesThrough() {
|
|
|
|
|
|
// Bare `&` with nothing after it.
|
|
|
|
|
|
#expect("&".decodingHTMLEntities() == "&")
|
|
|
|
|
|
#expect("a & b".decodingHTMLEntities() == "a & b")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func malformedNumericEntitiesPassThrough() {
|
|
|
|
|
|
// `&#;` and `&#x;` have no digits.
|
|
|
|
|
|
#expect("&#;".decodingHTMLEntities() == "&#;")
|
|
|
|
|
|
#expect("&#x;".decodingHTMLEntities() == "&#x;")
|
|
|
|
|
|
// Non-digit body.
|
|
|
|
|
|
#expect("&#abc;".decodingHTMLEntities() == "&#abc;")
|
|
|
|
|
|
#expect("&#xZZ;".decodingHTMLEntities() == "&#xZZ;")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Numeric entity edge cases
|
|
|
|
|
|
|
|
|
|
|
|
@Test func numericEntityZeroPassesThrough() {
|
|
|
|
|
|
// Codepoint 0 is not representable — decoder treats it as malformed.
|
|
|
|
|
|
#expect("�".decodingHTMLEntities() == "�")
|
|
|
|
|
|
#expect("�".decodingHTMLEntities() == "�")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func numericEntityOutOfRangePassesThrough() {
|
|
|
|
|
|
// Valid Unicode is 0x0 … 0x10FFFF. Anything above must not decode.
|
|
|
|
|
|
#expect("�".decodingHTMLEntities() == "�")
|
|
|
|
|
|
#expect("�".decodingHTMLEntities() == "�")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test("Windows Latin-1 extension remap (128-159 → Windows-1252 codepoints)",
|
|
|
|
|
|
arguments: [
|
|
|
|
|
|
// Historical HTML authors typed `€` expecting the Windows-1252
|
|
|
|
|
|
// codepoint (€) even though U+0080 is a C1 control character.
|
|
|
|
|
|
// WebKit's HTMLEntityParser applies this remap; our decoder matches.
|
|
|
|
|
|
("€", "€"), // 0x80 → U+20AC EURO SIGN
|
|
|
|
|
|
("‚", "‚"), // 0x82 → U+201A
|
|
|
|
|
|
("…", "…"), // 0x85 → U+2026 HORIZONTAL ELLIPSIS
|
|
|
|
|
|
("†", "†"), // 0x86 → U+2020 DAGGER
|
|
|
|
|
|
("‘", "‘"), // 0x91 → U+2018 LEFT SINGLE QUOTATION MARK
|
|
|
|
|
|
("’", "’"), // 0x92 → U+2019 RIGHT SINGLE QUOTATION MARK
|
|
|
|
|
|
("“", "“"), // 0x93 → U+201C LEFT DOUBLE QUOTATION MARK
|
|
|
|
|
|
("”", "”"), // 0x94 → U+201D RIGHT DOUBLE QUOTATION MARK
|
|
|
|
|
|
("–", "–"), // 0x96 → U+2013 EN DASH
|
|
|
|
|
|
("—", "—"), // 0x97 → U+2014 EM DASH
|
|
|
|
|
|
("™", "™"), // 0x99 → U+2122 TRADE MARK SIGN
|
|
|
|
|
|
// Hex spelling must be remapped the same way.
|
|
|
|
|
|
("€", "€"),
|
|
|
|
|
|
("–", "–")
|
|
|
|
|
|
])
|
|
|
|
|
|
func windowsLatin1Remap(_ input: String, _ expected: String) {
|
|
|
|
|
|
#expect(input.decodingHTMLEntities() == expected)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Mixed and chained entities
|
|
|
|
|
|
|
|
|
|
|
|
@Test func multipleConsecutiveEntities() {
|
|
|
|
|
|
#expect("&<>".decodingHTMLEntities() == "&<>")
|
|
|
|
|
|
#expect("“”".decodingHTMLEntities() == "“”")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func entitiesMixedWithPlainText() {
|
|
|
|
|
|
let input = "One & two: ‘three’ — — —."
|
|
|
|
|
|
let expected = "One & two: ‘three’ — — —."
|
|
|
|
|
|
#expect(input.decodingHTMLEntities() == expected)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func stringWithNoEntitiesUnchanged() {
|
|
|
|
|
|
// Fast-path case: no `&` in input, returned unchanged.
|
|
|
|
|
|
let input = "Just some text with no entities at all."
|
|
|
|
|
|
#expect(input.decodingHTMLEntities() == input)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func emptyStringUnchanged() {
|
|
|
|
|
|
#expect("".decodingHTMLEntities() == "")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Hex-digit case, boundary, and 4-byte UTF-8 (Tier 2 pinning)
|
|
|
|
|
|
|
|
|
|
|
|
@Test func hexEntityIsCaseInsensitiveForDigits() {
|
|
|
|
|
|
// Hex digits a-f and A-F must both decode the same. Pin as a paired
|
|
|
|
|
|
// test — if only one case works, the assertion pair fails rather than
|
|
|
|
|
|
// one or the other silently passing.
|
|
|
|
|
|
let lower = "«".decodingHTMLEntities()
|
|
|
|
|
|
let upper = "«".decodingHTMLEntities()
|
|
|
|
|
|
#expect(lower == upper)
|
|
|
|
|
|
#expect(lower == "«") // U+00AB
|
|
|
|
|
|
// Mixed case too.
|
|
|
|
|
|
#expect("«".decodingHTMLEntities() == "«")
|
|
|
|
|
|
#expect("«".decodingHTMLEntities() == "«")
|
|
|
|
|
|
// The `x` itself may also be uppercase (`&#X…;`).
|
|
|
|
|
|
#expect("…".decodingHTMLEntities() == "…")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func entityAtEndOfInputBoundary() {
|
|
|
|
|
|
// Entity with trailing semicolon right at EOF decodes.
|
|
|
|
|
|
#expect("foo&".decodingHTMLEntities() == "foo&")
|
|
|
|
|
|
// Entity with no semicolon and no more input passes through literally
|
|
|
|
|
|
// (scanner must not walk off the end of the buffer).
|
|
|
|
|
|
#expect("foo&".decodingHTMLEntities() == "foo&")
|
|
|
|
|
|
// Bare `&` at EOF.
|
|
|
|
|
|
#expect("foo&".decodingHTMLEntities() == "foo&")
|
|
|
|
|
|
// Numeric entity at EOF with no digits.
|
|
|
|
|
|
#expect("foo&#".decodingHTMLEntities() == "foo&#")
|
|
|
|
|
|
#expect("foo&#x".decodingHTMLEntities() == "foo&#x")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test func fourByteUTF8EmojiDecodes() {
|
|
|
|
|
|
// High codepoints need 4-byte UTF-8 encoding. Exercises the full
|
|
|
|
|
|
// encode-as-UTF-8 path in the decoder, not the 1/2/3-byte shortcuts.
|
|
|
|
|
|
#expect("😀".decodingHTMLEntities() == "😀") // U+1F600
|
|
|
|
|
|
#expect("😀".decodingHTMLEntities() == "😀") // decimal form
|
|
|
|
|
|
#expect("👋".decodingHTMLEntities() == "👋") // U+1F44B
|
|
|
|
|
|
// Round-trip: the decoded string's UTF-8 should be the canonical
|
|
|
|
|
|
// 4-byte sequence for 😀 (0xF0 0x9F 0x98 0x80).
|
|
|
|
|
|
let bytes = Array("😀".decodingHTMLEntities().utf8)
|
|
|
|
|
|
#expect(bytes == [0xF0, 0x9F, 0x98, 0x80])
|
2025-04-21 20:23:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|