Skip to content

Commit

Permalink
dom: nodeIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed May 20, 2013
1 parent 96f7222 commit a4b1ab1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 14 additions & 2 deletions summernote.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
var commonAncestor = function(nodeA, nodeB) {
var aAncestor = listAncestor(nodeA);
for (var n = nodeB; n; n = n.parentNode) {
//TODO: IE8 there is no indexOf
//TODO: IE8, indexOf
if (aAncestor.indexOf(n) != -1) { return n; }
}

Expand All @@ -90,8 +90,20 @@
* listing all Nodes between nodeA and nodeB
*/
var listBetween = function(nodeA, nodeB) {
//FIXME: must nodeA and nodeB be sorted, use comparePoints later.
var aNode = [];
var elAncestor = commonAncestor(nodeA, nodeB);
return [nodeA];
//TODO: IE8, createNodeIterator
var iterator = document.createNodeIterator(elAncestor,
NodeFilter.SHOW_ALL, null,
false);
var node, bStart = false;
while (node = iterator.nextNode()) {
if (nodeA === node) { bStart = true; }
if (bStart) { aNode.push(node); }
if (nodeB === node) { break; }
}
return aNode;
};

return {
Expand Down
6 changes: 5 additions & 1 deletion test/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ test('dom.listBetween', function() {
$s = $cont.find('s'), $i = $cont.find('i');

deepEqual(dom.listBetween($b[0], $b[0]), [$b[0]], 'same elements');
//deepEqual(dom.listBetween($b[0], $u[0]), [$b[0], $u[0]], 'nextSibling');
deepEqual(dom.listBetween($b[0], $u[0]), [$b[0], $b[0].firstChild, $u[0]], 'adjacent');
deepEqual(dom.listBetween($b[0], $u[0]), [$b[0], $b[0].firstChild, $u[0]], 'distance 2');
deepEqual(dom.listBetween($b[0], $s[0]), [$b[0], $b[0].firstChild,
$u[0], $u[0].firstChild,
$s[0]], 'distance 2');
});

0 comments on commit a4b1ab1

Please sign in to comment.