-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add xml-parser to detect any svg content, with or without mimetype
- Loading branch information
1 parent
67a54f3
commit 8b326ed
Showing
4 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @ref: https://github.com/sindresorhus/is-svg | ||
// @ref: https://github.com/sindresorhus/is-svg/pull/38 | ||
import {XMLParser, XMLValidator} from 'fast-xml-parser'; | ||
|
||
export default function isSvg(data: string) { | ||
if (typeof data !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof data}\``); | ||
} | ||
|
||
data = data.toLowerCase().trim(); | ||
|
||
if (data.length === 0) { | ||
return false; | ||
} | ||
|
||
// Has to be `!==` as it can also return an object with error info. | ||
if (XMLValidator.validate(data) !== true) { | ||
return false; | ||
} | ||
|
||
let jsonObject; | ||
const parser = new XMLParser(); | ||
|
||
try { | ||
jsonObject = parser.parse(data); | ||
} catch { | ||
return false; | ||
} | ||
|
||
if (!jsonObject) { | ||
return false; | ||
} | ||
|
||
if (!('svg' in jsonObject)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters