diff --git a/lib/svgo/jsAPI.js b/lib/svgo/jsAPI.js index bbc99fb4..5361fce0 100644 --- a/lib/svgo/jsAPI.js +++ b/lib/svgo/jsAPI.js @@ -132,6 +132,48 @@ JSAPI.prototype.renameElem = function(name) { }; +/** + * Determine if element has an attribute by local name + * (any, or by name or by name + value). + * + * @param {String} [name] attribute name + * @param {String} [val] attribute value (will be toString()'ed) + * @return {Boolean} + */ + JSAPI.prototype.hasAttrLocal = function(localName, val) { + + if (!this.attrs || !Object.keys(this.attrs).length) return false; + + if (!arguments.length) return !!this.attrs; + + var callback; + + switch (val != null && val.constructor && val.constructor.name) { + case 'Function': callback = funcValueTest; break; + case 'RegExp': callback = regexpValueTest; break; + case 'String': callback = stringValueTest; break; + default: callback = nameTest; + } + return this.someAttr(callback); + + function nameTest(attr) { + return attr.local === localName; + } + + function funcValueTest(attr) { + return attr.local === localName && val(attr.value); + } + + function regexpValueTest(attr) { + return attr.local === localName && val.test(attr.value); + } + + function stringValueTest(attr) { + return attr.local === localName && val == attr.value; + } + +}; + /** * Get a specific attribute from an element * (by name or name + value). diff --git a/plugins/cleanupIDs.js b/plugins/cleanupIDs.js index 6ae6d914..c89056af 100644 --- a/plugins/cleanupIDs.js +++ b/plugins/cleanupIDs.js @@ -91,7 +91,7 @@ exports.fn = function(data, params) { // save IDs href references else if ( - attr.name === 'xlink:href' && (match = attr.value.match(regReferencesHref)) || + attr.local === 'href' && (match = attr.value.match(regReferencesHref)) || attr.name === 'begin' && (match = attr.value.match(regReferencesBegin)) ) { key = idPrefix + match[1]; diff --git a/plugins/removeEmptyContainers.js b/plugins/removeEmptyContainers.js index 02803d82..356a5835 100644 --- a/plugins/removeEmptyContainers.js +++ b/plugins/removeEmptyContainers.js @@ -27,6 +27,6 @@ var container = require('./_collections').elemsGroups.container; exports.fn = function(item) { return !(item.isElem(container) && !item.isElem('svg') && item.isEmpty() && - (!item.isElem('pattern') || !item.hasAttr('xlink:href'))); + (!item.isElem('pattern') || !item.hasAttrLocal('href'))); }; diff --git a/plugins/removeEmptyText.js b/plugins/removeEmptyText.js index fd80e950..049b1819 100644 --- a/plugins/removeEmptyText.js +++ b/plugins/removeEmptyText.js @@ -53,7 +53,7 @@ exports.fn = function(item, params) { if ( params.tref && item.isElem('tref') && - !item.hasAttr('xlink:href') + !item.hasAttrLocal('href') ) return false; }; diff --git a/plugins/removeRasterImages.js b/plugins/removeRasterImages.js index 71ee9c58..cbddebe3 100644 --- a/plugins/removeRasterImages.js +++ b/plugins/removeRasterImages.js @@ -20,8 +20,7 @@ exports.fn = function(item) { if ( item.isElem('image') && - item.hasAttr('xlink:href') && - /(\.|image\/)(jpg|png|gif)/.test(item.attr('xlink:href').value) + item.hasAttrLocal('href', /(\.|image\/)(jpg|png|gif)/) ) { return false; } diff --git a/test/plugins/cleanupIDs.05.svg b/test/plugins/cleanupIDs.05.svg new file mode 100644 index 00000000..1cdd603a --- /dev/null +++ b/test/plugins/cleanupIDs.05.svg @@ -0,0 +1,11 @@ + + + + + +@@@ + + + + + diff --git a/test/plugins/removeEmptyContainers.03.svg b/test/plugins/removeEmptyContainers.03.svg new file mode 100644 index 00000000..24bb71ee --- /dev/null +++ b/test/plugins/removeEmptyContainers.03.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + +@@@ + + + + + + + + + + + +