Skip to content

Commit 0189697

Browse files
committed
XMLNode: Avoid double encoding in stringValue.
It is unnecessary to encode entities when the node is comment or text. They will be encoded when XML is put out. Resolves [SR-10759](https://bugs.swift.org/browse/SR-10759).
1 parent 5fb434e commit 0189697

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Foundation/XMLNode.swift

Lines changed: 16 additions & 10 deletions
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
}

0 commit comments

Comments
 (0)