File tree Expand file tree Collapse file tree 4 files changed +50
-5
lines changed Expand file tree Collapse file tree 4 files changed +50
-5
lines changed Original file line number Diff line number Diff line change
1
+ export type Options = {
2
+ /**
3
+ Whether to validate the SVG as proper XML.
4
+
5
+ Turning this off can improve performance significantly.
6
+
7
+ @default true
8
+ */
9
+ validate ?: boolean ;
10
+ } ;
11
+
1
12
/**
2
13
Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
3
14
@@ -9,4 +20,4 @@ isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
9
20
//=> true
10
21
```
11
22
*/
12
- export default function isSvg ( string : string ) : boolean ;
23
+ export default function isSvg ( string : string , options ?: Options ) : boolean ;
Original file line number Diff line number Diff line change 1
1
import { XmlTextDetector } from '@file-type/xml' ;
2
2
3
- export default function isSvg ( string ) {
3
+ export default function isSvg ( string , { validate = true } = { } ) {
4
4
if ( typeof string !== 'string' ) {
5
5
throw new TypeError ( `Expected a \`string\`, got \`${ typeof string } \`` ) ;
6
6
}
@@ -11,7 +11,23 @@ export default function isSvg(string) {
11
11
return false ;
12
12
}
13
13
14
- const xmlTextDetector = new XmlTextDetector ( ) ;
15
- xmlTextDetector . write ( string ) ;
16
- return xmlTextDetector . isValid ( ) && xmlTextDetector . fileType ?. ext === 'svg' ;
14
+ const xmlTextDetector = new XmlTextDetector ( { fullScan : validate } ) ;
15
+
16
+ if ( validate ) {
17
+ xmlTextDetector . write ( string ) ;
18
+
19
+ if ( ! xmlTextDetector . isValid ( ) ) {
20
+ return false ;
21
+ }
22
+ } else {
23
+ const chunkSize = 128 ;
24
+
25
+ let offset = 0 ;
26
+ while ( string . length > offset && ! xmlTextDetector . onEnd ) {
27
+ xmlTextDetector . write ( string . slice ( offset , Math . min ( offset + chunkSize , string . length ) ) ) ;
28
+ offset += chunkSize ;
29
+ }
30
+ }
31
+
32
+ return xmlTextDetector . fileType ?. ext === 'svg' ;
17
33
}
Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ import {expectType} from 'tsd';
2
2
import isSvg from './index.js' ;
3
3
4
4
expectType < boolean > ( isSvg ( '<svg></svg>' ) ) ;
5
+ expectType < boolean > ( isSvg ( '<svg></svg>' , { validate : false } ) ) ;
Original file line number Diff line number Diff line change @@ -16,3 +16,20 @@ import isSvg from 'is-svg';
16
16
isSvg (' <svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>' );
17
17
// => true
18
18
```
19
+
20
+ ## API
21
+
22
+ ### isSvg(string, options?)
23
+
24
+ #### options
25
+
26
+ Type: ` object `
27
+
28
+ ##### validate
29
+
30
+ Type: ` boolean ` \
31
+ Default: ` true `
32
+
33
+ Whether to validate the SVG as proper XML.
34
+
35
+ Turning this off can improve performance significantly.
You can’t perform that action at this time.
0 commit comments