Skip to content

SR-10717: var kind: XMLNode.Kind { get } should return a correct value on Linux. #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion CoreFoundation/Parsing.subproj/CFXMLInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ CFIndex _kCFXMLTypeInvalid = 0;
CFIndex _kCFXMLTypeDocument = XML_DOCUMENT_NODE;
CFIndex _kCFXMLTypeElement = XML_ELEMENT_NODE;
CFIndex _kCFXMLTypeAttribute = XML_ATTRIBUTE_NODE;
CFIndex _kCFXMLTypeProcessingInstruction = XML_PI_NODE;
CFIndex _kCFXMLTypeComment = XML_COMMENT_NODE;
CFIndex _kCFXMLTypeText = XML_TEXT_NODE;
CFIndex _kCFXMLTypeDTD = XML_DTD_NODE;
CFIndex _kCFXMLDocTypeHTML = XML_DOC_HTML;
CFIndex _kCFXMLTypeNamespace = 22; // libxml2 does not define namespaces as nodes, so we have to fake it
Expand Down Expand Up @@ -1006,6 +1009,14 @@ _CFXMLDTDPtr _CFXMLNewDTD(_CFXMLDocPtr doc, const unsigned char* name, const uns
return xmlNewDtd(doc, name, publicID, systemID);
}

void _CFXMLNotationScanner(void* payload, void* data, xmlChar* name) {
xmlNotationPtr notation = (xmlNotationPtr)payload;
_cfxmlNotation* node = (_cfxmlNotation*)data;
node->type = XML_NOTATION_NODE;
node->name = notation->name;
node->notation = notation;
}

_CFXMLDTDNodePtr _CFXMLParseDTDNode(const unsigned char* xmlString) {
CFDataRef data = CFDataCreateWithBytesNoCopy(NULL, xmlString, xmlStrlen(xmlString), kCFAllocatorNull);
xmlDtdPtr dtd = _CFXMLParseDTDFromData(data, NULL);
Expand All @@ -1016,7 +1027,12 @@ _CFXMLDTDNodePtr _CFXMLParseDTDNode(const unsigned char* xmlString) {
}

xmlNodePtr node = dtd->children;
xmlUnlinkNode(node);
if (node != NULL) {
xmlUnlinkNode(node);
} else if (dtd->notations) {
node = (xmlNodePtr)calloc(1, sizeof(_cfxmlNotation));
xmlHashScan((xmlNotationTablePtr)dtd->notations, &_CFXMLNotationScanner, (void*)node);
}

return node;
}
Expand Down
3 changes: 3 additions & 0 deletions CoreFoundation/Parsing.subproj/CFXMLInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ extern CFIndex _kCFXMLTypeInvalid;
extern CFIndex _kCFXMLTypeDocument;
extern CFIndex _kCFXMLTypeElement;
extern CFIndex _kCFXMLTypeAttribute;
extern CFIndex _kCFXMLTypeProcessingInstruction;
extern CFIndex _kCFXMLTypeComment;
extern CFIndex _kCFXMLTypeText;
extern CFIndex _kCFXMLTypeDTD;
extern CFIndex _kCFXMLDocTypeHTML;
extern CFIndex _kCFXMLTypeNamespace;
Expand Down
9 changes: 9 additions & 0 deletions Foundation/XMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ open class XMLNode: NSObject, NSCopying {
case _kCFXMLTypeNamespace:
return .namespace

case _kCFXMLTypeProcessingInstruction:
return .processingInstruction

case _kCFXMLTypeComment:
return .comment

case _kCFXMLTypeText:
return .text

default:
return .invalid
}
Expand Down
16 changes: 16 additions & 0 deletions TestFoundation/TestXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TestXMLDocument : LoopbackServerTest {
("test_removeNamespace", test_removeNamespace),
("test_optionPreserveAll", test_optionPreserveAll),
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
("test_nodeKinds", test_nodeKinds),
]
}

Expand Down Expand Up @@ -550,6 +551,21 @@ class TestXMLDocument : LoopbackServerTest {

XCTAssertEqual(try? test(), "plans")
}

func test_nodeKinds() {
XCTAssertEqual(XMLDocument(rootElement: nil).kind, .document)
XCTAssertEqual(XMLElement(name: "prefix:localName").kind, .element)
XCTAssertEqual((XMLNode.attribute(withName: "name", stringValue: "value") as? XMLNode)?.kind, .attribute)
XCTAssertEqual((XMLNode.namespace(withName: "namespace", stringValue: "http://example.com/") as? XMLNode)?.kind, .namespace)
XCTAssertEqual((XMLNode.processingInstruction(withName: "name", stringValue: "value") as? XMLNode)?.kind, .processingInstruction)
XCTAssertEqual((XMLNode.comment(withStringValue: "comment") as? XMLNode)?.kind, .comment)
XCTAssertEqual((XMLNode.text(withStringValue: "text") as? XMLNode)?.kind, .text)
XCTAssertEqual((try? XMLDTD(data:#"<!ENTITY a "A">"#.data(using: .utf8)!))?.kind, .DTDKind)
XCTAssertEqual(XMLDTDNode(xmlString: #"<!ENTITY b "B">"#)?.kind, .entityDeclaration)
XCTAssertEqual(XMLDTDNode(xmlString: "<!ATTLIST A B CDATA #IMPLIED>")?.kind, .attributeDeclaration)
XCTAssertEqual(XMLDTDNode(xmlString: "<!ELEMENT E EMPTY>")?.kind, .elementDeclaration)
XCTAssertEqual(XMLDTDNode(xmlString: #"<!NOTATION f SYSTEM "F">"#)?.kind, .notationDeclaration)
}
}

fileprivate extension XMLNode {
Expand Down