Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding insertChild:atIndex: method, along with code to test it.

  • Loading branch information...
commit 02a6523c05168b8e2b71bd970952ed71b196770f 1 parent 97c8904
@robbiehanson authored
Showing with 87 additions and 1 deletion.
  1. +1 −1  DDXMLElement.h
  2. +35 −0 DDXMLElement.m
  3. +51 −0 DDXMLTesting.m
View
2  DDXMLElement.h
@@ -38,7 +38,7 @@
#pragma mark --- Children ---
-//- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index;
+- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index;
//- (void)insertChildren:(NSArray *)children atIndex:(NSUInteger)index;
- (void)removeChildAtIndex:(NSUInteger)index;
- (void)setChildren:(NSArray *)children;
View
35 DDXMLElement.m
@@ -524,6 +524,41 @@ - (void)addChild:(DDXMLNode *)child
xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)child->genericPtr);
}
+- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index
+{
+ // NSXML version uses these same assertions
+ DDCheck([child hasParent] == NO, @"Cannot add a child that has a parent; detach or copy first");
+ DDCheck([child isXmlNodePtr], @"Elements can only have text, elements, processing instructions, and comments as children");
+
+ NSUInteger i = 0;
+
+ xmlNodePtr childNodePtr = ((xmlNodePtr)genericPtr)->children;
+ while(childNodePtr != NULL)
+ {
+ // Ignore all but element, comment, text, or processing instruction nodes
+ if([[self class] isXmlNodePtr:(xmlKindPtr)childNodePtr])
+ {
+ if(i == index)
+ {
+ xmlAddPrevSibling(childNodePtr, (xmlNodePtr)child->genericPtr);
+ return;
+ }
+
+ i++;
+ }
+ childNodePtr = childNodePtr->next;
+ }
+
+ if(i == index)
+ {
+ xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)child->genericPtr);
+ return;
+ }
+
+ // NSXML version uses this same assertion
+ DDCheck(NO, @"index (%u) beyond bounds (%u)", (unsigned)index, (unsigned)++i);
+}
+
- (void)setChildren:(NSArray *)children
{
[self removeAllChildren];
View
51 DDXMLTesting.m
@@ -27,6 +27,7 @@ + (void)testElements;
+ (void)testXPath;
+ (void)testNodesForXPath;
+ (void)testNSXMLBugs;
++ (void)testInsertChild;
@end
@implementation DDXMLTesting
@@ -58,6 +59,7 @@ + (void)performTests
[self testXPath];
[self testNodesForXPath];
[self testNSXMLBugs];
+ [self testInsertChild];
[self tearDown];
}
@@ -1432,4 +1434,53 @@ + (void)testNSXMLBugs
[pool release];
}
++ (void)testInsertChild
+{
+ NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSXMLElement *nsParent = [NSXMLElement elementWithName:@"parent"];
+ DDXMLElement *ddParent = [DDXMLElement elementWithName:@"parent"];
+
+ NSXMLElement *nsChild2 = [NSXMLElement elementWithName:@"child2"];
+ DDXMLElement *ddChild2 = [DDXMLElement elementWithName:@"child2"];
+
+ [nsParent insertChild:nsChild2 atIndex:0];
+ [ddParent insertChild:ddChild2 atIndex:0];
+
+ NSAssert([[nsParent XMLString] isEqualToString:[ddParent XMLString]], @"Failed test 1");
+
+ NSXMLElement *nsChild0 = [NSXMLElement elementWithName:@"child0"];
+ DDXMLElement *ddChild0 = [DDXMLElement elementWithName:@"child0"];
+
+ [nsParent insertChild:nsChild0 atIndex:0];
+ [ddParent insertChild:ddChild0 atIndex:0];
+
+ NSAssert([[nsParent XMLString] isEqualToString:[ddParent XMLString]], @"Failed test 2");
+
+ NSXMLElement *nsChild1 = [NSXMLElement elementWithName:@"child1"];
+ DDXMLElement *ddChild1 = [DDXMLElement elementWithName:@"child1"];
+
+ [nsParent insertChild:nsChild1 atIndex:1];
+ [ddParent insertChild:ddChild1 atIndex:1];
+
+ NSAssert([[nsParent XMLString] isEqualToString:[ddParent XMLString]], @"Failed test 3");
+
+ NSXMLElement *nsChild3 = [NSXMLElement elementWithName:@"child3"];
+ DDXMLElement *ddChild3 = [DDXMLElement elementWithName:@"child3"];
+
+ [nsParent insertChild:nsChild3 atIndex:3];
+ [ddParent insertChild:ddChild3 atIndex:3];
+
+ NSAssert([[nsParent XMLString] isEqualToString:[ddParent XMLString]], @"Failed test 4");
+
+// NSXMLElement *nsChild5 = [NSXMLElement elementWithName:@"child5"];
+// DDXMLElement *ddChild5 = [DDXMLElement elementWithName:@"child5"];
+
+// [nsParent insertChild:nsChild5 atIndex:5]; // Exception - index (5) beyond bounds (5)
+// [ddParent insertChild:ddChild5 atIndex:5]; // Exception - index (5) beyond bounds (5)
+
+ [pool release];
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.