-
Notifications
You must be signed in to change notification settings - Fork 244
/
NodeUtils.js
213 lines (197 loc) · 4.82 KB
/
NodeUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const {Element, DataNode} = require('domhandler');
const {removeElement, append, prepend} = require('domutils');
/**
* Depth-first walk through the DOM tree.
* @param {Node} node
*/
const nextNode = function (node) {
// Walk downwards if there are children
const firstChild = node.firstChild;
if (firstChild) {
return firstChild;
}
// Return the direct sibling or walk upwards until we find a node with sibling
let tmp = node;
while (tmp) {
const next = tmp.nextSibling;
if (next) {
return next;
}
// Walk upwards
tmp = tmp.parent;
}
// We are done
return null;
};
/**
* Remove node from DOM
*
* @param {Node} node
*/
const remove = function (node) {
removeElement(node);
};
/**
* Appends a node to the given parent
*
* @param {Node} parent
* @param {Node} node
*/
const appendChild = function (elem, child) {
if (!child) {
return;
}
child.parent = elem;
if (elem.children.push(child) !== 1) {
const sibling = elem.children[elem.children.length - 2];
sibling.next = child;
child.prev = sibling;
child.next = null;
}
};
/**
* Inserts a Node before the reference node. If referenceNode is null, inserts the node as
* the first child.
*
* @param {Node} newNode the node to be inserted.
* @param {Node} referenceNode the reference node, where the new node will be added before.
*/
const insertBefore = function (parent, newNode, referenceNode) {
if (referenceNode) {
prepend(referenceNode, newNode);
return;
}
// if referenceNode.nextSibling is null, referenceNode is the last child. newNode is inserted
// as the last element.
appendChild(parent, newNode);
};
/**
* Inserts a Node after the reference node. If referenceNode is null, inserts the node as
* the first child.
*
* @param {Node} newNode the node to be inserted.
* @param {Node} referenceNode the reference node, where the new node will be added after.
*/
const insertAfter = function (parent, newNode, referenceNode) {
if (referenceNode) {
append(referenceNode, newNode);
return;
}
// if referenceNode.nextSibling is null, referenceNode is the last child. newNode is inserted
// as the last element.
appendChild(parent, newNode);
};
/**
* Appends all nodes to the given parent
*
* @param {Node} parent
* @param {Array<Node>} node
*/
const appendAll = function (node, nodes) {
if (!nodes) {
return;
}
for (let i = 0, len = nodes.length; i < len; i++) {
appendChild(node, nodes[i]);
}
};
/**
* Returns the first child with the given tag name
*
* @param {Node} node
* @param {string} tagName
*/
const firstChildByTag = function (node, tagName) {
if (!node || !node.children) {
return null;
}
return node.children.find((child) => child.tagName && child.tagName === tagName);
};
//
/**
* Returns true if an attribute with the given name exists
* @param {Node} node
* @param {string} attribute
* @return {boolean}
*/
const hasAttribute = function (node, attribute) {
if (!node.attribs) return false;
return attribute in node.attribs;
};
/**
* Sets the attribute to value.
* @param {Node} node
* @param {string} name
* @param {*} value
*/
const setAttribute = function (node, name, value) {
if (!node.attribs) node.attribs = {};
node.attribs[name] = value;
};
/**
* Move a node from one parent to another
* @param {Node} nodeToMove
* @param {Node} newParent
*/
const move = function (nodeToMove, newParent) {
remove(nodeToMove);
appendChild(newParent, nodeToMove);
};
/**
* Creates a new element
*
* @param {string} tagName
* @param {obj} [attribs={}]
* @returns {Node} new node
*/
const createElement = (tagName, attribs) => {
return new Element(tagName, attribs);
};
/**
* Inserts text
*
* @param {Node} node
* @param {string} the text
*/
const insertText = (node, text) => {
const dataNode = new DataNode('text', text);
appendChild(node, dataNode);
};
/**
* Creates a new doctype
*/
const createDocType = () => {
const result = new DataNode('directive', '!doctype html');
return result;
};
module.exports = {
appendChild,
appendAll,
insertAfter,
nextNode,
remove,
createDocType,
createElement,
insertText,
insertBefore,
hasAttribute,
setAttribute,
firstChildByTag,
move,
};