Skip to content

Commit 136aebd

Browse files
authored
fix: repair invalid raw html content during hydration (#8912)
When the HTML that is coming from raw html is invalid, the browser reshuffles things. Bail in that case to try to repair more often. Should help with withastro/astro#7557
1 parent 35221c8 commit 136aebd

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

.changeset/spicy-ladybugs-return.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: repair invalid raw html content during hydration

packages/svelte/.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ sites/svelte.dev/.vercel
1616
/test/**/expected*
1717
/test/**/_output
1818
/test/**/shards/*.test.js
19+
/test/hydration/samples/raw-repair/_after.html
1920
/types
2021
!rollup.config.js
2122
!vitest.config.js

packages/svelte/src/runtime/internal/dom.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,14 @@ export function claim_comment(nodes, data) {
777777
);
778778
}
779779

780-
function find_comment(nodes, text, start) {
780+
function get_comment_idx(nodes, text, start) {
781781
for (let i = start; i < nodes.length; i += 1) {
782782
const node = nodes[i];
783783
if (node.nodeType === 8 /* comment node */ && node.textContent.trim() === text) {
784784
return i;
785785
}
786786
}
787-
return nodes.length;
787+
return -1;
788788
}
789789

790790
/**
@@ -793,11 +793,12 @@ function find_comment(nodes, text, start) {
793793
*/
794794
export function claim_html_tag(nodes, is_svg) {
795795
// find html opening tag
796-
const start_index = find_comment(nodes, 'HTML_TAG_START', 0);
797-
const end_index = find_comment(nodes, 'HTML_TAG_END', start_index);
798-
if (start_index === end_index) {
796+
const start_index = get_comment_idx(nodes, 'HTML_TAG_START', 0);
797+
const end_index = get_comment_idx(nodes, 'HTML_TAG_END', start_index + 1);
798+
if (start_index === -1 || end_index === -1) {
799799
return new HtmlTagHydration(is_svg);
800800
}
801+
801802
init_claim_info(nodes);
802803
const html_tag_nodes = nodes.splice(start_index, end_index - start_index + 1);
803804
detach(html_tag_nodes[0]);
@@ -1048,17 +1049,13 @@ export class HtmlTag {
10481049
* @default false
10491050
*/
10501051
is_svg = false;
1051-
// parent for creating node
1052-
/** */
1052+
/** parent for creating node */
10531053
e = undefined;
1054-
// html tag nodes
1055-
/** */
1054+
/** html tag nodes */
10561055
n = undefined;
1057-
// target
1058-
/** */
1056+
/** target */
10591057
t = undefined;
1060-
// anchor
1061-
/** */
1058+
/** anchor */
10621059
a = undefined;
10631060
constructor(is_svg = false) {
10641061
this.is_svg = is_svg;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p><p>invalid</p></p>
2+
<p><p>invalid</p></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<p><!-- HTML_TAG_START --></p>
2+
<p>invalid</p>
3+
<!-- HTML_TAG_END -->
4+
<p></p>
5+
<p><!-- HTML_TAG_START --></p>
6+
<p>invalid</p>
7+
<!-- HTML_TAG_END -->
8+
<p></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let content;
3+
</script>
4+
5+
<p>{@html content}</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Inner from './inner.svelte';
3+
</script>
4+
5+
<Inner content="<p>invalid</p>" />
6+
7+
<p>{@html '<p>invalid</p>'}</p>

0 commit comments

Comments
 (0)