UNPKG

1.14 kBJavaScriptView Raw
1'use strict';
2
3exports.type = 'perItem';
4
5exports.active = true;
6
7exports.description = 'removes viewBox attribute when possible';
8
9var viewBoxElems = ['svg', 'pattern', 'symbol'];
10
11/**
12 * Remove viewBox attr which coincides with a width/height box.
13 *
14 * @see http://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
15 *
16 * @example
17 * <svg width="100" height="50" viewBox="0 0 100 50">
18 * ⬇
19 * <svg width="100" height="50">
20 *
21 * @param {Object} item current iteration item
22 * @return {Boolean} if false, item will be filtered out
23 *
24 * @author Kir Belevich
25 */
26exports.fn = function(item) {
27
28 if (
29 item.isElem(viewBoxElems) &&
30 item.hasAttr('viewBox') &&
31 item.hasAttr('width') &&
32 item.hasAttr('height')
33 ) {
34
35 var nums = item.attr('viewBox').value.split(/[ ,]+/g);
36
37 if (
38 nums[0] === '0' &&
39 nums[1] === '0' &&
40 item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
41 item.attr('height').value.replace(/px$/, '') === nums[3]
42 ) {
43 item.removeAttr('viewBox');
44 }
45
46 }
47
48};