1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/svg-baker/lib/transformations/extract-namespaces-to-root.js

36 lines
745 B
JavaScript
Raw Normal View History

2023-12-18 13:12:25 +08:00
const { getRoot } = require('../utils');
/**
* @return {Function} PostHTML plugin
*/
function extractNamespacesToRoot() {
return (tree) => {
const namespaces = {};
tree.match({ tag: /.*/ }, (node) => {
const attrs = node.attrs || {};
Object.keys(attrs).forEach((attr) => {
if (attr.startsWith('xmlns')) {
if (attr in namespaces === false) {
namespaces[attr] = attrs[attr];
}
delete node.attrs[attr];
}
});
return node;
});
const root = getRoot(tree);
root.attrs = root.attrs || {};
Object.keys(namespaces).forEach(name => root.attrs[name] = namespaces[name]);
return tree;
};
}
module.exports = extractNamespacesToRoot;