Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/cli/src/commands/create/icons.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from 'vitest';

import { stripXmlPreamble } from './icons.js';

describe('stripXmlPreamble', () => {
it('strips <?xml?> declaration', () => {
const input = '<?xml version="1.0" encoding="UTF-8"?>\n<svg viewBox="0 0 24 24"></svg>';
expect(stripXmlPreamble(input)).toBe('<svg viewBox="0 0 24 24"></svg>');
});

it('strips <!DOCTYPE> declaration', () => {
const input =
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg></svg>';
expect(stripXmlPreamble(input)).toBe('<svg></svg>');
});

it('strips XML comments before <svg>', () => {
const input = '<!-- generated by Inkscape -->\n<svg></svg>';
expect(stripXmlPreamble(input)).toBe('<svg></svg>');
});

it('strips combined preamble (<?xml?> + <!DOCTYPE> + comment)', () => {
const input = [
'<?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">',
'<!-- Created with Illustrator -->',
'<svg viewBox="0 0 100 100"></svg>',
].join('\n');
expect(stripXmlPreamble(input)).toBe('<svg viewBox="0 0 100 100"></svg>');
});

it('returns input unchanged when no preamble exists', () => {
const input = '<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>';
expect(stripXmlPreamble(input)).toBe(input);
});

it('handles leading whitespace before <svg>', () => {
const input = ' \n <svg></svg>';
expect(stripXmlPreamble(input)).toBe('<svg></svg>');
});
});
7 changes: 6 additions & 1 deletion packages/cli/src/commands/create/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { dirExists } from '../../util/files.js';

type SvgAsset = { name: string; contents: string };

/** Strip XML preamble (<?xml?>, <!DOCTYPE>, comments) before the <svg> tag. */
export function stripXmlPreamble(contents: string): string {
return contents.replace(/^[\s\S]*?(<svg)/i, '$1');
}

// TODO: When Blocks support is fully removed, this command can be removed as well, since icons will
// be loaded directly in the webview once we're fully in a Devvit Web world.
export default class Icons extends DevvitCommand {
Expand Down Expand Up @@ -58,7 +63,7 @@ export default class Icons extends DevvitCommand {
assets.map(async (asset) => {
const name = path.relative(assetsPath, asset);
const contents = await fsp.readFile(asset, 'utf-8');
return { name, contents };
return { name, contents: stripXmlPreamble(contents) };
})
);
}
Expand Down