├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixtures ├── fixture.jpg └── fixture.svg ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixtures/fixture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/is-svg/61a51ace851133b1fb7c0046605269abb7c8db54/fixtures/fixture.jpg -------------------------------------------------------------------------------- /fixtures/fixture.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics). 3 | 4 | @example 5 | ``` 6 | import isSvg from 'is-svg'; 7 | 8 | isSvg(''); 9 | //=> true 10 | ``` 11 | */ 12 | export default function isSvg(string: string): boolean; 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {XmlTextDetector} from '@file-type/xml'; 2 | 3 | export default function isSvg(string) { 4 | if (typeof string !== 'string') { 5 | throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``); 6 | } 7 | 8 | string = string.trim(); 9 | 10 | if (string.length === 0) { 11 | return false; 12 | } 13 | 14 | const xmlTextDetector = new XmlTextDetector(); 15 | xmlTextDetector.write(string); 16 | return xmlTextDetector.isValid() && xmlTextDetector.fileType?.ext === 'svg'; 17 | } 18 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import isSvg from './index.js'; 3 | 4 | expectType(isSvg('')); 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-svg", 3 | "version": "6.0.0", 4 | "description": "Check if a string is SVG", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-svg", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=20" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "svg", 31 | "vector", 32 | "graphics", 33 | "image", 34 | "picture", 35 | "type", 36 | "detect", 37 | "check", 38 | "is", 39 | "string" 40 | ], 41 | "dependencies": { 42 | "@file-type/xml": "^0.4.3" 43 | }, 44 | "devDependencies": { 45 | "@types/node": "^22.15.3", 46 | "ava": "^6.2.0", 47 | "time-span": "^5.1.0", 48 | "tsd": "^0.32.0", 49 | "xo": "^0.60.0" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-svg 2 | 3 | > Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install is-svg 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import isSvg from 'is-svg'; 15 | 16 | isSvg(''); 17 | //=> true 18 | ``` 19 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import test from 'ava'; 3 | import timeSpan from 'time-span'; 4 | import isSvg from './index.js'; 5 | 6 | test('valid SVGs', t => { 7 | t.true(isSvg(fs.readFileSync('fixtures/fixture.svg', 'utf8'))); 8 | t.true(isSvg('')); 9 | t.true(isSvg('')); 10 | t.true(isSvg('\n\n')); 11 | t.true(isSvg(']>')); 12 | t.true(isSvg(' ]>')); 13 | t.true(isSvg(' ')); 14 | t.true(isSvg(' ')); 15 | t.true(isSvg('\n')); 16 | t.true(isSvg('\n')); 17 | t.true(isSvg('')); 18 | t.true(isSvg(` 19 | 20 | 21 | 22 | `)); 23 | t.true(isSvg(` 24 | `)); 30 | t.true(isSvg('')); 31 | }); 32 | 33 | test('invalid SVGs', t => { 34 | t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg', 'utf8'))); 35 | t.false(isSvg('')); 36 | t.false(isSvg('')); 37 | t.false(isSvg(fs.readFileSync('index.js', 'utf8'))); 38 | t.false(isSvg('this string contains an svg in the middle')); 39 | t.false(isSvg('')); 40 | t.false(isSvg('this string ends with an svg ')); 41 | t.false(isSvg(' hello I am an svg oops maybe not')); 42 | t.false(isSvg('this is not svg, but it mentions tags')); 43 | t.false(isSvg(fs.readFileSync('readme.md', 'utf8'))); 44 | 45 | // https://github.com/NaturalIntelligence/fast-xml-parser/issues/327 46 | // t.false(isSvg(' this string starts with an svg')); 47 | }); 48 | 49 | test('supports non-english characters', t => { 50 | t.true(isSvg(` 53 | 54 | Right-to-left Text 55 | 56 | A simple example for using the 'direction' property in documents 57 | that predominantly use right-to-left languages. 58 | 59 | 60 | داستان SVG 1.1 SE طولا ني است. 61 | 62 | `)); 63 | }); 64 | 65 | test('support markup inside Entity tags', t => { 66 | t.true(isSvg(' "> ]>&Smile;')); 67 | t.true(isSvg(' "> ]>&Smile;')); 68 | t.true(isSvg(` 69 | 70 | "> 72 | "> 73 | ]> 74 | 76 | 77 | &Melon; 78 | 79 | 80 | &Orange; 81 | 82 | `)); 83 | }); 84 | 85 | test('regex should not be quadratic', t => { 86 | const end = timeSpan(); 87 | 88 | isSvg(`