Add Node methods to prepend nodes

This commit is contained in:
iska
2015-05-02 21:08:21 +02:00
parent 3d3e4d52b4
commit 2c9f68e2ea
4 changed files with 84 additions and 25 deletions
+5 -1
View File
@@ -67,12 +67,16 @@ typedef NS_ENUM(short, HTMLNodeType)
- (NSUInteger)indexOfChildNode:(HTMLNode *)node;
- (HTMLNode *)insertNode:(HTMLNode *)node beforeChildNode:(HTMLNode *)child;
- (HTMLNode *)prependNode:(HTMLNode *)node;
- (void)prependNodes:(NSArray *)nodes;
- (HTMLNode *)appendNode:(HTMLNode *)node;
- (void)appendNodes:(NSArray *)nodes;
- (HTMLNode *)insertNode:(HTMLNode *)node beforeChildNode:(HTMLNode *)child;
- (HTMLNode *)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)replacement;
- (void)replaceAllChildNodesWithNode:(HTMLNode *)node;
+12
View File
@@ -139,6 +139,18 @@
return [self.childNodes indexOfObject:node];
}
- (HTMLNode *)prependNode:(HTMLNode *)node
{
return [self insertNode:node beforeChildNode:self.firstChiledNode];
}
- (void)prependNodes:(NSArray *)nodes
{
for (id node in nodes.reverseObjectEnumerator) {
[self insertNode:node beforeChildNode:self.firstChiledNode];
}
}
- (HTMLNode *)appendNode:(HTMLNode *)node
{
return [self insertNode:node beforeChildNode:nil];
@@ -18,30 +18,6 @@ extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
@implementation HTMLKitMutationAlgorithmsTests
- (void)testAppendDocumentFragment
{
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
HTMLComment *comment = [HTMLComment new];
[element appendNode:comment];
HTMLDocumentFragment *fragment = [HTMLDocumentFragment new];
HTMLElement *firstChild = [HTMLElement new];
HTMLElement *secondChild = [HTMLElement new];
[fragment appendNode:firstChild];
[fragment appendNode:secondChild];
[element appendNode:fragment];
XCTAssertEqual(element.childNodesCount, 3);
XCTAssertEqual(fragment.childNodesCount, 0);
XCTAssertEqualObjects(firstChild.parentNode, element);
XCTAssertEqualObjects(secondChild.parentNode, element);
XCTAssertEqualObjects(element.firstChiledNode, comment);
XCTAssertEqualObjects(element.firstChiledNode.nextSibling, firstChild);
XCTAssertEqualObjects(element.lastChildNode.previousSibling, firstChild);
XCTAssertEqualObjects(element.lastChildNode, secondChild);
}
- (void)testValidParentNodeWhenAppending
{
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
+67
View File
@@ -44,6 +44,73 @@
XCTAssertNil(node.lastChildNode);
}
- (void)testAppendNode
{
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
HTMLComment *comment = [HTMLComment new];
[element appendNode:comment];
XCTAssertEqual(element.childNodesCount, 1);
XCTAssertEqual(element.firstChiledNode, comment);
HTMLElement *firstElement = [HTMLElement new];
HTMLElement *secondElement = [HTMLElement new];
NSArray *nodes = @[firstElement, secondElement];
[element appendNodes:nodes];
XCTAssertEqual(element.childNodesCount, 3);
XCTAssertEqual(element.firstChiledNode, comment);
XCTAssertEqual(element.lastChildNode, secondElement);
}
- (void)testPrependNode
{
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
HTMLText *text = [HTMLText new];
[element appendNode:text];
HTMLComment *comment = [HTMLComment new];
[element prependNode:comment];
XCTAssertEqual(element.childNodesCount, 2);
XCTAssertEqual(element.firstChiledNode, comment);
HTMLElement *firstElement = [HTMLElement new];
HTMLElement *secondElement = [HTMLElement new];
NSArray *nodes = @[firstElement, secondElement];
[element prependNodes:nodes];
XCTAssertEqual(element.childNodesCount, 4);
XCTAssertEqual(element.firstChiledNode, firstElement);
XCTAssertEqual(element.lastChildNode, text);
}
- (void)testAppendDocumentFragment
{
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
HTMLComment *comment = [HTMLComment new];
[element appendNode:comment];
HTMLDocumentFragment *fragment = [HTMLDocumentFragment new];
HTMLElement *firstChild = [HTMLElement new];
HTMLElement *secondChild = [HTMLElement new];
[fragment appendNode:firstChild];
[fragment appendNode:secondChild];
[element appendNode:fragment];
XCTAssertEqual(element.childNodesCount, 3);
XCTAssertEqual(fragment.childNodesCount, 0);
XCTAssertEqualObjects(firstChild.parentNode, element);
XCTAssertEqualObjects(secondChild.parentNode, element);
XCTAssertEqualObjects(element.firstChiledNode, comment);
XCTAssertEqualObjects(element.firstChiledNode.nextSibling, firstChild);
XCTAssertEqualObjects(element.lastChildNode.previousSibling, firstChild);
XCTAssertEqualObjects(element.lastChildNode, secondChild);
}
- (void)testParentNode
{
HTMLNode *node = [[HTMLNode alloc] initWithName:@"name" type:HTMLNodeElement];