Skip to content

Commit

Permalink
[new plugin] remove-xlink-ns
Browse files Browse the repository at this point in the history
Ref #1508

The new plugin removes xmlns:xlink from <svg> and replaces xlink:href
with href attribute.

xlink namespace is obsolete in SVG 2. Href attribute is recommended
replacement to xlink:href.

This plugin will be enabled by default in SVGO 3.

See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/href
  • Loading branch information
TrySound committed Aug 22, 2021
1 parent 98c023b commit 5a0bfb3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
exports['preset-default'] = require('./preset-default.js');

// builtin plugins
exports['remove-xlink-ns'] = require('./remove-xlink-ns.js');
exports.addAttributesToSVGElement = require('./addAttributesToSVGElement.js');
exports.addClassesToSVGElement = require('./addClassesToSVGElement.js');
exports.cleanupAttrs = require('./cleanupAttrs.js');
Expand Down
33 changes: 33 additions & 0 deletions plugins/remove-xlink-ns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

exports.name = 'remove-xlink-ns';
exports.type = 'visitor';
exports.active = false;
exports.description =
'removes xmlns:xlink and replaces xlink:href with href attribute';

/**
* removes xmlns:xlink from <svg> and replaces xlink:href with href attribute
*
* xlink namespace is obsolete in SVG 2. Href attribute is recommended
* replacement to xlink:href.
*
* https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/href
*
* @type {import('../lib/types').Plugin<void>}
*/
exports.fn = () => {
return {
element: {
enter: (node) => {
if (node.attributes['xmlns:xlink'] != null) {
delete node.attributes['xmlns:xlink'];
}
if (node.attributes['xlink:href'] != null) {
node.attributes['href'] = node.attributes['xlink:href'];
delete node.attributes['xlink:href'];
}
},
},
};
};
26 changes: 26 additions & 0 deletions test/plugins/remove-xlink-ns.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5a0bfb3

Please sign in to comment.