| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const htmlCommentRegex = require('html-comment-regex'); |
| 3 | |
| 4 | const 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 | |
| 18 | const cleanEntities = svg => { |
| 19 | const entityRegex = /\s*<!Entity\s+\S*\s*(?:"|')[^"]+(?:"|')\s*>/img; |
| 20 | // Remove entities |
| 21 | return svg.replace(entityRegex, ''); |
| 22 | }; |
| 23 | |
| 24 | const regex = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*\s*(?:\[?(?:\s*<![^>]*>\s*)*\]?)*[^>]*>\s*)?(?:<svg[^>]*>[^]*<\/svg>|<svg[^/>]*\/\s*>)\s*$/i; |
| 25 | |
| 26 | const isSvg = input => Boolean(input) && !isBinary(input) && regex.test(cleanEntities(input.toString()).replace(htmlCommentRegex, '')); |
| 27 | |
| 28 | module.exports = isSvg; |
| 29 | // TODO: Remove this for the next major release |
| 30 | module.exports.default = isSvg; |