Skip to content

Commit fc4a6cc

Browse files
authored
Merge pull request #2473 from millenomi/xmldocument-xmlnode-init
2 parents 789fd59 + fcbc060 commit fc4a6cc

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

Foundation/XMLDocument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ open class XMLDocument : XMLNode {
6767
return _CFXMLDocPtr(_xmlNode)
6868
}
6969

70-
public init() {
71-
NSUnimplemented()
70+
public convenience init() {
71+
self.init(rootElement: nil)
7272
}
7373

7474
/*!

Foundation/XMLElement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ open class XMLElement: XMLNode {
237237
}
238238

239239
set {
240-
if var nodes = newValue?.map({ $0._xmlNode }) {
240+
if var nodes = newValue?.map({ $0._xmlNode! }) {
241241
nodes.withUnsafeMutableBufferPointer({ (bufPtr) in
242242
_CFXMLSetNamespaces(_xmlNode, bufPtr.baseAddress, bufPtr.count)
243243
})

Foundation/XMLNode.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,20 @@ open class XMLNode: NSObject, NSCopying {
9696
return copy(with: nil)
9797
}
9898

99-
internal let _xmlNode: _CFXMLNodePtr
99+
internal let _xmlNode: _CFXMLNodePtr!
100100
internal var _xmlDocument: XMLDocument?
101101

102102
open func copy(with zone: NSZone? = nil) -> Any {
103103
let newNode = _CFXMLCopyNode(_xmlNode, true)
104104
return XMLNode._objectNodeForNode(newNode)
105105
}
106106

107+
@available(*, deprecated, message: "On Darwin, this initializer creates nodes that are valid objects but crash your process if used. The same behavior is replicated in swift-corelibs-foundation, but you should not use this initializer on either platform; use one of the class methods or initializers instead to create a specific kind of code.")
108+
public convenience override init() {
109+
// Match the Darwin behavior.
110+
self.init(kind: .invalid)
111+
}
112+
107113
/*!
108114
@method initWithKind:
109115
@abstract Invokes @link initWithKind:options: @/link with options set to NSXMLNodeOptionsNone
@@ -138,13 +144,15 @@ open class XMLNode: NSObject, NSCopying {
138144
_xmlNode = _CFXMLNewNamespace("", "")
139145

140146
default:
141-
fatalError("invalid node kind for this initializer")
147+
_xmlNode = nil
142148
}
143149

144150
super.init()
145151

146-
withOpaqueUnretainedReference {
147-
_CFXMLNodeSetPrivateData(_xmlNode, $0)
152+
if let node = _xmlNode {
153+
withOpaqueUnretainedReference {
154+
_CFXMLNodeSetPrivateData(node, $0)
155+
}
148156
}
149157
}
150158

@@ -795,6 +803,8 @@ open class XMLNode: NSObject, NSCopying {
795803
internal var _childNodes: Set<XMLNode> = []
796804

797805
deinit {
806+
guard _xmlNode != nil else { return }
807+
798808
for node in _childNodes {
799809
node.detach()
800810
}
@@ -882,7 +892,7 @@ open class XMLNode: NSObject, NSCopying {
882892
_CFXMLNodeAddPrevSibling(first, child._xmlNode)
883893
} else {
884894
let currChild = self.child(at: index - 1)!._xmlNode
885-
_CFXMLNodeAddNextSibling(currChild, child._xmlNode)
895+
_CFXMLNodeAddNextSibling(currChild!, child._xmlNode)
886896
}
887897
}
888898

TestFoundation/TestXMLDocument.swift

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,6 @@
99

1010
class TestXMLDocument : LoopbackServerTest {
1111

12-
static var allTests: [(String, (TestXMLDocument) -> () throws -> Void)] {
13-
return [
14-
("test_basicCreation", test_basicCreation),
15-
("test_nextPreviousNode", test_nextPreviousNode),
16-
// Disabled because of https://bugs.swift.org/browse/SR-10098
17-
// ("test_xpath", test_xpath),
18-
("test_elementCreation", test_elementCreation),
19-
("test_elementChildren", test_elementChildren),
20-
("test_stringValue", test_stringValue),
21-
("test_objectValue", test_objectValue),
22-
("test_attributes", test_attributes),
23-
("test_attributesWithNamespace", test_attributesWithNamespace),
24-
("test_comments", test_comments),
25-
("test_processingInstruction", test_processingInstruction),
26-
("test_parseXMLString", test_parseXMLString),
27-
("test_prefixes", test_prefixes),
28-
/* ⚠️ */ ("test_validation_success", testExpectedToFail(test_validation_success,
29-
/* ⚠️ */ #"<https://bugs.swift.org/browse/SR-10643> Could not build URI for external subset "http://127.0.0.1:-2/DTDs/PropertyList-1.0.dtd""#)),
30-
/* ⚠️ */ ("test_validation_failure", testExpectedToFail(test_validation_failure,
31-
/* ⚠️ */ "<https://bugs.swift.org/browse/SR-10643> XCTAssert in last catch block fails")),
32-
("test_dtd", test_dtd),
33-
("test_documentWithDTD", test_documentWithDTD),
34-
("test_dtd_attributes", test_dtd_attributes),
35-
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash),
36-
("test_nodeFindingWithNamespaces", test_nodeFindingWithNamespaces),
37-
("test_createElement", test_createElement),
38-
("test_addNamespace", test_addNamespace),
39-
("test_removeNamespace", test_removeNamespace),
40-
("test_optionPreserveAll", test_optionPreserveAll),
41-
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
42-
("test_nodeKinds", test_nodeKinds),
43-
("test_sr10776_documentName", test_sr10776_documentName),
44-
]
45-
}
46-
4712
func test_basicCreation() {
4813
let doc = XMLDocument(rootElement: nil)
4914
XCTAssert(doc.version == "1.0", "expected 1.0, got \(String(describing: doc.version))")
@@ -649,6 +614,47 @@ class TestXMLDocument : LoopbackServerTest {
649614
doc.name = "name"
650615
XCTAssertNil(doc.name) // `name` of XMLDocument is always nil.
651616
}
617+
618+
func test_creatingAnEmptyDocumentAndNode() {
619+
_ = XMLDocument()
620+
_ = XMLNode()
621+
}
622+
623+
static var allTests: [(String, (TestXMLDocument) -> () throws -> Void)] {
624+
return [
625+
("test_basicCreation", test_basicCreation),
626+
("test_nextPreviousNode", test_nextPreviousNode),
627+
// Disabled because of https://bugs.swift.org/browse/SR-10098
628+
// ("test_xpath", test_xpath),
629+
("test_elementCreation", test_elementCreation),
630+
("test_elementChildren", test_elementChildren),
631+
("test_stringValue", test_stringValue),
632+
("test_objectValue", test_objectValue),
633+
("test_attributes", test_attributes),
634+
("test_attributesWithNamespace", test_attributesWithNamespace),
635+
("test_comments", test_comments),
636+
("test_processingInstruction", test_processingInstruction),
637+
("test_parseXMLString", test_parseXMLString),
638+
("test_prefixes", test_prefixes),
639+
/* ⚠️ */ ("test_validation_success", testExpectedToFail(test_validation_success,
640+
/* ⚠️ */ #"<https://bugs.swift.org/browse/SR-10643> Could not build URI for external subset "http://127.0.0.1:-2/DTDs/PropertyList-1.0.dtd""#)),
641+
/* ⚠️ */ ("test_validation_failure", testExpectedToFail(test_validation_failure,
642+
/* ⚠️ */ "<https://bugs.swift.org/browse/SR-10643> XCTAssert in last catch block fails")),
643+
("test_dtd", test_dtd),
644+
("test_documentWithDTD", test_documentWithDTD),
645+
("test_dtd_attributes", test_dtd_attributes),
646+
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash),
647+
("test_nodeFindingWithNamespaces", test_nodeFindingWithNamespaces),
648+
("test_createElement", test_createElement),
649+
("test_addNamespace", test_addNamespace),
650+
("test_removeNamespace", test_removeNamespace),
651+
("test_optionPreserveAll", test_optionPreserveAll),
652+
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
653+
("test_nodeKinds", test_nodeKinds),
654+
("test_sr10776_documentName", test_sr10776_documentName),
655+
("test_creatingAnEmptyDocumentAndNode", test_creatingAnEmptyDocumentAndNode),
656+
]
657+
}
652658
}
653659

654660
fileprivate extension XMLNode {

0 commit comments

Comments
 (0)