1
0
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:
GreLI
2016-03-11 13:31:30 +03:00
parent 4de61ef494
commit c3e0eb6b00
7 changed files with 85 additions and 5 deletions

View File

@ -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 * Get a specific attribute from an element
* (by name or name + value). * (by name or name + value).

View File

@ -91,7 +91,7 @@ exports.fn = function(data, params) {
// save IDs href references // save IDs href references
else if ( 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)) attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
) { ) {
key = idPrefix + match[1]; key = idPrefix + match[1];

View File

@ -27,6 +27,6 @@ var container = require('./_collections').elemsGroups.container;
exports.fn = function(item) { exports.fn = function(item) {
return !(item.isElem(container) && !item.isElem('svg') && item.isEmpty() && return !(item.isElem(container) && !item.isElem('svg') && item.isEmpty() &&
(!item.isElem('pattern') || !item.hasAttr('xlink:href'))); (!item.isElem('pattern') || !item.hasAttrLocal('href')));
}; };

View File

@ -53,7 +53,7 @@ exports.fn = function(item, params) {
if ( if (
params.tref && params.tref &&
item.isElem('tref') && item.isElem('tref') &&
!item.hasAttr('xlink:href') !item.hasAttrLocal('href')
) return false; ) return false;
}; };

View File

@ -20,8 +20,7 @@ exports.fn = function(item) {
if ( if (
item.isElem('image') && item.isElem('image') &&
item.hasAttr('xlink:href') && item.hasAttrLocal('href', /(\.|image\/)(jpg|png|gif)/)
/(\.|image\/)(jpg|png|gif)/.test(item.attr('xlink:href').value)
) { ) {
return false; return false;
} }

View 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

View 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