mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-11-01 18:59:10 +08:00
These packages will be used to dynamically optimize SVG images during the build. R=jacktfranklin@chromium.org Bug: 1216402 Change-Id: I04e95aa7d79c9d67beaf8a7861182c52b16b7d0f Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2939992 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
109 lines
2.2 KiB
JavaScript
109 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const { expect } = require('chai');
|
|
const { visit, detachNodeFromParent } = require('./xast.js');
|
|
|
|
const getAst = () => {
|
|
const ast = {
|
|
type: 'root',
|
|
children: [
|
|
{
|
|
type: 'element',
|
|
name: 'g',
|
|
attributes: {},
|
|
children: [
|
|
{
|
|
type: 'element',
|
|
name: 'rect',
|
|
attributes: {},
|
|
children: [],
|
|
},
|
|
{
|
|
type: 'element',
|
|
name: 'circle',
|
|
attributes: {},
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: 'element',
|
|
name: 'ellipse',
|
|
attributes: {},
|
|
children: [],
|
|
},
|
|
],
|
|
};
|
|
ast.children[0].parentNode = ast;
|
|
ast.children[0].children[0].parentNode = ast.children[0];
|
|
ast.children[0].children[1].parentNode = ast.children[0];
|
|
ast.children[1].parentNode = ast;
|
|
return ast;
|
|
};
|
|
|
|
describe('xast', () => {
|
|
it('enter into nodes', () => {
|
|
const root = getAst();
|
|
const entered = [];
|
|
visit(root, {
|
|
root: {
|
|
enter: (node) => {
|
|
entered.push(node.type);
|
|
},
|
|
},
|
|
element: {
|
|
enter: (node) => {
|
|
entered.push(`${node.type}:${node.name}`);
|
|
},
|
|
},
|
|
});
|
|
expect(entered).to.deep.equal([
|
|
'root',
|
|
'element:g',
|
|
'element:rect',
|
|
'element:circle',
|
|
'element:ellipse',
|
|
]);
|
|
});
|
|
|
|
it('exit from nodes', () => {
|
|
const root = getAst();
|
|
const exited = [];
|
|
visit(root, {
|
|
root: {
|
|
exit: (node) => {
|
|
exited.push(node.type);
|
|
},
|
|
},
|
|
element: {
|
|
exit: (node) => {
|
|
exited.push(`${node.type}:${node.name}`);
|
|
},
|
|
},
|
|
});
|
|
expect(exited).to.deep.equal([
|
|
'element:rect',
|
|
'element:circle',
|
|
'element:g',
|
|
'element:ellipse',
|
|
'root',
|
|
]);
|
|
});
|
|
|
|
it('skip entering children if node is detached', () => {
|
|
const root = getAst();
|
|
const entered = [];
|
|
visit(root, {
|
|
element: {
|
|
enter: (node) => {
|
|
entered.push(node.name);
|
|
if (node.name === 'g') {
|
|
detachNodeFromParent(node);
|
|
}
|
|
},
|
|
},
|
|
});
|
|
expect(entered).to.deep.equal(['g', 'ellipse']);
|
|
});
|
|
});
|