From b01cac954e98f643c7ec89b7823e4690b1f6dbc8 Mon Sep 17 00:00:00 2001 From: Yang Pan Date: Sun, 8 Dec 2024 02:46:46 -0500 Subject: [PATCH 1/6] test: add sample for unquoted attributes --- .../samples/attribute-unquoted/input.svelte | 3 +- .../samples/attribute-unquoted/output.json | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte index 4bab0df72f3e..959042384e69 100644 --- a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte +++ b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte @@ -1 +1,2 @@ -
\ No newline at end of file +
+home \ No newline at end of file diff --git a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json index 5df4d66ab668..c73f4d5c4d6a 100644 --- a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json +++ b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json @@ -2,7 +2,7 @@ "html": { "type": "Fragment", "start": 0, - "end": 21, + "end": 40, "children": [ { "type": "Element", @@ -27,6 +27,45 @@ } ], "children": [] + }, + { + "type": "Text", + "start": 21, + "end": 22, + "raw": "\n", + "data": "\n" + }, + { + "type": "Element", + "start": 22, + "end": 40, + "name": "a", + "attributes": [ + { + "type": "Attribute", + "start": 25, + "end": 31, + "name": "href", + "value": [ + { + "start": 30, + "end": 31, + "type": "Text", + "raw": "/", + "data": "/" + } + ] + } + ], + "children": [ + { + "type": "Text", + "start": 32, + "end": 36, + "raw": "home", + "data": "home" + } + ] } ] } From 2ff87421e5d595ce88971b328455f4293dd4b9f3 Mon Sep 17 00:00:00 2001 From: Yang Pan Date: Sun, 8 Dec 2024 02:49:05 -0500 Subject: [PATCH 2/6] fix: handle unquoted slash in attributes --- .../compiler/phases/1-parse/state/element.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/svelte/src/compiler/phases/1-parse/state/element.js b/packages/svelte/src/compiler/phases/1-parse/state/element.js index 45350bb1aec5..2106caf9dc90 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/element.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/element.js @@ -504,8 +504,24 @@ function read_attribute(parser) { let value = true; if (parser.eat('=')) { parser.allow_whitespace(); - value = read_attribute_value(parser); - end = parser.index; + + if (parser.template[parser.index] === '/') { + const char_start = parser.index; + parser.index++; // consume '/' + value = [ + { + start: char_start, + end: char_start + 1, + type: 'Text', + raw: '/', + data: '/' + } + ]; + end = parser.index; + } else { + value = read_attribute_value(parser); + end = parser.index; + } } else if (parser.match_regex(regex_starts_with_quote_characters)) { e.expected_token(parser.index, '='); } From 8941b77e700fd63de1f48d3fa103627638df1006 Mon Sep 17 00:00:00 2001 From: Yang Pan Date: Sun, 8 Dec 2024 03:00:09 -0500 Subject: [PATCH 3/6] docs: allow unquoted slash in attributes --- .changeset/long-boxes-flow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/long-boxes-flow.md diff --git a/.changeset/long-boxes-flow.md b/.changeset/long-boxes-flow.md new file mode 100644 index 000000000000..b5b22a00e2f7 --- /dev/null +++ b/.changeset/long-boxes-flow.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +Allow unquoted slash in attributes From 863232e56227c239af46f2d42b56234ca5a2394a Mon Sep 17 00:00:00 2001 From: Yang Pan Date: Thu, 12 Dec 2024 16:53:59 +0900 Subject: [PATCH 4/6] test: add additional sample for unquoted href attributes --- .../samples/attribute-unquoted/input.svelte | 3 +- .../samples/attribute-unquoted/output.json | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte index 959042384e69..527d6eebf104 100644 --- a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte +++ b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/input.svelte @@ -1,2 +1,3 @@
-home \ No newline at end of file +home +home \ No newline at end of file diff --git a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json index c73f4d5c4d6a..ab2912a2c019 100644 --- a/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json +++ b/packages/svelte/tests/parser-legacy/samples/attribute-unquoted/output.json @@ -2,7 +2,7 @@ "html": { "type": "Fragment", "start": 0, - "end": 40, + "end": 62, "children": [ { "type": "Element", @@ -66,6 +66,45 @@ "data": "home" } ] + }, + { + "type": "Text", + "start": 40, + "end": 41, + "raw": "\n", + "data": "\n" + }, + { + "type": "Element", + "start": 41, + "end": 62, + "name": "a", + "attributes": [ + { + "type": "Attribute", + "start": 44, + "end": 53, + "name": "href", + "value": [ + { + "start": 49, + "end": 53, + "type": "Text", + "raw": "/foo", + "data": "/foo" + } + ] + } + ], + "children": [ + { + "type": "Text", + "start": 54, + "end": 58, + "raw": "home", + "data": "home" + } + ] } ] } From 3d990a02eff74bd9fc42c961e85604ab3793f732 Mon Sep 17 00:00:00 2001 From: Yang Pan Date: Thu, 12 Dec 2024 16:54:26 +0900 Subject: [PATCH 5/6] fix: improve handling of self-closing tags with unquoted attributes --- packages/svelte/src/compiler/phases/1-parse/state/element.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/phases/1-parse/state/element.js b/packages/svelte/src/compiler/phases/1-parse/state/element.js index 2106caf9dc90..cd5cdd3e6e2c 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/element.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/element.js @@ -505,7 +505,7 @@ function read_attribute(parser) { if (parser.eat('=')) { parser.allow_whitespace(); - if (parser.template[parser.index] === '/') { + if (parser.template[parser.index] === '/' && parser.template[parser.index + 1] === '>') { const char_start = parser.index; parser.index++; // consume '/' value = [ From ea9d4cfc9450b54f9495cfe3b58f9c693016031e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 12 Dec 2024 14:39:02 -0500 Subject: [PATCH 6/6] Update .changeset/long-boxes-flow.md --- .changeset/long-boxes-flow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/long-boxes-flow.md b/.changeset/long-boxes-flow.md index b5b22a00e2f7..d249354b6769 100644 --- a/.changeset/long-boxes-flow.md +++ b/.changeset/long-boxes-flow.md @@ -2,4 +2,4 @@ 'svelte': patch --- -Allow unquoted slash in attributes +fix: allow unquoted slash in attributes