mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Check for "href’ by local name
This commit is contained in:
@ -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).
|
||||
|
@ -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];
|
||||
|
@ -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')));
|
||||
|
||||
};
|
||||
|
@ -53,7 +53,7 @@ exports.fn = function(item, params) {
|
||||
if (
|
||||
params.tref &&
|
||||
item.isElem('tref') &&
|
||||
!item.hasAttr('xlink:href')
|
||||
!item.hasAttrLocal('href')
|
||||
) return false;
|
||||
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
|
11
test/plugins/cleanupIDs.05.svg
Normal file
11
test/plugins/cleanupIDs.05.svg
Normal file
@ -0,0 +1,11 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://www.w3.org/1999/xlink">
|
||||
<path d="M0 0" id="a"/>
|
||||
<use x:href="#a" x="50" y="50"/>
|
||||
</svg>
|
||||
|
||||
@@@
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://www.w3.org/1999/xlink">
|
||||
<path d="M0 0" id="a"/>
|
||||
<use x:href="#a" x="50" y="50"/>
|
||||
</svg>
|
After Width: | Height: | Size: 310 B |
28
test/plugins/removeEmptyContainers.03.svg
Normal file
28
test/plugins/removeEmptyContainers.03.svg
Normal file
@ -0,0 +1,28 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://www.w3.org/1999/xlink">
|
||||
<defs>
|
||||
<pattern id="a">
|
||||
<rect/>
|
||||
</pattern>
|
||||
<pattern x:href="url(#a)" id="b"/>
|
||||
</defs>
|
||||
<g>
|
||||
<marker>
|
||||
<a/>
|
||||
</marker>
|
||||
<path d="..."/>
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
@@@
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://www.w3.org/1999/xlink">
|
||||
<defs>
|
||||
<pattern id="a">
|
||||
<rect/>
|
||||
</pattern>
|
||||
<pattern x:href="url(#a)" id="b"/>
|
||||
</defs>
|
||||
<g>
|
||||
<path d="..."/>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 574 B |
Reference in New Issue
Block a user