Skip to content

Commit

Permalink
Refactor removeEditorsNSData (svg#1557)
Browse files Browse the repository at this point in the history
- covered with types
- migrated to visitor plugin api
- got rid from parseName utility in favour of simple string operations
  • Loading branch information
TrySound authored Sep 10, 2021
1 parent 298820b commit f587aae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
89 changes: 45 additions & 44 deletions plugins/removeEditorsNSData.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
'use strict';

const { parseName } = require('../lib/svgo/tools.js');
const { editorNamespaces } = require('./_collections');
const { detachNodeFromParent } = require('../lib/xast.js');
const { editorNamespaces } = require('./_collections.js');

exports.type = 'visitor';
exports.name = 'removeEditorsNSData';

exports.type = 'perItem';

exports.active = true;

exports.description = 'removes editors namespaces, elements and attributes';

const prefixes = [];

exports.params = {
additionalNamespaces: [],
};

/**
* Remove editors namespaces, elements and attributes.
*
Expand All @@ -25,43 +16,53 @@ exports.params = {
* <sodipodi:namedview/>
* <path sodipodi:nodetypes="cccc"/>
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*
* @type {import('../lib/types').Plugin<{
* additionalNamespaces?: Array<string>
* }>}
*/
exports.fn = function (item, params) {
exports.fn = (_root, params) => {
let namespaces = editorNamespaces;
if (Array.isArray(params.additionalNamespaces)) {
namespaces = [...editorNamespaces, ...params.additionalNamespaces];
}

if (item.type === 'element') {
if (item.isElem('svg')) {
for (const [name, value] of Object.entries(item.attributes)) {
const { prefix, local } = parseName(name);
if (prefix === 'xmlns' && namespaces.includes(value)) {
prefixes.push(local);

// <svg xmlns:sodipodi="">
delete item.attributes[name];
/**
* @type {Array<string>}
*/
const prefixes = [];
return {
element: {
enter: (node, parentNode) => {
// collect namespace aliases from svg element
if (node.name === 'svg') {
for (const [name, value] of Object.entries(node.attributes)) {
if (name.startsWith('xmlns:') && namespaces.includes(value)) {
prefixes.push(name.slice('xmlns:'.length));
// <svg xmlns:sodipodi="">
delete node.attributes[name];
}
}
}
}
}

// <* sodipodi:*="">
for (const name of Object.keys(item.attributes)) {
const { prefix } = parseName(name);
if (prefixes.includes(prefix)) {
delete item.attributes[name];
}
}

// <sodipodi:*>
const { prefix } = parseName(item.name);
if (prefixes.includes(prefix)) {
return false;
}
}
// remove editor attributes, for example
// <* sodipodi:*="">
for (const name of Object.keys(node.attributes)) {
if (name.includes(':')) {
const [prefix] = name.split(':');
if (prefixes.includes(prefix)) {
delete node.attributes[name];
}
}
}
// remove editor elements, for example
// <sodipodi:*>
if (node.name.includes(':')) {
const [prefix] = node.name.split(':');
if (prefixes.includes(prefix)) {
detachNodeFromParent(node, parentNode);
}
}
},
},
};
};
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"plugins/plugins.js",
"plugins/prefixIds.js",
"plugins/removeDimensions.js",
"plugins/removeEditorsNSData.js",
"plugins/removeEmptyAttrs.js",
"plugins/removeNonInheritableGroupAttrs.js",
"plugins/removeUnusedNS.js",
Expand Down

0 comments on commit f587aae

Please sign in to comment.