1 | "use strict";
|
2 |
|
3 | var NodeTraversal = module.exports = {
|
4 | nextSkippingChildren: nextSkippingChildren,
|
5 | nextAncestorSibling: nextAncestorSibling,
|
6 | next: next,
|
7 | previous: previous,
|
8 | deepLastChild: deepLastChild
|
9 | };
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function nextSkippingChildren(node, stayWithin) {
|
16 | if (node === stayWithin) {
|
17 | return null;
|
18 | }
|
19 | if (node.nextSibling !== null) {
|
20 | return node.nextSibling;
|
21 | }
|
22 | return nextAncestorSibling(node, stayWithin);
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function nextAncestorSibling(node, stayWithin) {
|
30 | for (node = node.parentNode; node !== null; node = node.parentNode) {
|
31 | if (node === stayWithin) {
|
32 | return null;
|
33 | }
|
34 | if (node.nextSibling !== null) {
|
35 | return node.nextSibling;
|
36 | }
|
37 | }
|
38 | return null;
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function next(node, stayWithin) {
|
46 | var n;
|
47 | n = node.firstChild;
|
48 | if (n !== null) {
|
49 | return n;
|
50 | }
|
51 | if (node === stayWithin) {
|
52 | return null;
|
53 | }
|
54 | n = node.nextSibling;
|
55 | if (n !== null) {
|
56 | return n;
|
57 | }
|
58 | return nextAncestorSibling(node, stayWithin);
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function deepLastChild(node) {
|
66 | while (node.lastChild) {
|
67 | node = node.lastChild;
|
68 | }
|
69 | return node;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function previous(node, stayWithin) {
|
77 | var p;
|
78 | p = node.previousSibling;
|
79 | if (p !== null) {
|
80 | return deepLastChild(p);
|
81 | }
|
82 | p = node.parentNode;
|
83 | if (p === stayWithin) {
|
84 | return null;
|
85 | }
|
86 | return p;
|
87 | }
|