Skip to content

Commit

Permalink
fix: πŸ› make markup tag regexp less greedy
Browse files Browse the repository at this point in the history
aka copy-paste from svelte's source

βœ… Closes: #310
  • Loading branch information
kaisermann committed Feb 11, 2021
1 parent 4b35944 commit 384ba5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modules/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function transformMarkup(
markupTagName = markupTagName.toLocaleLowerCase();

const markupPattern = new RegExp(
`<${markupTagName}([\\s\\S]*?)(?:>([\\s\\S]*)<\\/${markupTagName}>|/>)`,
`/<!--[^]*?-->|<${markupTagName}(\\s[^]*?)?(?:>([^]*?)<\\/${markupTagName}>|\\/>)`,
);

const templateMatch = content.match(markupPattern);
Expand All @@ -20,7 +20,7 @@ export async function transformMarkup(
return transformer({ content, attributes: {}, filename, options });
}

const [fullMatch, attributesStr, templateCode] = templateMatch;
const [fullMatch, attributesStr = '', templateCode] = templateMatch;

/** Transform an attribute string into a key-value object */
const attributes = attributesStr
Expand Down
9 changes: 9 additions & 0 deletions test/autoProcess/markup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ test('should transform a custom language between <template lang="..."></template
expect(preprocessed.toString()).toBe('<script></script><style></style>');
});

test('should ignore subsequent markup tags.', async () => {
const input = `<template><div>Hey</div></template><template>ignored</template>`;
const preprocessed = await preprocess(input, sveltePreprocess());

expect(preprocessed.toString()).toBe(
`<div>Hey</div><template>ignored</template>`,
);
});

MARKUP_LANGS.forEach(([lang, ext]) => {
describe(`markup - preprocessor - ${lang}`, () => {
const template = `<template lang="${lang}">${getFixtureContent(
Expand Down

0 comments on commit 384ba5c

Please sign in to comment.