fix(workspace): fix svg file with xml header (#5388)

This commit is contained in:
EYHN 2023-12-25 06:58:11 +00:00
parent 9493bd99f9
commit 56f0580382
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C
2 changed files with 20 additions and 6 deletions

View File

@ -9,5 +9,10 @@ describe('isSvgBuffer', () => {
expect(isSvgBuffer(Buffer.from('<svg></svg>'))).toBe(true);
expect(isSvgBuffer(Buffer.from(' \n\r\t<svg></svg>'))).toBe(true);
expect(isSvgBuffer(Buffer.from('<123>'))).toBe(false);
expect(
isSvgBuffer(
Buffer.from('<?xml version="1.0" encoding="UTF-8"?><svg></svg>')
)
).toBe(true);
});
});

View File

@ -1,7 +1,7 @@
import isSvg from 'is-svg';
function fastCheckIsNotSvg(buffer: Uint8Array) {
// check first non-whitespace character is not '<svg'
// check first non-whitespace character is not '<svg' or '<?xml'
for (let i = 0; i < buffer.length; i++) {
const ch = buffer[i];
@ -18,11 +18,20 @@ function fastCheckIsNotSvg(buffer: Uint8Array) {
continue;
}
return !(
buffer[i] === /* '<' */ 0x3c &&
buffer[i + 1] === /* 's' */ 0x73 &&
buffer[i + 2] === /* 'v' */ 0x76 &&
buffer[i + 3] === /* 'g' */ 0x67
return (
!(
buffer[i] === /* '<' */ 0x3c &&
buffer[i + 1] === /* 's' */ 0x73 &&
buffer[i + 2] === /* 'v' */ 0x76 &&
buffer[i + 3] === /* 'g' */ 0x67
) &&
!(
buffer[i] === /* '<' */ 0x3c &&
buffer[i + 1] === /* '?' */ 0x3f &&
buffer[i + 2] === /* 'x' */ 0x78 &&
buffer[i + 3] === /* 'm' */ 0x6d &&
buffer[i + 4] === /* 'l' */ 0x6c
)
);
}