mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 09:18:18 +00:00
Drop illegal XML characters in escapingSpecialXMLCharacters.
This commit is contained in:
1 parent
c314885422
commit
858b65931d
2 files changed
+43
-6
No files matched your search
@@ -282,14 +282,13 @@ public extension String {
|
||||
self.range(of: string, options: .caseInsensitive) != nil
|
||||
}
|
||||
|
||||
/// Returns the string with the special XML characters (other than single-quote) ampersand-escaped.
|
||||
///
|
||||
/// The four escaped characters are `<`, `>`, `&`, and `"`.
|
||||
/// Returns the string with the special XML characters ampersand-escaped.
|
||||
/// Also drops characters that are illegal in XML.
|
||||
var escapingSpecialXMLCharacters: String {
|
||||
var escaped = String()
|
||||
|
||||
for char in self {
|
||||
switch char {
|
||||
for scalar in unicodeScalars {
|
||||
switch scalar {
|
||||
case "&":
|
||||
escaped.append("&")
|
||||
case "<":
|
||||
@@ -298,8 +297,12 @@ public extension String {
|
||||
escaped.append(">")
|
||||
case "\"":
|
||||
escaped.append(""")
|
||||
case "'":
|
||||
escaped.append("'")
|
||||
default:
|
||||
escaped.append(char)
|
||||
if scalar.isLegalXMLCharacter {
|
||||
escaped.unicodeScalars.append(scalar)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,3 +324,13 @@ public extension String {
|
||||
self.stripping(prefix: "http://").stripping(prefix: "https://")
|
||||
}
|
||||
}
|
||||
|
||||
private extension Unicode.Scalar {
|
||||
|
||||
var isLegalXMLCharacter: Bool {
|
||||
value == 0x9 || value == 0xA || value == 0xD
|
||||
|| (value >= 0x20 && value <= 0xD7FF)
|
||||
|| (value >= 0xE000 && value <= 0xFFFD)
|
||||
|| (value >= 0x10000 && value <= 0x10FFFF)
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,30 @@ final class StringRSCoreTests: XCTestCase {
|
||||
|
||||
}
|
||||
|
||||
func testEscapingSpecialXMLCharactersEscapesApostrophe() {
|
||||
|
||||
let str = "it's a 'test'"
|
||||
XCTAssertEqual(str.escapingSpecialXMLCharacters, "it's a 'test'")
|
||||
}
|
||||
|
||||
func testEscapingSpecialXMLCharactersDropsInvalidControlCharacters() {
|
||||
|
||||
let str = "a\u{0}b\u{1}c\u{1F}d"
|
||||
XCTAssertEqual(str.escapingSpecialXMLCharacters, "abcd")
|
||||
}
|
||||
|
||||
func testEscapingSpecialXMLCharactersKeepsTabNewlineAndCarriageReturn() {
|
||||
|
||||
let str = "a\tb\nc\rd"
|
||||
XCTAssertEqual(str.escapingSpecialXMLCharacters, "a\tb\nc\rd")
|
||||
}
|
||||
|
||||
func testEscapingSpecialXMLCharactersLeavesUnicodeIntact() {
|
||||
|
||||
let str = "café — 🎉"
|
||||
XCTAssertEqual(str.escapingSpecialXMLCharacters, "café — 🎉")
|
||||
}
|
||||
|
||||
func testStrippingHTTPOrHTTPSScheme() {
|
||||
|
||||
let http = "http://ranchero.com/"
|
||||
|
||||
Reference in new issue
Block a user