mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-07 12:33:47 +08:00
DISABLE_THIRD_PARTY_CHECK=NPM fix R=jacktfranklin@chromium.org Bug: none Change-Id: I41baa3294f88b7e4463fdc580c050932c5abe40e Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3372932 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Tim Van der Lippe <tvanderlippe@chromium.org>
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
exports.type = 'visitor';
|
|
exports.name = 'removeUnusedNS';
|
|
exports.active = true;
|
|
exports.description = 'removes unused namespaces declaration';
|
|
|
|
/**
|
|
* Remove unused namespaces declaration from svg element
|
|
* which are not used in elements or attributes
|
|
*
|
|
* @author Kir Belevich
|
|
*
|
|
* @type {import('../lib/types').Plugin<void>}
|
|
*/
|
|
exports.fn = () => {
|
|
/**
|
|
* @type {Set<string>}
|
|
*/
|
|
const unusedNamespaces = new Set();
|
|
return {
|
|
element: {
|
|
enter: (node, parentNode) => {
|
|
// collect all namespaces from svg element
|
|
// (such as xmlns:xlink="http://www.w3.org/1999/xlink")
|
|
if (node.name === 'svg' && parentNode.type === 'root') {
|
|
for (const name of Object.keys(node.attributes)) {
|
|
if (name.startsWith('xmlns:')) {
|
|
const local = name.slice('xmlns:'.length);
|
|
unusedNamespaces.add(local);
|
|
}
|
|
}
|
|
}
|
|
if (unusedNamespaces.size !== 0) {
|
|
// preserve namespace used in nested elements names
|
|
if (node.name.includes(':')) {
|
|
const [ns] = node.name.split(':');
|
|
if (unusedNamespaces.has(ns)) {
|
|
unusedNamespaces.delete(ns);
|
|
}
|
|
}
|
|
// preserve namespace used in nested elements attributes
|
|
for (const name of Object.keys(node.attributes)) {
|
|
if (name.includes(':')) {
|
|
const [ns] = name.split(':');
|
|
unusedNamespaces.delete(ns);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
exit: (node, parentNode) => {
|
|
// remove unused namespace attributes from svg element
|
|
if (node.name === 'svg' && parentNode.type === 'root') {
|
|
for (const name of unusedNamespaces) {
|
|
delete node.attributes[`xmlns:${name}`];
|
|
}
|
|
}
|
|
},
|
|
},
|
|
};
|
|
};
|