File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,9 +21,17 @@ const cleanEntities = svg => {
2121 return svg . replace ( entityRegex , '' ) ;
2222} ;
2323
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 , '' ) ;
2525
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 , '' ) ) ;
2735
2836module . exports = isSvg ;
2937// TODO: Remove this for the next major release
Original file line number Diff line number Diff line change 4242 "devDependencies" : {
4343 "@types/node" : " ^11.13.0" ,
4444 "ava" : " ^1.4.1" ,
45+ "time-span" : " ^4.0.0" ,
4546 "tsd" : " ^0.7.2" ,
4647 "xo" : " ^0.24.0"
4748 }
Original file line number Diff line number Diff line change 11import fs from 'fs' ;
22import test from 'ava' ;
3+ import timeSpan from 'time-span' ;
34import isSvg from '.' ;
45
56test ( 'valid SVGs' , t => {
@@ -70,3 +71,15 @@ test('support markup inside Entity tags', t => {
7071 </g>
7172 </svg>` ) ) ;
7273} ) ;
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