-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxast.js
106 lines (94 loc) · 2.95 KB
/
xast.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
import { selectAll, is } from 'css-select';
import xastAdaptor from './svgo/css-select-adapter.js';
/**
* @typedef {import('./types.js').XastRoot} XastRoot
* @typedef {import('./types.js').XastNode} XastNode
* @typedef {import('./types.js').XastChild} XastChild
* @typedef {import('./types.js').XastParent} XastParent
* @typedef {import('./types.js').XastElement} XastElement
* @typedef {import('./types.js').ParentList} ParentList
* @typedef {import('./types.js').Visitor} Visitor
*/
const cssSelectOptions = {
xmlMode: true,
adapter: xastAdaptor,
};
/**
* @type {(node: XastNode, selector: string) => XastChild[]}
* @deprecated
*/
export const querySelectorAll = (node, selector) => {
return selectAll(selector, node, cssSelectOptions);
};
/**
* @type {(node: XastChild, selector: string) => boolean}
* @deprecated
*/
export const matches = (node, selector) => {
return is(node, selector, cssSelectOptions);
};
export const visitSkip = Symbol();
/**
* @type {(node: XastRoot, visitor: Visitor) => void}
*/
export const visit = (node, visitor) => {
const callbacks = visitor.root;
if (callbacks && callbacks.enter) {
const symbol = callbacks.enter(node);
if (symbol === visitSkip) {
return;
}
}
// visit element children if still attached to parent
if (node.type === 'root') {
for (const child of node.children) {
visitChild(child, visitor, [{ element: node }]);
}
}
if (callbacks && callbacks.exit) {
callbacks.exit(node);
}
};
/**
* @type {(node: XastChild, visitor: Visitor, parents: ParentList) => void}
*/
const visitChild = (node, visitor, parents) => {
const callbacks = visitor[node.type];
const parentNode = parents[parents.length - 1].element;
if (callbacks && callbacks.enter) {
// @ts-ignore
const symbol = callbacks.enter(node, parentNode, parents);
// If node was detached, node.parentNode is no longer defined.
if (symbol === visitSkip || !node.parentNode) {
return;
}
}
// visit element children if still attached to parent
if (node.type === 'element') {
parents.push({ element: node });
// If node.parentNode was deleted by detachNodeFromParent, don't visit its children.
for (const child of node.children) {
visitChild(child, visitor, parents);
}
parents.pop();
}
if (callbacks && callbacks.exit) {
// @ts-ignore
callbacks.exit(node, parentNode, parents);
}
};
/**
* @param {XastChild} node
* @param {XastParent} [parentNode]
* @deprecated
*/
// Disable no-unused-vars until all calls to detachNodeFromParent() are updated.
// eslint-disable-next-line no-unused-vars
export const detachNodeFromParent = (node, parentNode) => {
// avoid splice to not break for loops
node.parentNode.children = node.parentNode.children.filter(
(child) => child !== node,
);
// @ts-ignore - plugins should always have parentNode defined; undefine parentNode here so visit() can avoid visiting its children.
delete node.parentNode;
};