File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,7 @@ open class XMLNode: NSObject, NSCopying {
91
91
}
92
92
93
93
internal let _xmlNode : _CFXMLNodePtr
94
+ internal var _xmlDocument : XMLDocument ?
94
95
95
96
open func copy( with zone: NSZone ? = nil ) -> Any {
96
97
let newNode = _CFXMLCopyNode ( _xmlNode, true )
@@ -761,6 +762,8 @@ open class XMLNode: NSObject, NSCopying {
761
762
node. detach ( )
762
763
}
763
764
765
+ _xmlDocument = nil
766
+
764
767
switch kind {
765
768
case . document:
766
769
_CFXMLFreeDocument ( _CFXMLDocPtr ( _xmlNode) )
@@ -790,6 +793,11 @@ open class XMLNode: NSObject, NSCopying {
790
793
withUnretainedReference {
791
794
_CFXMLNodeSetPrivateData ( _xmlNode, $0)
792
795
}
796
+ if let documentPtr = _CFXMLNodeGetDocument ( _xmlNode) {
797
+ if documentPtr != ptr {
798
+ _xmlDocument = XMLDocument . _objectNodeForNode ( documentPtr)
799
+ }
800
+ }
793
801
}
794
802
795
803
internal class func _objectNodeForNode( _ node: _CFXMLNodePtr ) -> XMLNode {
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ class TestXMLDocument : LoopbackServerTest {
34
34
( " test_addNamespace " , test_addNamespace) ,
35
35
( " test_removeNamespace " , test_removeNamespace) ,
36
36
( " test_optionPreserveAll " , test_optionPreserveAll) ,
37
+ ( " test_rootElementRetainsDocument " , test_rootElementRetainsDocument) ,
37
38
]
38
39
}
39
40
@@ -527,6 +528,22 @@ class TestXMLDocument : LoopbackServerTest {
527
528
let expected = xmlString. lowercased ( ) + " \n "
528
529
XCTAssertEqual ( expected, String ( describing: document) )
529
530
}
531
+
532
+ func test_rootElementRetainsDocument( ) {
533
+ let str = """
534
+ <?xml version= " 1.0 " encoding= " UTF-8 " ?>
535
+ <plans></plans>
536
+ """
537
+
538
+ let data = str. data ( using: . utf8) !
539
+
540
+ func test( ) throws -> String ? {
541
+ let doc = try XMLDocument ( data: data, options: [ ] ) . rootElement ( )
542
+ return doc? . name
543
+ }
544
+
545
+ XCTAssertEqual ( try ? test ( ) , " plans " )
546
+ }
530
547
}
531
548
532
549
fileprivate extension XMLNode {
You can’t perform that action at this time.
0 commit comments