Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eecfabd

Browse files
committed
Code styled and logic improvement in convertShapeToPath
1 parent d3e4f63 commit eecfabd

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

lib/svgo/jsAPI.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ JSAPI.prototype.isElem = function(param) {
2626

2727
};
2828

29+
/**
30+
* Renames an element
31+
*
32+
* @param {String} name new element name
33+
* @return {Object} element
34+
*/
35+
JSAPI.prototype.renameElem = function(name) {
36+
37+
if (typeof name == 'string' && name != '')
38+
this.elem = this.local = name;
39+
40+
return this;
41+
42+
};
43+
2944
/**
3045
* Determine if element is empty.
3146
*
@@ -97,17 +112,19 @@ JSAPI.prototype.isElem = function(param) {
97112
/**
98113
* Remove a specific attribute.
99114
*
100-
* @param {String} name attribute name
115+
* @param {String|Array} name attribute name
101116
* @param {String} [val] attribute value
102117
* @return {Boolean}
103118
*/
104-
JSAPI.prototype.removeAttr = function(name, val) {
119+
JSAPI.prototype.removeAttr = function(name, val, recursive) {
105120

106121
if (!arguments.length) return false;
107122

123+
if (Array.isArray(name)) name.forEach(this.removeAttr, this);
124+
108125
if (!this.hasAttr(name)) return false;
109126

110-
if (val && this.attrs[name].value !== val) return false;
127+
if (!recursive && val && this.attrs[name].value !== val) return false;
111128

112129
delete this.attrs[name];
113130

plugins/convertShapeToPath.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ exports.type = 'perItem';
44

55
exports.active = true;
66

7-
var empty = { value: 0 },
8-
regSeparator = /\s+,?\s*|,\s*/;
7+
var none = { value: 0 },
8+
regNumber = /[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g;
99

1010
/**
1111
* Converts basic shape to more compact path.
@@ -30,8 +30,8 @@ exports.fn = function(item) {
3030
!item.hasAttr('ry')
3131
) {
3232

33-
var x = +(item.attr('x') || empty).value,
34-
y = +(item.attr('y') || empty).value,
33+
var x = +(item.attr('x') || none).value,
34+
y = +(item.attr('y') || none).value,
3535
width = +item.attr('width').value,
3636
height = +item.attr('height').value;
3737

@@ -54,15 +54,15 @@ exports.fn = function(item) {
5454
local: 'd'
5555
});
5656

57-
['x', 'y', 'width', 'height'].forEach(function(attr){ item.removeAttr(attr) });
58-
item.elem = item.local = 'path';
57+
item.renameElem('path')
58+
.removeAttr(['x', 'y', 'width', 'height']);
5959

6060
} else if (item.isElem('line')) {
6161

62-
var x1 = +(item.attr('x1') || empty).value,
63-
y1 = +(item.attr('y1') || empty).value,
64-
x2 = +(item.attr('x2') || empty).value,
65-
y2 = +(item.attr('y2') || empty).value;
62+
var x1 = +(item.attr('x1') || none).value,
63+
y1 = +(item.attr('y1') || none).value,
64+
x2 = +(item.attr('x2') || none).value,
65+
y2 = +(item.attr('y2') || none).value;
6666
if (isNaN(x1 - y1 + x2 - y2)) return;
6767

6868
item.addAttr({
@@ -72,8 +72,8 @@ exports.fn = function(item) {
7272
local: 'd'
7373
});
7474

75-
['x1', 'y1', 'x2', 'y2'].forEach(function(attr){ item.removeAttr(attr) });
76-
item.elem = item.local = 'path';
75+
item.renameElem('path')
76+
.removeAttr(['x1', 'y1', 'x2', 'y2']);
7777

7878
} else if ((
7979
item.isElem('polyline') ||
@@ -82,7 +82,12 @@ exports.fn = function(item) {
8282
item.hasAttr('points')
8383
) {
8484

85-
var coords = item.attr('points').value.split(regSeparator);
85+
var points = item.attr('points').value.trim(),
86+
coords = [];
87+
88+
for (var num; num = regNumber.exec(points);)
89+
coords.push(+num);
90+
8691
if (coords.length < 4) return false;
8792

8893
item.addAttr({
@@ -94,9 +99,8 @@ exports.fn = function(item) {
9499
local: 'd'
95100
});
96101

97-
item.removeAttr('points');
98-
item.elem = item.local = 'path';
99-
102+
item.renameElem('path')
103+
.removeAttr('points');
100104
}
101105

102106
};

test/plugins/convertShapeToPath.03.svg

Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)