Skip to content

Commit

Permalink
Preverve viewBox in nested <svg>
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Mar 6, 2021
1 parent a2b0e73 commit 28c01cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
37 changes: 19 additions & 18 deletions plugins/removeViewBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ var viewBoxElems = ['svg', 'pattern', 'symbol'];
*
* @author Kir Belevich
*/
exports.fn = function(item) {
exports.fn = function (item) {
if (
item.isElem(viewBoxElems) &&
item.hasAttr('viewBox') &&
item.hasAttr('width') &&
item.hasAttr('height')
) {
// TODO remove width/height for such case instead
if (item.isElem('svg') && item.closestElem('svg')) {
return;
}

var nums = item.attr('viewBox').value.split(/[ ,]+/g);

if (
item.isElem(viewBoxElems) &&
item.hasAttr('viewBox') &&
item.hasAttr('width') &&
item.hasAttr('height')
nums[0] === '0' &&
nums[1] === '0' &&
item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
item.attr('height').value.replace(/px$/, '') === nums[3]
) {

var nums = item.attr('viewBox').value.split(/[ ,]+/g);

if (
nums[0] === '0' &&
nums[1] === '0' &&
item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
item.attr('height').value.replace(/px$/, '') === nums[3]
) {
item.removeAttr('viewBox');
}

item.removeAttr('viewBox');
}

}
};
29 changes: 29 additions & 0 deletions test/plugins/removeViewBox.05.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ const runTests = async ({ svgFiles }) => {
name === 'styling-css-01-b' ||
name === 'styling-css-03-b' ||
name === 'styling-css-04-f' ||
// mismatched draft cases
name === 'struct-use-07-b'
// strange artifact breaks inconsistently breaks regression tests
name === 'filters-conv-05-f'
) {
console.info(`${name} is skipped`);
skipped += 1;
Expand Down

0 comments on commit 28c01cf

Please sign in to comment.