Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
exports.type = 'perItem'; | ||
|
||
exports.active = false; | ||
|
||
exports.description = 'removes elements that are drawn outside of the viewbox (disabled by default)'; | ||
|
||
var SVGO = require('../lib/svgo.js'), | ||
_path = require('./_path.js'), | ||
intersects = _path.intersects, | ||
path2js = _path.path2js, | ||
viewBoxJS; | ||
|
||
/** | ||
* Remove elements that are drawn outside of the viewbox. | ||
* | ||
* @param {Object} item current iteration item | ||
* @return {Boolean} if false, item will be filtered out | ||
* | ||
* @author JoshyPHP | ||
*/ | ||
exports.fn = function(item) { | ||
|
||
if (item.isElem('path') && typeof viewBoxJS !== 'undefined') | ||
{ | ||
return intersects(viewBoxJS, path2js(item)); | ||
} | ||
if (item.isElem('svg')) | ||
{ | ||
viewBoxJS = getViewBoxJS(item); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Compute the JS representation of the viewbox's path. | ||
* | ||
* @param {Object} svg svg element item | ||
* @return {Object} JS representation, or undefined | ||
*/ | ||
function getViewBoxJS(svg) | ||
{ | ||
var viewbox = ''; | ||
if (svg.hasAttr('viewBox')) | ||
{ | ||
// Remove commas and plus signs, normalize and trim whitespace | ||
viewbox = svg.attr('viewBox').value; | ||
} | ||
else if (svg.hasAttr('height') && svg.hasAttr('width')) | ||
{ | ||
viewbox = '0 0 ' + svg.attr('width').value + ' ' + svg.attr('height').value; | ||
} | ||
|
||
// Remove commas and plus signs, normalize and trim whitespace | ||
viewbox = viewbox.replace(/[,+]|px/g, ' ').replace(/\s+/g, ' ').replace(/^\s*|\s*$/g, ''); | ||
|
||
// Ensure that the dimensions are 4 values separated by space | ||
var m = /^(-?\d*\.?\d+) (-?\d*\.?\d+) (\d*\.?\d+) (\d*\.?\d+)$/.exec(viewbox); | ||
if (!m) | ||
{ | ||
return undefined; | ||
} | ||
|
||
var path = new SVGO().createContentItem({ | ||
elem: 'path', | ||
prefix: '', | ||
local: 'path' | ||
}); | ||
path.addAttr({ | ||
name: 'd', | ||
prefix: '', | ||
local: 'd', | ||
value: 'M' + m[1] + ' ' + m[2] + 'h' + m[3] + 'v' + m[4] + 'H' + m[1] + 'z' | ||
}); | ||
|
||
return path2js(path); | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.