File tree Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -21,9 +21,17 @@ const cleanEntities = svg => {
21
21
return svg . replace ( entityRegex , '' ) ;
22
22
} ;
23
23
24
- const regex = / ^ \s * (?: < \? x m l [ ^ > ] * > \s * ) ? (?: < ! d o c t y p e s v g [ ^ > ] * \s * (?: \[ ? (?: \s * < ! [ ^ > ] * > \s * ) * \] ? ) * [ ^ > ] * > \s * ) ? (?: < s v g [ ^ > ] * > [ ^ ] * < \/ s v g > | < s v g [ ^ / > ] * \/ \s * > ) \s * $ / i ;
24
+ const removeDtdMarkupDeclarations = svg => svg . replace ( / \[ ? (?: \s * < ! [ A - Z ] + [ ^ > ] * > \s * ) * \] ? / g , '' ) ;
25
25
26
- const isSvg = input => Boolean ( input ) && ! isBinary ( input ) && regex . test ( cleanEntities ( input . toString ( ) ) . replace ( htmlCommentRegex , '' ) ) ;
26
+ const clean = svg => {
27
+ svg = cleanEntities ( svg ) ;
28
+ svg = removeDtdMarkupDeclarations ( svg ) ;
29
+ return svg ;
30
+ } ;
31
+
32
+ const regex = / ^ \s * (?: < \? x m l [ ^ > ] * > \s * ) ? (?: < ! d o c t y p e s v g [ ^ > ] * > \s * ) ? (?: < s v g [ ^ > ] * > [ ^ ] * < \/ s v g > | < s v g [ ^ / > ] * \/ \s * > ) \s * $ / i;
33
+
34
+ const isSvg = input => Boolean ( input ) && ! isBinary ( input ) && regex . test ( clean ( input . toString ( ) ) . replace ( htmlCommentRegex , '' ) ) ;
27
35
28
36
module . exports = isSvg ;
29
37
// TODO: Remove this for the next major release
Original file line number Diff line number Diff line change 42
42
"devDependencies" : {
43
43
"@types/node" : " ^11.13.0" ,
44
44
"ava" : " ^1.4.1" ,
45
+ "time-span" : " ^4.0.0" ,
45
46
"tsd" : " ^0.7.2" ,
46
47
"xo" : " ^0.24.0"
47
48
}
Original file line number Diff line number Diff line change 1
1
import fs from 'fs' ;
2
2
import test from 'ava' ;
3
+ import timeSpan from 'time-span' ;
3
4
import isSvg from '.' ;
4
5
5
6
test ( 'valid SVGs' , t => {
@@ -70,3 +71,15 @@ test('support markup inside Entity tags', t => {
70
71
</g>
71
72
</svg>` ) ) ;
72
73
} ) ;
74
+
75
+ test ( 'regex should not be quadratic' , t => {
76
+ const end = timeSpan ( ) ;
77
+
78
+ isSvg ( `<!doctype svg ${ ' ' . repeat ( 34560 ) } ` ) ;
79
+
80
+ if ( end . seconds ( ) < 10 ) {
81
+ t . pass ( ) ;
82
+ } else {
83
+ t . fail ( ) ;
84
+ }
85
+ } ) ;
You can’t perform that action at this time.
0 commit comments