1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const { selectAll, selectOne, is } = require('css-select');
|
11 | const xastAdaptor = require('./svgo/css-select-adapter.js');
|
12 |
|
13 | const cssSelectOptions = {
|
14 | xmlMode: true,
|
15 | adapter: xastAdaptor,
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const querySelectorAll = (node, selector) => {
|
22 | return selectAll(selector, node, cssSelectOptions);
|
23 | };
|
24 | exports.querySelectorAll = querySelectorAll;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const querySelector = (node, selector) => {
|
30 | return selectOne(selector, node, cssSelectOptions);
|
31 | };
|
32 | exports.querySelector = querySelector;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | const matches = (node, selector) => {
|
38 | return is(node, selector, cssSelectOptions);
|
39 | };
|
40 | exports.matches = matches;
|
41 |
|
42 | const visitSkip = Symbol();
|
43 | exports.visitSkip = visitSkip;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const visit = (node, visitor, parentNode) => {
|
49 | const callbacks = visitor[node.type];
|
50 | if (callbacks && callbacks.enter) {
|
51 |
|
52 | const symbol = callbacks.enter(node, parentNode);
|
53 | if (symbol === visitSkip) {
|
54 | return;
|
55 | }
|
56 | }
|
57 |
|
58 | if (node.type === 'root') {
|
59 |
|
60 | for (const child of node.children) {
|
61 | visit(child, visitor, node);
|
62 | }
|
63 | }
|
64 |
|
65 | if (node.type === 'element') {
|
66 | if (parentNode.children.includes(node)) {
|
67 | for (const child of node.children) {
|
68 | visit(child, visitor, node);
|
69 | }
|
70 | }
|
71 | }
|
72 | if (callbacks && callbacks.exit) {
|
73 |
|
74 | callbacks.exit(node, parentNode);
|
75 | }
|
76 | };
|
77 | exports.visit = visit;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | const detachNodeFromParent = (node, parentNode) => {
|
84 |
|
85 | parentNode.children = parentNode.children.filter((child) => child !== node);
|
86 | };
|
87 | exports.detachNodeFromParent = detachNodeFromParent;
|