diff --git a/.changeset/fruity-knives-ring.md b/.changeset/fruity-knives-ring.md new file mode 100644 index 000000000000..169945077674 --- /dev/null +++ b/.changeset/fruity-knives-ring.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: set Root start/end to null when fragment contains only whitespace diff --git a/packages/svelte/src/compiler/phases/1-parse/index.js b/packages/svelte/src/compiler/phases/1-parse/index.js index 8f7ef76be567..04ac9f12f92b 100644 --- a/packages/svelte/src/compiler/phases/1-parse/index.js +++ b/packages/svelte/src/compiler/phases/1-parse/index.js @@ -115,7 +115,10 @@ export class Parser { e.unexpected_eof(this.index); } - if (this.root.fragment.nodes.length) { + const has_content = this.root.fragment.nodes.some( + (node) => node.type !== 'Text' || node.data.trim().length > 0 + ); + if (has_content) { let start = /** @type {number} */ (this.root.fragment.nodes[0].start); while (regex_whitespace.test(template[start])) start += 1; diff --git a/packages/svelte/tests/parser-modern/samples/script-style-no-markup/input.svelte b/packages/svelte/tests/parser-modern/samples/script-style-no-markup/input.svelte new file mode 100644 index 000000000000..c4dc4d5271af --- /dev/null +++ b/packages/svelte/tests/parser-modern/samples/script-style-no-markup/input.svelte @@ -0,0 +1,6 @@ + + diff --git a/packages/svelte/tests/parser-modern/samples/script-style-no-markup/output.json b/packages/svelte/tests/parser-modern/samples/script-style-no-markup/output.json new file mode 100644 index 000000000000..33674de5041f --- /dev/null +++ b/packages/svelte/tests/parser-modern/samples/script-style-no-markup/output.json @@ -0,0 +1,112 @@ +{ + "css": { + "type": "StyleSheet", + "start": 54, + "end": 91, + "attributes": [], + "children": [ + { + "type": "Rule", + "prelude": { + "type": "SelectorList", + "start": 63, + "end": 66, + "children": [ + { + "type": "ComplexSelector", + "start": 63, + "end": 66, + "children": [ + { + "type": "RelativeSelector", + "combinator": null, + "selectors": [ + { + "type": "TypeSelector", + "name": "div", + "start": 63, + "end": 66 + } + ], + "start": 63, + "end": 66 + } + ] + } + ] + }, + "block": { + "type": "Block", + "start": 67, + "end": 82, + "children": [ + { + "type": "Declaration", + "start": 69, + "end": 79, + "property": "color", + "value": "red" + } + ] + }, + "start": 63, + "end": 82 + } + ], + "content": { + "start": 61, + "end": 83, + "styles": "\n\tdiv { color: red; }\n", + "comment": null + } + }, + "js": [], + "start": null, + "end": null, + "type": "Root", + "fragment": { + "type": "Fragment", + "nodes": [ + { + "type": "Text", + "start": 53, + "end": 54, + "raw": "\n", + "data": "\n" + } + ] + }, + "options": null, + "instance": { + "type": "Script", + "start": 0, + "end": 53, + "context": "default", + "content": { + "type": "Program", + "start": 8, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [], + "sourceType": "module", + "trailingComments": [ + { + "type": "Line", + "value": " script and style but no markup", + "start": 10, + "end": 43 + } + ] + }, + "attributes": [] + } +}