Skip to content

Commit 0a04b94

Browse files
authored
Merge pull request #2287 from YOCKOW/xmlnode-kind
SR-10717: `var kind: XMLNode.Kind { get }` should return a correct value on Linux.
2 parents f9b18eb + 381423e commit 0a04b94

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ CFIndex _kCFXMLTypeInvalid = 0;
5757
CFIndex _kCFXMLTypeDocument = XML_DOCUMENT_NODE;
5858
CFIndex _kCFXMLTypeElement = XML_ELEMENT_NODE;
5959
CFIndex _kCFXMLTypeAttribute = XML_ATTRIBUTE_NODE;
60+
CFIndex _kCFXMLTypeProcessingInstruction = XML_PI_NODE;
61+
CFIndex _kCFXMLTypeComment = XML_COMMENT_NODE;
62+
CFIndex _kCFXMLTypeText = XML_TEXT_NODE;
6063
CFIndex _kCFXMLTypeDTD = XML_DTD_NODE;
6164
CFIndex _kCFXMLDocTypeHTML = XML_DOC_HTML;
6265
CFIndex _kCFXMLTypeNamespace = 22; // libxml2 does not define namespaces as nodes, so we have to fake it
@@ -1006,6 +1009,14 @@ _CFXMLDTDPtr _CFXMLNewDTD(_CFXMLDocPtr doc, const unsigned char* name, const uns
10061009
return xmlNewDtd(doc, name, publicID, systemID);
10071010
}
10081011

1012+
void _CFXMLNotationScanner(void* payload, void* data, xmlChar* name) {
1013+
xmlNotationPtr notation = (xmlNotationPtr)payload;
1014+
_cfxmlNotation* node = (_cfxmlNotation*)data;
1015+
node->type = XML_NOTATION_NODE;
1016+
node->name = notation->name;
1017+
node->notation = notation;
1018+
}
1019+
10091020
_CFXMLDTDNodePtr _CFXMLParseDTDNode(const unsigned char* xmlString) {
10101021
CFDataRef data = CFDataCreateWithBytesNoCopy(NULL, xmlString, xmlStrlen(xmlString), kCFAllocatorNull);
10111022
xmlDtdPtr dtd = _CFXMLParseDTDFromData(data, NULL);
@@ -1016,7 +1027,12 @@ _CFXMLDTDNodePtr _CFXMLParseDTDNode(const unsigned char* xmlString) {
10161027
}
10171028

10181029
xmlNodePtr node = dtd->children;
1019-
xmlUnlinkNode(node);
1030+
if (node != NULL) {
1031+
xmlUnlinkNode(node);
1032+
} else if (dtd->notations) {
1033+
node = (xmlNodePtr)calloc(1, sizeof(_cfxmlNotation));
1034+
xmlHashScan((xmlNotationTablePtr)dtd->notations, &_CFXMLNotationScanner, (void*)node);
1035+
}
10201036

10211037
return node;
10221038
}

CoreFoundation/Parsing.subproj/CFXMLInterface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ extern CFIndex _kCFXMLTypeInvalid;
5353
extern CFIndex _kCFXMLTypeDocument;
5454
extern CFIndex _kCFXMLTypeElement;
5555
extern CFIndex _kCFXMLTypeAttribute;
56+
extern CFIndex _kCFXMLTypeProcessingInstruction;
57+
extern CFIndex _kCFXMLTypeComment;
58+
extern CFIndex _kCFXMLTypeText;
5659
extern CFIndex _kCFXMLTypeDTD;
5760
extern CFIndex _kCFXMLDocTypeHTML;
5861
extern CFIndex _kCFXMLTypeNamespace;

Foundation/XMLNode.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ open class XMLNode: NSObject, NSCopying {
294294
case _kCFXMLTypeNamespace:
295295
return .namespace
296296

297+
case _kCFXMLTypeProcessingInstruction:
298+
return .processingInstruction
299+
300+
case _kCFXMLTypeComment:
301+
return .comment
302+
303+
case _kCFXMLTypeText:
304+
return .text
305+
297306
default:
298307
return .invalid
299308
}

TestFoundation/TestXMLDocument.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TestXMLDocument : LoopbackServerTest {
3838
("test_removeNamespace", test_removeNamespace),
3939
("test_optionPreserveAll", test_optionPreserveAll),
4040
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
41+
("test_nodeKinds", test_nodeKinds),
4142
]
4243
}
4344

@@ -550,6 +551,21 @@ class TestXMLDocument : LoopbackServerTest {
550551

551552
XCTAssertEqual(try? test(), "plans")
552553
}
554+
555+
func test_nodeKinds() {
556+
XCTAssertEqual(XMLDocument(rootElement: nil).kind, .document)
557+
XCTAssertEqual(XMLElement(name: "prefix:localName").kind, .element)
558+
XCTAssertEqual((XMLNode.attribute(withName: "name", stringValue: "value") as? XMLNode)?.kind, .attribute)
559+
XCTAssertEqual((XMLNode.namespace(withName: "namespace", stringValue: "http://example.com/") as? XMLNode)?.kind, .namespace)
560+
XCTAssertEqual((XMLNode.processingInstruction(withName: "name", stringValue: "value") as? XMLNode)?.kind, .processingInstruction)
561+
XCTAssertEqual((XMLNode.comment(withStringValue: "comment") as? XMLNode)?.kind, .comment)
562+
XCTAssertEqual((XMLNode.text(withStringValue: "text") as? XMLNode)?.kind, .text)
563+
XCTAssertEqual((try? XMLDTD(data:#"<!ENTITY a "A">"#.data(using: .utf8)!))?.kind, .DTDKind)
564+
XCTAssertEqual(XMLDTDNode(xmlString: #"<!ENTITY b "B">"#)?.kind, .entityDeclaration)
565+
XCTAssertEqual(XMLDTDNode(xmlString: "<!ATTLIST A B CDATA #IMPLIED>")?.kind, .attributeDeclaration)
566+
XCTAssertEqual(XMLDTDNode(xmlString: "<!ELEMENT E EMPTY>")?.kind, .elementDeclaration)
567+
XCTAssertEqual(XMLDTDNode(xmlString: #"<!NOTATION f SYSTEM "F">"#)?.kind, .notationDeclaration)
568+
}
553569
}
554570

555571
fileprivate extension XMLNode {

0 commit comments

Comments
 (0)