From e1382490bf55dfd9f645d5c3fa3b3b66730a65df Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Mon, 27 May 2019 14:10:33 +0900 Subject: [PATCH 1/2] XMLDocument: Add a test for SR-10776. --- TestFoundation/TestXMLDocument.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/TestFoundation/TestXMLDocument.swift b/TestFoundation/TestXMLDocument.swift index f51bd9de96..ad8ea17cff 100644 --- a/TestFoundation/TestXMLDocument.swift +++ b/TestFoundation/TestXMLDocument.swift @@ -40,6 +40,7 @@ class TestXMLDocument : LoopbackServerTest { ("test_optionPreserveAll", test_optionPreserveAll), ("test_rootElementRetainsDocument", test_rootElementRetainsDocument), ("test_nodeKinds", test_nodeKinds), + ("test_sr10776_documentName", test_sr10776_documentName), ] } @@ -625,7 +626,7 @@ class TestXMLDocument : LoopbackServerTest { XCTAssertEqual(try? test(), "plans") } - + func test_nodeKinds() { XCTAssertEqual(XMLDocument(rootElement: nil).kind, .document) XCTAssertEqual(XMLElement(name: "prefix:localName").kind, .element) @@ -640,6 +641,14 @@ class TestXMLDocument : LoopbackServerTest { XCTAssertEqual(XMLDTDNode(xmlString: "")?.kind, .elementDeclaration) XCTAssertEqual(XMLDTDNode(xmlString: #""#)?.kind, .notationDeclaration) } + + func test_sr10776_documentName() { + let doc = XMLDocument(rootElement: nil) + XCTAssertNil(doc.name) + + doc.name = "name" + XCTAssertNil(doc.name) // `name` of XMLDocument is always nil. + } } fileprivate extension XMLNode { From ae3542d6d5418b97fe8cd021db3547e363ecace4 Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Mon, 27 May 2019 20:28:47 +0900 Subject: [PATCH 2/2] XMLNode: `var name` must be always `nil` when the node is document. Although Libxml2's `struct _xmlDoc` has a member named `name` that indicates name/filename/URI of the document, `var name` of `XMLDocument` on Darwin returns always nil. This commit makes the behavior the same with Darwin. --- CoreFoundation/Parsing.subproj/CFXMLInterface.c | 1 + Foundation/XMLNode.swift | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CoreFoundation/Parsing.subproj/CFXMLInterface.c b/CoreFoundation/Parsing.subproj/CFXMLInterface.c index 2702f433e4..9fcac433c7 100644 --- a/CoreFoundation/Parsing.subproj/CFXMLInterface.c +++ b/CoreFoundation/Parsing.subproj/CFXMLInterface.c @@ -495,6 +495,7 @@ static inline xmlChar* _getQName(xmlNodePtr node) { const xmlChar* ncname = node->name; switch (node->type) { + case XML_DOCUMENT_NODE: case XML_NOTATION_NODE: case XML_DTD_NODE: case XML_ELEMENT_DECL: diff --git a/Foundation/XMLNode.swift b/Foundation/XMLNode.swift index 05a265f1e9..a47a6a898c 100644 --- a/Foundation/XMLNode.swift +++ b/Foundation/XMLNode.swift @@ -328,9 +328,13 @@ open class XMLNode: NSObject, NSCopying { return returned == nil ? nil : unsafeBitCast(returned!, to: NSString.self) as String } set { - if case .namespace = kind { + switch kind { + case .document: + // As with Darwin, ignore the name when the node is document. + break + case .namespace: _CFXMLNamespaceSetPrefix(_xmlNode, newValue, Int64(newValue?.utf8.count ?? 0)) - } else { + default: if let newName = newValue { _CFXMLNodeSetName(_xmlNode, newName) } else {