source: trip-planner-front/node_modules/svgo/lib/xast.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[6a3a178]1'use strict';
2
3/**
4 * @typedef {import('./types').XastNode} XastNode
5 * @typedef {import('./types').XastChild} XastChild
6 * @typedef {import('./types').XastParent} XastParent
7 * @typedef {import('./types').Visitor} Visitor
8 */
9
10const { selectAll, selectOne, is } = require('css-select');
11const xastAdaptor = require('./svgo/css-select-adapter.js');
12
13const cssSelectOptions = {
14 xmlMode: true,
15 adapter: xastAdaptor,
16};
17
18/**
19 * @type {(node: XastNode, selector: string) => Array<XastChild>}
20 */
21const querySelectorAll = (node, selector) => {
22 return selectAll(selector, node, cssSelectOptions);
23};
24exports.querySelectorAll = querySelectorAll;
25
26/**
27 * @type {(node: XastNode, selector: string) => null | XastChild}
28 */
29const querySelector = (node, selector) => {
30 return selectOne(selector, node, cssSelectOptions);
31};
32exports.querySelector = querySelector;
33
34/**
35 * @type {(node: XastChild, selector: string) => boolean}
36 */
37const matches = (node, selector) => {
38 return is(node, selector, cssSelectOptions);
39};
40exports.matches = matches;
41
42/**
43 * @type {(node: XastChild, name: string) => null | XastChild}
44 */
45const closestByName = (node, name) => {
46 let currentNode = node;
47 while (currentNode) {
48 if (currentNode.type === 'element' && currentNode.name === name) {
49 return currentNode;
50 }
51 // @ts-ignore parentNode is hidden from public usage
52 currentNode = currentNode.parentNode;
53 }
54 return null;
55};
56exports.closestByName = closestByName;
57
58const visitSkip = Symbol();
59exports.visitSkip = visitSkip;
60
61/**
62 * @type {(node: XastNode, visitor: Visitor, parentNode?: any) => void}
63 */
64const visit = (node, visitor, parentNode) => {
65 const callbacks = visitor[node.type];
66 if (callbacks && callbacks.enter) {
67 // @ts-ignore hard to infer
68 const symbol = callbacks.enter(node, parentNode);
69 if (symbol === visitSkip) {
70 return;
71 }
72 }
73 // visit root children
74 if (node.type === 'root') {
75 // copy children array to not loose cursor when children is spliced
76 for (const child of node.children) {
77 visit(child, visitor, node);
78 }
79 }
80 // visit element children if still attached to parent
81 if (node.type === 'element') {
82 if (parentNode.children.includes(node)) {
83 for (const child of node.children) {
84 visit(child, visitor, node);
85 }
86 }
87 }
88 if (callbacks && callbacks.exit) {
89 // @ts-ignore hard to infer
90 callbacks.exit(node, parentNode);
91 }
92};
93exports.visit = visit;
94
95/**
96 * @type {(node: XastChild, parentNode: XastParent) => void}
97 */
98const detachNodeFromParent = (node, parentNode) => {
99 // avoid splice to not break for loops
100 parentNode.children = parentNode.children.filter((child) => child !== node);
101};
102exports.detachNodeFromParent = detachNodeFromParent;
Note: See TracBrowser for help on using the repository browser.