Skip to content

Commit 3f15ec7

Browse files
committed
Fix: existing attributes are not replaced
1 parent fd5641d commit 3f15ec7

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
@@ -232,6 +232,9 @@ class TestXMLDocument : LoopbackServerTest {
232232
XCTAssertEqual(element.xmlString, "<root></root>", element.xmlString)
233233

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

236239
let otherAttribute = XMLNode.attribute(withName: "foo", stringValue: "bar") as! XMLNode
237240
element.addAttribute(otherAttribute)
@@ -242,7 +245,7 @@ class TestXMLDocument : LoopbackServerTest {
242245
}
243246

244247
XCTAssertEqual(attributes.count, 2)
245-
XCTAssertEqual(attributes.first, attribute)
248+
XCTAssertEqual(attributes.first, attribute2)
246249
XCTAssertEqual(attributes.last, otherAttribute)
247250

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

0 commit comments

Comments
 (0)