Skip to content

Commit 97796f5

Browse files
authored
Merge pull request #2046 from hironytic/replace-by-add-attribute
Fix: existing attributes are not replaced
2 parents 86651e3 + 3f15ec7 commit 97796f5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Foundation/XMLElement.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,15 @@ open class XMLElement: XMLNode {
8282

8383
/*!
8484
@method addAttribute:
85-
@abstract Adds an attribute. Attributes with duplicate names are not added.
85+
@abstract Adds an attribute. Attributes with duplicate names replace the old one.
8686
*/
8787
open func addAttribute(_ attribute: XMLNode) {
8888
guard let name = _CFXMLNodeCopyName(attribute._xmlNode)?._swiftObject else {
8989
fatalError("Attributes must have a name!")
9090
}
9191

92-
name.cString(using: .utf8)!.withUnsafeBufferPointer() {
93-
guard let ptr = $0.baseAddress, _CFXMLNodeHasProp(_xmlNode, ptr) == nil else { return }
94-
addChild(attribute)
95-
}
92+
removeAttribute(forName: name)
93+
addChild(attribute)
9694
}
9795

9896
/*!

TestFoundation/TestXMLDocument.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ class TestXMLDocument : LoopbackServerTest {
233233
XCTAssertEqual(element.xmlString, "<root></root>", element.xmlString)
234234

235235
element.addAttribute(attribute)
236+
let attribute2 = XMLNode.attribute(withName: "color", stringValue: "#00ff00") as! XMLNode
237+
element.addAttribute(attribute2)
238+
XCTAssertEqual(element.attribute(forName: "color")?.stringValue, "#00ff00")
236239

237240
let otherAttribute = XMLNode.attribute(withName: "foo", stringValue: "bar") as! XMLNode
238241
element.addAttribute(otherAttribute)
@@ -243,7 +246,7 @@ class TestXMLDocument : LoopbackServerTest {
243246
}
244247

245248
XCTAssertEqual(attributes.count, 2)
246-
XCTAssertEqual(attributes.first, attribute)
249+
XCTAssertEqual(attributes.first, attribute2)
247250
XCTAssertEqual(attributes.last, otherAttribute)
248251

249252
let barAttribute = XMLNode.attribute(withName: "bar", stringValue: "buz") as! XMLNode

0 commit comments

Comments
 (0)