Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 6be5b80

Browse files
committed
fix(node cursor): Do not eat nodes
1 parent bacdbe0 commit 6be5b80

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/node_cursor.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ class NodeCursor {
9595
var nodes = nodeList();
9696

9797
for (var i = 0, ii = nodes.length; i < ii; i++) {
98-
nodes[i].remove();
98+
// NOTE(deboer): If elements is a list of child nodes on a node, then
99+
// calling Node.remove() may also remove it from the list. Thus, we
100+
// call elements.removeAt first so only one node is removed.
99101
elements.removeAt(index);
102+
nodes[i].remove();
100103
}
101104

102105
return new NodeCursor(nodes);

test/node_cursor_spec.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ main() {
9696

9797
expect(STRINGIFY(childCursor.elements[0]), equals('<span>text</span>'));
9898
});
99+
100+
it('should preserve the top-level elements', () {
101+
var dom = $('<span>text</span>MoreText<div>other</div>');
102+
var parentCursor = new NodeCursor(dom);
103+
104+
var childCursor = parentCursor.replaceWithAnchor('child');
105+
expect(STRINGIFY(dom), equals('[<!--ANCHOR: child-->, MoreText, <div>other</div>]'));
106+
107+
expect(STRINGIFY(childCursor.elements[0]), equals('<span>text</span>'));
108+
});
99109
});
100110
}
101111

0 commit comments

Comments
 (0)