1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-28 09:22:00 +03:00

Update ID in animateMotion ‘begin’ attribute. Fixes #373

This commit is contained in:
GreLI
2015-06-21 17:51:00 +03:00
parent b5d4166fa9
commit 20e2a30773
2 changed files with 22 additions and 11 deletions

View File

@ -15,6 +15,7 @@ exports.params = {
var referencesProps = require('./_collections').referencesProps, var referencesProps = require('./_collections').referencesProps,
regReferencesUrl = /^url\(("|')?#(.+?)\1\)$/, regReferencesUrl = /^url\(("|')?#(.+?)\1\)$/,
regReferencesHref = /^#(.+?)$/, regReferencesHref = /^#(.+?)$/,
regReferencesBegin = /^(\w+?)\./,
styleOrScript = ['style', 'script'], styleOrScript = ['style', 'script'],
generateIDchars = [ generateIDchars = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
@ -83,15 +84,14 @@ exports.fn = function(data, params) {
} }
// save IDs href references // save IDs href references
else if (attr.name === 'xlink:href') { else if (
match = attr.value.match(regReferencesHref); attr.name === 'xlink:href' && (match = attr.value.match(regReferencesHref)) ||
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
if (match) { ) {
if (referencesIDs[idPrefix + match[1]]) { if (referencesIDs[idPrefix + match[1]]) {
referencesIDs[idPrefix + match[1]].push(attr); referencesIDs[idPrefix + match[1]].push(attr);
} else { } else {
referencesIDs[idPrefix + match[1]] = [attr]; referencesIDs[idPrefix + match[1]] = [attr];
}
} }
} }
}); });
@ -126,13 +126,16 @@ exports.fn = function(data, params) {
IDs[k].attr('id').value = currentIDstring; IDs[k].attr('id').value = currentIDstring;
referencesIDs[k].forEach(function(attr) { referencesIDs[k].forEach(function(attr) {
attr.value = attr.value.replace('#' + k.replace(idPrefix, ''), '#' + currentIDstring); k = k.replace(idPrefix, '');
attr.value = attr.value
.replace('#' + k, '#' + currentIDstring)
.replace(k + '.', currentIDstring + '.');
}); });
} }
// don't remove referenced IDs // don't remove referenced IDs
delete IDs[k]; delete IDs[idPrefix + k];
} }
} }

View File

@ -7,14 +7,18 @@
<text id="referencedText"> <text id="referencedText">
referenced text referenced text
</text> </text>
<path id="crochet" d="..."/>
<path id="block" d="..."/>
</defs> </defs>
<g id="g001"> <g id="g001">
<circle id="circle001" fill="url(#gradient001)" cx="60" cy="60" r="50"/> <circle id="circle001" fill="url(#gradient001)" cx="60" cy="60" r="50"/>
<rect fill="url('#gradient001')" x="0" y="0" width="500" height="100"/>
<tref xlink:href="#referencedText"/> <tref xlink:href="#referencedText"/>
</g> </g>
<g> <g>
<tref xlink:href="#referencedText"/> <tref xlink:href="#referencedText"/>
</g> </g>
<animateMotion xlink:href="#crochet" dur="0.5s" begin="block.mouseover" fill="freeze" path="m 0,0 0,-21"/>
</svg> </svg>
@@@ @@@
@ -28,12 +32,16 @@
<text id="b"> <text id="b">
referenced text referenced text
</text> </text>
<path id="c" d="..."/>
<path id="d" d="..."/>
</defs> </defs>
<g> <g>
<circle fill="url(#a)" cx="60" cy="60" r="50"/> <circle fill="url(#a)" cx="60" cy="60" r="50"/>
<rect fill="url('#a')" x="0" y="0" width="500" height="100"/>
<tref xlink:href="#b"/> <tref xlink:href="#b"/>
</g> </g>
<g> <g>
<tref xlink:href="#b"/> <tref xlink:href="#b"/>
</g> </g>
<animateMotion xlink:href="#c" dur="0.5s" begin="d.mouseover" fill="freeze" path="m 0,0 0,-21"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB