From 986fc32d0dcbf03e25d5a670099706b4d1eee881 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 16 Nov 2023 14:00:59 +0100 Subject: [PATCH] fix: handle closing tags inside attribute values 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 000000000000..0f745d4b48fd --- /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 187c73aff323..492368abd533 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 000000000000..ce42ba8e5ba4 --- /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 000000000000..aa1a533ac775 --- /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 000000000000..d4ade07a7560 --- /dev/null +++ b/packages/svelte/test/preprocess/samples/attributes-with-closing-tag/output.svelte @@ -0,0 +1 @@ +