diff --git a/plugins/plugins.js b/plugins/plugins.js index 409e07b11..4d0be8fcc 100644 --- a/plugins/plugins.js +++ b/plugins/plugins.js @@ -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'); diff --git a/plugins/remove-xlink-ns.js b/plugins/remove-xlink-ns.js new file mode 100644 index 000000000..2bce38e2d --- /dev/null +++ b/plugins/remove-xlink-ns.js @@ -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 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} + */ +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']; + } + }, + }, + }; +}; diff --git a/test/plugins/remove-xlink-ns.01.svg b/test/plugins/remove-xlink-ns.01.svg new file mode 100644 index 000000000..94d262bc5 --- /dev/null +++ b/test/plugins/remove-xlink-ns.01.svg @@ -0,0 +1,26 @@ +Remove xmlns:xlink and replace xlink:href with href attribute + +=== + + + + + + + + + + + +@@@ + + + + + + + + + + +