diff --git a/HTMLKit/HTMLNode.h b/HTMLKit/HTMLNode.h
index b07c56f..332fb4f 100644
--- a/HTMLKit/HTMLNode.h
+++ b/HTMLKit/HTMLNode.h
@@ -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;
diff --git a/HTMLKit/HTMLNode.m b/HTMLKit/HTMLNode.m
index 6aac8c2..470cc15 100644
--- a/HTMLKit/HTMLNode.m
+++ b/HTMLKit/HTMLNode.m
@@ -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];
diff --git a/HTMLKitTests/HTMLKitMutationAlgorithmsTests.m b/HTMLKitTests/HTMLKitMutationAlgorithmsTests.m
index d4d37f6..990d574 100644
--- a/HTMLKitTests/HTMLKitMutationAlgorithmsTests.m
+++ b/HTMLKitTests/HTMLKitMutationAlgorithmsTests.m
@@ -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"];
diff --git a/HTMLKitTests/HTMLKitNodesTests.m b/HTMLKitTests/HTMLKitNodesTests.m
index 56a5d6a..e2b65cc 100644
--- a/HTMLKitTests/HTMLKitNodesTests.m
+++ b/HTMLKitTests/HTMLKitNodesTests.m
@@ -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];