@@ -4,8 +4,8 @@ exports.type = 'perItem';
4
4
5
5
exports . active = true ;
6
6
7
- var empty = { value : 0 } ,
8
- regSeparator = / \s + , ? \s * | , \s * / ;
7
+ var none = { value : 0 } ,
8
+ regNumber = / [ - + ] ? (?: \d * \. \d + | \d + \. ? ) (?: [ e E ] [ - + ] ? \d + ) ? / g ;
9
9
10
10
/**
11
11
* Converts basic shape to more compact path.
@@ -30,8 +30,8 @@ exports.fn = function(item) {
30
30
! item . hasAttr ( 'ry' )
31
31
) {
32
32
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 ,
35
35
width = + item . attr ( 'width' ) . value ,
36
36
height = + item . attr ( 'height' ) . value ;
37
37
@@ -54,15 +54,15 @@ exports.fn = function(item) {
54
54
local : 'd'
55
55
} ) ;
56
56
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' ] ) ;
59
59
60
60
} else if ( item . isElem ( 'line' ) ) {
61
61
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 ;
66
66
if ( isNaN ( x1 - y1 + x2 - y2 ) ) return ;
67
67
68
68
item . addAttr ( {
@@ -72,8 +72,8 @@ exports.fn = function(item) {
72
72
local : 'd'
73
73
} ) ;
74
74
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' ] ) ;
77
77
78
78
} else if ( (
79
79
item . isElem ( 'polyline' ) ||
@@ -82,7 +82,12 @@ exports.fn = function(item) {
82
82
item . hasAttr ( 'points' )
83
83
) {
84
84
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
+
86
91
if ( coords . length < 4 ) return false ;
87
92
88
93
item . addAttr ( {
@@ -94,9 +99,8 @@ exports.fn = function(item) {
94
99
local : 'd'
95
100
} ) ;
96
101
97
- item . removeAttr ( 'points' ) ;
98
- item . elem = item . local = 'path' ;
99
-
102
+ item . renameElem ( 'path' )
103
+ . removeAttr ( 'points' ) ;
100
104
}
101
105
102
106
} ;
0 commit comments