Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 6, 2019
1 parent 7f4af70 commit 0859162
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
10 changes: 5 additions & 5 deletions index.js
@@ -1,19 +1,19 @@
'use strict';
const htmlCommentRegex = require('html-comment-regex');

function isBinary(buf) {
const isBuf = Buffer.isBuffer(buf);
const isBinary = buffer => {
const isBuffer = Buffer.isBuffer(buffer);

for (let i = 0; i < 24; i++) {
const charCode = isBuf ? buf[i] : buf.charCodeAt(i);
const characterCode = isBuffer ? buffer[i] : buffer.charCodeAt(i);

if (charCode === 65533 || charCode <= 8) {
if (characterCode === 65533 || characterCode <= 8) {
return true;
}
}

return false;
}
};

const regex = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*\s*(?:\[?(?:\s*<![^>]*>\s*)*\]?)*[^>]*>\s*)?<svg[^>]*>[^]*<\/svg>\s*$/i;

Expand Down
48 changes: 24 additions & 24 deletions test.js
@@ -1,41 +1,41 @@
import fs from 'fs';
import test from 'ava';
import m from '.';
import isSvg from '.';

test('valid SVGs', t => {
t.true(m(fs.readFileSync('fixtures/fixture.svg')));
t.true(m('<svg width="100" height="100" viewBox="0 0 30 30" version="1.1"></svg>'));
t.true(m('<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>'));
t.true(m('<?xml version="1.0" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg></svg>'));
t.true(m('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">]><svg></svg>'));
t.true(m('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/"> <!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/"> ]><svg></svg>'));
t.true(m('<svg></svg> '));
t.true(m(' <svg></svg>'));
t.true(m('<svg>\n</svg>'));
t.true(m('<!--unicorn--><svg>\n</svg><!--cake-->'));
t.true(m(`
t.true(isSvg(fs.readFileSync('fixtures/fixture.svg')));
t.true(isSvg('<svg width="100" height="100" viewBox="0 0 30 30" version="1.1"></svg>'));
t.true(isSvg('<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>'));
t.true(isSvg('<?xml version="1.0" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg></svg>'));
t.true(isSvg('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">]><svg></svg>'));
t.true(isSvg('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/"> <!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/"> ]><svg></svg>'));
t.true(isSvg('<svg></svg> '));
t.true(isSvg(' <svg></svg>'));
t.true(isSvg('<svg>\n</svg>'));
t.true(isSvg('<!--unicorn--><svg>\n</svg><!--cake-->'));
t.true(isSvg(`
<!-- Generator: Some Graphic Design Software -->
<svg version="1.1">
</svg>
`));
});

test('invalid SVGs', t => {
t.false(m(fs.readFileSync('fixtures/fixture.jpg')));
t.false(m('this is not svg, but it mentions <svg> tags'));
t.false(m('<svg> hello I am an svg oops maybe not'));
t.false(m('<svg></svg> this string starts with an svg'));
t.false(m('this string ends with an svg <svg></svg>'));
t.false(m('<div><svg></svg>'));
t.false(m('<div><svg></svg></div>'));
t.false(m('this string contains an svg <svg></svg> in the middle'));
t.false(m(fs.readFileSync('readme.md')));
t.false(m(fs.readFileSync('index.js')));
t.false(m());
t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg')));
t.false(isSvg('this is not svg, but it mentions <svg> tags'));
t.false(isSvg('<svg> hello I am an svg oops maybe not'));
t.false(isSvg('<svg></svg> this string starts with an svg'));
t.false(isSvg('this string ends with an svg <svg></svg>'));
t.false(isSvg('<div><svg></svg>'));
t.false(isSvg('<div><svg></svg></div>'));
t.false(isSvg('this string contains an svg <svg></svg> in the middle'));
t.false(isSvg(fs.readFileSync('readme.md')));
t.false(isSvg(fs.readFileSync('index.js')));
t.false(isSvg());
});

test('supports non-english characters', t => {
t.true(m(`<svg xmlns="http://www.w3.org/2000/svg"
t.true(isSvg(`<svg xmlns="http://www.w3.org/2000/svg"
width="100%" height="100%" viewBox="0 0 400 400"
direction="rtl" xml:lang="fa">
Expand Down

0 comments on commit 0859162

Please sign in to comment.