From eceb95c3105b3b31eb54d00c5e82adf624d2a4f8 Mon Sep 17 00:00:00 2001 From: Keegan Street Date: Wed, 25 Oct 2017 11:20:29 +1100 Subject: [PATCH] Updated addElementsToSVGElement plugin to support attribute values, as well as attribute names. --- plugins/addAttributesToSVGElement.js | 41 +++++++++++++++---- test/plugins/addAttributesToSVGElement.03.svg | 13 ++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 test/plugins/addAttributesToSVGElement.03.svg diff --git a/plugins/addAttributesToSVGElement.js b/plugins/addAttributesToSVGElement.js index f17b8dc4..da2448b1 100644 --- a/plugins/addAttributesToSVGElement.js +++ b/plugins/addAttributesToSVGElement.js @@ -7,7 +7,7 @@ exports.active = false; exports.description = 'adds attributes to an outer element'; var ENOCLS = `Error in plugin "addAttributesToSVGElement": absent parameters. -It should have a list of classes in "attributes" or one "attribute". +It should have a list of "attributes" or one "attribute". Config example: plugins: @@ -16,7 +16,13 @@ plugins: plugins: - addAttributesToSVGElement: - attributes: ["mySvg", "size-big"]`; + attributes: ["mySvg", "size-big"] + +plugins: +- addAttributesToSVGElement: + attributes: + - focusable: false + - data-image: icon`; /** * Add attributes to an outer element. Example config: @@ -29,10 +35,16 @@ plugins: * - addAttributesToSVGElement: * attributes: ['data-icon', 'data-disabled'] * + * plugins: + * - addAttributesToSVGElement: + * attributes: + * - focusable: false + * - data-image: icon + * * @author April Arcus */ exports.fn = function(data, params) { - if (!params || !(Array.isArray(params.attributes) && params.attributes.some(String) || params.attribute)) { + if (!params || !(Array.isArray(params.attributes) || params.attribute)) { console.error(ENOCLS); return data; } @@ -42,11 +54,24 @@ exports.fn = function(data, params) { if (svg.isElem('svg')) { attributes.forEach(function (attribute) { - if (!svg.hasAttr(attribute)) { - svg.addAttr({ - name: attribute, - prefix: '', - local: attribute + if (typeof attribute === 'string') { + if (!svg.hasAttr(attribute)) { + svg.addAttr({ + name: attribute, + prefix: '', + local: attribute + }); + } + } else if (typeof attribute === 'object') { + Object.keys(attribute).forEach(function (key) { + if (!svg.hasAttr(key)) { + svg.addAttr({ + name: key, + value: attribute[key], + prefix: '', + local: key + }); + } }); } }); diff --git a/test/plugins/addAttributesToSVGElement.03.svg b/test/plugins/addAttributesToSVGElement.03.svg new file mode 100644 index 00000000..110cf0ff --- /dev/null +++ b/test/plugins/addAttributesToSVGElement.03.svg @@ -0,0 +1,13 @@ + + test + + +@@@ + + + test + + +@@@ + +{"attributes":[{"focusable":"false"},{"data-image":"icon"}]}