From 5a0bfb3ee3dc88d9ca99fe6572cea0079dc86f69 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sun, 22 Aug 2021 13:11:03 +0300 Subject: [PATCH] [new plugin] remove-xlink-ns Ref https://github.com/svg/svgo/pull/1508 The new plugin 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. This plugin will be enabled by default in SVGO 3. See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/href --- plugins/plugins.js | 1 + plugins/remove-xlink-ns.js | 33 +++++++++++++++++++++++++++++ test/plugins/remove-xlink-ns.01.svg | 26 +++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 plugins/remove-xlink-ns.js create mode 100644 test/plugins/remove-xlink-ns.01.svg 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 + +=== + + + + + + + + + + + +@@@ + + + + + + + + + + +