File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed
Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -2,14 +2,27 @@ import Foundation
22
33extension String {
44 func addingXMLEncoding( ) -> String {
5- withCFString { string -> NSString in
6- CFXMLCreateStringByEscapingEntities ( nil , string , nil )
7- } as String
5+ var result = " "
6+ result . reserveCapacity ( count )
7+ return unicodeScalars . reduce ( into : result , { $0 . append ( $1 . named_escapingIfNeeded ) } )
88 }
9+ }
910
10- private func withCFString< Result> ( _ body: ( CFString ) throws -> Result ) rethrows -> Result {
11- try withCString { cString in
12- try body ( CFStringCreateWithCString ( nil , cString, CFStringBuiltInEncodings . UTF8. rawValue) )
11+ extension UnicodeScalar {
12+ fileprivate var named_escapingIfNeeded : String {
13+ switch value {
14+ case ( " & " as Unicode . Scalar) . value:
15+ return " & "
16+ case ( " < " as Unicode . Scalar ) . value:
17+ return " < "
18+ case ( " > " as Unicode . Scalar ) . value:
19+ return " > "
20+ case ( " \' " as Unicode . Scalar ) . value:
21+ return " ' "
22+ case ( " \" " as Unicode . Scalar ) . value:
23+ return " " "
24+ default:
25+ return String ( self )
1326 }
1427 }
1528}
Original file line number Diff line number Diff line change @@ -16,6 +16,13 @@ final class SwimTests: XCTestCase {
1616 XCTAssertEqual ( n. rendered, " \n 3 > 1 " )
1717 }
1818
19+ func testUnicodeAndEscaping( ) {
20+ let n : Node = " 500 € "
21+ var result = " "
22+ n. write ( to: & result)
23+ XCTAssertEqual ( result, " \n 500 € " )
24+ }
25+
1926 func testRaw( ) {
2027 let n : Node = Node . raw ( " <marquee>Hello</marquee> " )
2128 XCTAssertEqual ( n. rendered, " \n <marquee>Hello</marquee> " )
You can’t perform that action at this time.
0 commit comments