blob: 3d5cfe4bddfe3635b541d54499aa0f5cefd4a289 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const htmlCommentRegex = require('html-comment-regex');
3
4const isBinary = buffer => {
5 const isBuffer = Buffer.isBuffer(buffer);
6
7 for (let i = 0; i < 24; i++) {
8 const characterCode = isBuffer ? buffer[i] : buffer.charCodeAt(i);
9
10 if (characterCode === 65533 || characterCode <= 8) {
11 return true;
12 }
13 }
14
15 return false;
16};
17
18const cleanEntities = svg => {
19 const entityRegex = /\s*<!Entity\s+\S*\s*(?:"|')[^"]+(?:"|')\s*>/img;
20 // Remove entities
21 return svg.replace(entityRegex, '');
22};
23
24const regex = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*\s*(?:\[?(?:\s*<![^>]*>\s*)*\]?)*[^>]*>\s*)?(?:<svg[^>]*>[^]*<\/svg>|<svg[^/>]*\/\s*>)\s*$/i;
25
26const isSvg = input => Boolean(input) && !isBinary(input) && regex.test(cleanEntities(input.toString()).replace(htmlCommentRegex, ''));
27
28module.exports = isSvg;
29// TODO: Remove this for the next major release
30module.exports.default = isSvg;