Skip to content

Commit

Permalink
Adding insertChild:atIndex: method, along with code to test it.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiehanson committed Jan 22, 2009
1 parent 97c8904 commit 02a6523
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DDXMLElement.h
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions DDXMLElement.m
Expand Up @@ -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];
Expand Down
51 changes: 51 additions & 0 deletions DDXMLTesting.m
Expand Up @@ -27,6 +27,7 @@ + (void)testElements;
+ (void)testXPath;
+ (void)testNodesForXPath;
+ (void)testNSXMLBugs;
+ (void)testInsertChild;
@end

@implementation DDXMLTesting
Expand Down Expand Up @@ -58,6 +59,7 @@ + (void)performTests
[self testXPath];
[self testNodesForXPath];
[self testNSXMLBugs];
[self testInsertChild];

[self tearDown];
}
Expand Down Expand Up @@ -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

0 comments on commit 02a6523

Please sign in to comment.