From b2e1f1c1029bd059961a50d51e4da8f2e741a9d7 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:33:04 +0100 Subject: [PATCH] fix: handle closing tags inside attribute values (#9486) Related to https://github.com/sveltejs/language-tools/issues/2204 --- .changeset/twelve-ears-act.md | 5 +++++ packages/svelte/src/compiler/preprocess/index.js | 6 ++++-- .../samples/attributes-with-closing-tag/_config.js | 6 ++++++ .../samples/attributes-with-closing-tag/input.svelte | 3 +++ .../samples/attributes-with-closing-tag/output.svelte | 1 + 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/twelve-ears-act.md create mode 100644 packages/svelte/test/preprocess/samples/attributes-with-closing-tag/_config.js create mode 100644 packages/svelte/test/preprocess/samples/attributes-with-closing-tag/input.svelte create mode 100644 packages/svelte/test/preprocess/samples/attributes-with-closing-tag/output.svelte diff --git a/.changeset/twelve-ears-act.md b/.changeset/twelve-ears-act.md new file mode 100644 index 0000000000..0f745d4b48 --- /dev/null +++ b/.changeset/twelve-ears-act.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: handle closing tags inside attribute values diff --git a/packages/svelte/src/compiler/preprocess/index.js b/packages/svelte/src/compiler/preprocess/index.js index 187c73aff3..492368abd5 100644 --- a/packages/svelte/src/compiler/preprocess/index.js +++ b/packages/svelte/src/compiler/preprocess/index.js @@ -252,8 +252,10 @@ function stringify_tag_attributes(attributes) { return value; } -const regex_style_tags = /|([^]*?)<\/style>|\/>)/gi; -const regex_script_tags = /|([^]*?)<\/script>|\/>)/gi; +const regex_style_tags = + /|'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s])|\s+[^=>'"\/]+)*\s*)(?:\/>|>([\S\s]*?)<\/style>)/g; +const regex_script_tags = + /|'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s])|\s+[^=>'"\/]+)*\s*)(?:\/>|>([\S\s]*?)<\/script>)/g; /** * Calculate the updates required to process all instances of the specified tag. diff --git a/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/_config.js b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/_config.js new file mode 100644 index 0000000000..ce42ba8e5b --- /dev/null +++ b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/_config.js @@ -0,0 +1,6 @@ +export default { + preprocess: { + script: ({ attributes }) => + attributes.generics && attributes.generics.includes('>') ? { code: '' } : null + } +}; diff --git a/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/input.svelte b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/input.svelte new file mode 100644 index 0000000000..aa1a533ac7 --- /dev/null +++ b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/input.svelte @@ -0,0 +1,3 @@ + diff --git a/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/output.svelte b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/output.svelte new file mode 100644 index 0000000000..d4ade07a75 --- /dev/null +++ b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/output.svelte @@ -0,0 +1 @@ +