Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor removeEditorsNSData #1557

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -23,7 +23,6 @@
"plugins/plugins.js",
"plugins/prefixIds.js",
"plugins/removeDimensions.js",
"plugins/removeEditorsNSData.js",
"plugins/removeEmptyAttrs.js",
"plugins/removeNonInheritableGroupAttrs.js",
"plugins/removeUnusedNS.js",
Expand Down