Skip to content

Commit 1dd5289

Browse files
authored
Merge pull request swiftlang#2330 from YOCKOW/sr10759
SR-10759: Avoid double encoding in `stringValue` of XMLNode.
2 parents ae7adfd + 0189697 commit 1dd5289

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Foundation/XMLNode.swift

+16-10
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,33 @@ open class XMLNode: NSObject, NSCopying {
372372
case .namespace:
373373
return _CFXMLNamespaceCopyValue(_xmlNode)?._swiftObject
374374

375+
case .element:
376+
// As with Darwin, children's string values are just concanated without spaces.
377+
return children?.compactMap({ $0.stringValue }).joined() ?? ""
378+
375379
default:
376380
return _CFXMLNodeCopyContent(_xmlNode)?._swiftObject
377381
}
378382
}
379383
set {
380-
if case .namespace = kind {
384+
switch kind {
385+
case .namespace:
381386
if let newValue = newValue {
382387
precondition(URL(string: newValue) != nil, "namespace stringValue must be a valid href")
383388
}
384-
385389
_CFXMLNamespaceSetValue(_xmlNode, newValue, Int64(newValue?.utf8.count ?? 0))
386-
return
387-
}
388390

389-
_removeAllChildNodesExceptAttributes() // in case anyone is holding a reference to any of these children we're about to destroy
391+
case .comment, .text:
392+
_CFXMLNodeSetContent(_xmlNode, newValue)
390393

391-
if let string = newValue {
392-
let newContent = _CFXMLEncodeEntities(_CFXMLNodeGetDocument(_xmlNode), string)?._swiftObject ?? ""
393-
_CFXMLNodeSetContent(_xmlNode, newContent)
394-
} else {
395-
_CFXMLNodeSetContent(_xmlNode, nil)
394+
default:
395+
_removeAllChildNodesExceptAttributes() // in case anyone is holding a reference to any of these children we're about to destroy
396+
if let string = newValue {
397+
let newContent = _CFXMLEncodeEntities(_CFXMLNodeGetDocument(_xmlNode), string)?._swiftObject ?? ""
398+
_CFXMLNodeSetContent(_xmlNode, newContent)
399+
} else {
400+
_CFXMLNodeSetContent(_xmlNode, nil)
401+
}
396402
}
397403
}
398404
}

TestFoundation/TestXMLDocument.swift

+19
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,26 @@ class TestXMLDocument : LoopbackServerTest {
199199
func test_stringValue() {
200200
let element = XMLElement(name: "root")
201201
let foo = XMLElement(name: "foo")
202+
let text = XMLNode.text(withStringValue:"<text>") as! XMLNode
203+
let comment = XMLNode.comment(withStringValue:"<comment>") as! XMLNode
204+
foo.addChild(text)
205+
foo.addChild(comment)
202206
element.addChild(foo)
207+
208+
XCTAssertEqual(text.stringValue, "<text>")
209+
XCTAssertEqual(comment.stringValue, "<comment>")
210+
XCTAssertEqual(foo.stringValue, "<text><comment>") // Same with Darwin
211+
XCTAssertEqual(element.stringValue, "<text><comment>") // Same with Darwin
212+
213+
// Confirm that SR-10759 is resolved.
214+
// https://bugs.swift.org/browse/SR-10759
215+
text.stringValue = "<modified text>"
216+
comment.stringValue = "<modified comment>"
217+
XCTAssertEqual(text.stringValue, "<modified text>")
218+
XCTAssertEqual(comment.stringValue, "<modified comment>")
219+
220+
XCTAssertEqual(element.stringValue, "<modified text><modified comment>")
221+
XCTAssertEqual(element.xmlString, "<root><foo>&lt;modified text&gt;<!--<modified comment>--></foo></root>")
203222

204223
element.stringValue = "Hello!<evil/>"
205224
XCTAssertEqual(element.xmlString, "<root>Hello!&lt;evil/&gt;</root>")

0 commit comments

Comments
 (0)