Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fruity-knives-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: set Root start/end to null when fragment contains only whitespace
5 changes: 4 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, less explicit/clear and more defensive:

if (this.root.fragment.nodes.length) {
	// ...

	if (start <= end) {
		this.root.start = start;
		this.root.end = end;
	} else {
		// @ts-ignore
		this.root.start = this.root.end = null;
	}
} else {
	// @ts-ignore
	this.root.start = this.root.end = null;
}

let start = /** @type {number} */ (this.root.fragment.nodes[0].start);
while (regex_whitespace.test(template[start])) start += 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
// script and style but no markup
</script>
<style>
div { color: red; }
</style>
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor Author

@ryanatkn ryanatkn Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before the fix:

	"js": [],
	"start": 54,
	"end": 53,
	"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": []
}
}
Loading