You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 904B

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