From 18c64eae4f03e92d0b4a8ed2274eeeb4b5091be0 Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Mon, 12 Mar 2012 22:33:27 +0000 Subject: [PATCH] * Source/NSXMLNode.m (-detach): Don't handle the namespace case. * Source/NSXMLElement.m (-removeNamespaceForPrefix:): Implement. * Tests/base/NSXMLNode/basic.m: Move tests from here ... * Tests/base/NSXMLElement/basic.m: ... to here. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34927 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 +++++ Source/NSXMLElement.m | 49 ++++++++++++++++++++++++--------- Source/NSXMLNode.m | 14 ++++++++-- Tests/base/NSXMLElement/basic.m | 43 +++++++++++++++++++++++++++-- Tests/base/NSXMLNode/basic.m | 34 +---------------------- 5 files changed, 96 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index decaa7cf9..f03a14d27 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2012-03-12 Fred Kiefer + + * Source/NSXMLNode.m (-detach): Don't handle the namespace case. + * Source/NSXMLElement.m (-removeNamespaceForPrefix:): Implement. + * Tests/base/NSXMLNode/basic.m: Move tests from here ... + * Tests/base/NSXMLElement/basic.m: ... to here. + 2012-03-12 Fred Kiefer * Source/NSXMLElement.m: Correct used namespace field. diff --git a/Source/NSXMLElement.m b/Source/NSXMLElement.m index 2b4e1129c..ea0914a8c 100644 --- a/Source/NSXMLElement.m +++ b/Source/NSXMLElement.m @@ -294,11 +294,44 @@ GS_PRIVATE_INTERNAL(NSXMLElement) cur = cur->next; } } + [self _addSubNode: aNamespace]; } - (void) removeNamespaceForPrefix: (NSString*)name { - [self notImplemented: _cmd]; + if (internal->node->nsDef != NULL) + { + xmlNsPtr cur = internal->node->nsDef; + xmlNsPtr last = NULL; + const xmlChar *prefix = XMLSTRING(name); + + while (cur != NULL) + { + if (xmlStrcmp(prefix, cur->prefix) == 0) + { + if (last == NULL) + { + internal->node->nsDef = cur->next; + } + else + { + last->next = cur->next; + } + cur->next = NULL; + if (cur->_private != NULL) + { + [self _removeSubNode: (NSXMLNode *)cur->_private]; + } + else + { + xmlFreeNode(cur); + } + return; + } + last = cur; + cur = cur->next; + } + } } - (void) setNamespaces: (NSArray*)namespaces @@ -312,17 +345,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement) // internal->node->nsDef = NULL; while ((namespace = (NSXMLNode *)[en nextObject]) != nil) { - xmlNsPtr ns = (xmlNsPtr)[namespace _node]; - if (internal->node->nsDef == NULL) - { - internal->node->nsDef = ns; - cur = ns; - } - else - { - cur->next = ns; - cur = ns; - } + [self addNamespace: namespace]; } } @@ -543,7 +566,7 @@ joinTextNodes(xmlNodePtr nodeA, xmlNodePtr nodeB, NSMutableArray *nodesToDelete) subEnum = [nodesToDelete objectEnumerator]; while ((subNode = [subEnum nextObject])) { - [self _removeSubNode:subNode]; + [self _removeSubNode: subNode]; } } } diff --git a/Source/NSXMLNode.m b/Source/NSXMLNode.m index f3432aaa6..300f12593 100644 --- a/Source/NSXMLNode.m +++ b/Source/NSXMLNode.m @@ -1052,9 +1052,17 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces) { NSXMLNode *parent = [self parent]; - // separate our node from its parent and siblings - xmlUnlinkNode(node); - xmlSetTreeDoc(node, NULL); + if (node->type == XML_NAMESPACE_DECL) + { + // FIXME + } + else + { + // separate our node from its parent and siblings + xmlUnlinkNode(node); + xmlSetTreeDoc(node, NULL); + } + if (parent) { [parent _removeSubNode: self]; diff --git a/Tests/base/NSXMLElement/basic.m b/Tests/base/NSXMLElement/basic.m index c64f5a773..c8ade5cd0 100644 --- a/Tests/base/NSXMLElement/basic.m +++ b/Tests/base/NSXMLElement/basic.m @@ -7,10 +7,18 @@ int main() NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSXMLElement *node; NSXMLElement *other; + NSXMLElement *xml; + NSXMLNode *attr; + NSArray *instances; node = [[NSXMLElement alloc] initWithName: @"node"]; + xml = [[NSXMLElement alloc] initWithXMLString: @"" + error: NULL]; + other = [NSXMLNode elementWithName: @"other" children: nil attributes: nil]; + instances = [NSArray arrayWithObjects: node, other, xml, nil]; test_alloc(@"NSXMLElement"); - test_NSObject(@"NSXMLElement", [NSArray arrayWithObject: node]); + test_NSObject(@"NSXMLElement", instances); + test_NSCopying(@"NSXMLElement", @"NSXMLElement", instances, NO, YES); other = [[NSXMLElement alloc] initWithName: @"other"]; PASS(NO == [other isEqual: node], "differently named elements are not equal"); @@ -21,7 +29,7 @@ int main() [other release]; - PASS(NSXMLElementKind == [node kind], "invalid node kind is correct"); + PASS(NSXMLElementKind == [node kind], "element node kind is correct"); PASS(0 == [node level], "element node level is zero"); PASS_EQUAL([node URI], nil, "element node URI is nil"); PASS_EQUAL([node objectValue], @"", "element node object value is empty"); @@ -46,6 +54,37 @@ int main() [node release]; + // Equality tests. + node = [[NSXMLNode alloc] initWithKind: NSXMLElementKind]; + other = [[NSXMLNode alloc] initWithKind: NSXMLElementKind]; + [other setName: @"test"]; + [node setName: @"test"]; + PASS([node isEqual: other], + "Elements with the same name are equal"); + + attr = [NSXMLNode attributeWithName: @"key" + stringValue: @"value"]; + [node addAttribute:attr]; + PASS(![node isEqual: other], + "Elements with different attributes are NOT equal"); + + attr = [NSXMLNode attributeWithName: @"key" + stringValue: @"value"]; + [other addAttribute:attr]; + PASS([node isEqual: other], + "Elements with the same attributes are equal"); + + [other setStringValue: @"value"]; + PASS(![node isEqual: other], + "Elements with different values are NOT equal"); + + [node setStringValue: @"value"]; + PASS([node isEqual: other], + "Elements with same values are equal"); + + [node release]; + [other release]; + [arp release]; arp = nil; diff --git a/Tests/base/NSXMLNode/basic.m b/Tests/base/NSXMLNode/basic.m index bb51f1edb..935f3ae3a 100644 --- a/Tests/base/NSXMLNode/basic.m +++ b/Tests/base/NSXMLNode/basic.m @@ -103,6 +103,7 @@ int main() PASS_EQUAL([attr stringValue], @"value", "string value on attr node works"); // In libxml2 the value is on a child node //PASS_EQUAL([attr children], nil, "attr node children is nil"); + //PASS([attr childCount] == 0, "No child on attr node"); [attr setName: @"name"]; PASS_EQUAL([attr name], @"name", @@ -110,39 +111,6 @@ int main() [attr setStringValue: @"aString"]; PASS_EQUAL([attr stringValue], @"aString", "setting string value on attr node works"); - // In libxml2 the value is on a child node - //PASS([attr childCount] == 0, "No child on attr node"); - - // Equality tests. - node = [[NSXMLNode alloc] initWithKind: NSXMLElementKind]; - other = [[NSXMLNode alloc] initWithKind: NSXMLElementKind]; - [other setName: @"test"]; - [node setName: @"test"]; - PASS([node isEqual: other], - "Nodes with the same name are equal"); - - attr = [NSXMLNode attributeWithName: @"key" - stringValue: @"value"]; - [node addAttribute:attr]; - PASS(![node isEqual: other], - "Nodes with different attributes are NOT equal"); - - attr = [NSXMLNode attributeWithName: @"key" - stringValue: @"value"]; - [other addAttribute:attr]; - PASS([node isEqual: other], - "Nodes with the same attributes are equal"); - - [other setStringValue: @"value"]; - PASS(![node isEqual: other], - "Nodes with different values are NOT equal"); - - [node setStringValue: @"value"]; - PASS([node isEqual: other], - "Nodes with different values are equal"); - - [node release]; - [other release]; [arp release]; arp = nil;