From f36b414d300ae0b05db84818744d981d9518cd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=B6ller?= Date: Tue, 7 Jul 2020 17:07:59 +0200 Subject: [PATCH] preprocess self-closing script and style tags (#5082) --- src/compiler/preprocess/index.ts | 8 ++++---- .../samples/script-self-closing/_config.js | 12 ++++++++++++ .../samples/script-self-closing/input.svelte | 1 + .../samples/script-self-closing/output.svelte | 1 + .../preprocess/samples/style-self-closing/_config.js | 12 ++++++++++++ .../samples/style-self-closing/input.svelte | 3 +++ .../samples/style-self-closing/output.svelte | 3 +++ 7 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/preprocess/samples/script-self-closing/_config.js create mode 100644 test/preprocess/samples/script-self-closing/input.svelte create mode 100644 test/preprocess/samples/script-self-closing/output.svelte create mode 100644 test/preprocess/samples/style-self-closing/_config.js create mode 100644 test/preprocess/samples/style-self-closing/input.svelte create mode 100644 test/preprocess/samples/style-self-closing/output.svelte diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 1a13b869e7..d90d4814d7 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -94,8 +94,8 @@ export default async function preprocess( for (const fn of script) { source = await replace_async( source, - /|([^]*?)<\/script>/gi, - async (match, attributes = '', content) => { + /|([^]*?)<\/script>|\/>)/gi, + async (match, attributes = '', content = '') => { if (!attributes && !content) { return match; } @@ -114,8 +114,8 @@ export default async function preprocess( for (const fn of style) { source = await replace_async( source, - /|([^]*?)<\/style>/gi, - async (match, attributes = '', content) => { + /|([^]*?)<\/style>|\/>)/gi, + async (match, attributes = '', content = '') => { if (!attributes && !content) { return match; } diff --git a/test/preprocess/samples/script-self-closing/_config.js b/test/preprocess/samples/script-self-closing/_config.js new file mode 100644 index 0000000000..4baab9730e --- /dev/null +++ b/test/preprocess/samples/script-self-closing/_config.js @@ -0,0 +1,12 @@ +import * as assert from "assert"; + +export default { + preprocess: { + script: ({ content, attributes }) => { + assert.equal(content, ""); + return { + code: `console.log("${attributes["the-answer"]}");` + }; + } + } +}; diff --git a/test/preprocess/samples/script-self-closing/input.svelte b/test/preprocess/samples/script-self-closing/input.svelte new file mode 100644 index 0000000000..c5816cb3ba --- /dev/null +++ b/test/preprocess/samples/script-self-closing/input.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/preprocess/samples/style-self-closing/_config.js b/test/preprocess/samples/style-self-closing/_config.js new file mode 100644 index 0000000000..0a05cd3d64 --- /dev/null +++ b/test/preprocess/samples/style-self-closing/_config.js @@ -0,0 +1,12 @@ +import * as assert from "assert"; + +export default { + preprocess: { + style: ({ content, attributes: { color } }) => { + assert.equal(content, ""); + return { + code: `div { color: ${color}; }` + }; + } + } +}; diff --git a/test/preprocess/samples/style-self-closing/input.svelte b/test/preprocess/samples/style-self-closing/input.svelte new file mode 100644 index 0000000000..07176c87e5 --- /dev/null +++ b/test/preprocess/samples/style-self-closing/input.svelte @@ -0,0 +1,3 @@ +
$brand
+ + \ No newline at end of file