-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix: throw runtime error when template returns different html #15524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: throw runtime error when template returns different html |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
/** @param {string} html */ | ||
export function create_fragment_from_html(html) { | ||
import { DEV } from 'esm-env'; | ||
import * as e from '../errors.js'; | ||
|
||
/** | ||
* @param {string} html | ||
* @param {boolean} [check_structure] | ||
*/ | ||
export function create_fragment_from_html(html, check_structure = true) { | ||
var elem = document.createElement('template'); | ||
elem.innerHTML = html; | ||
if (DEV && check_structure) { | ||
let replace_comments = html.replaceAll('<!>', '<!---->'); | ||
let remove_attributes_and_text_input = replace_comments | ||
// we remove every attribute since the template automatically adds ="" after boolean attributes | ||
.replace(/<([a-z0-9]+)(\s+[^>]+?)?>/g, '<$1>') | ||
|
||
// we remove the text within the elements because the template change & to & (and similar) | ||
.replace(/>([^<>]*)/g, '>'); | ||
let remove_attributes_and_text_output = elem.innerHTML | ||
// we remove every attribute since the template automatically adds ="" after boolean attributes | ||
.replace(/<([a-z0-9]+)(\s+[^>]+?)?>/g, '<$1>') | ||
|
||
// we remove the text within the elements because the template change & to & (and similar) | ||
.replace(/>([^<>]*)/g, '>'); | ||
if (remove_attributes_and_text_input !== remove_attributes_and_text_output) { | ||
e.invalid_html_structure(remove_attributes_and_text_input, remove_attributes_and_text_output); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The HTML snippets could become arbitrarily large (especially if we enabled this for The alternative approach of checking the structure manually could help here since it means we can point to the specific element that's being mishandled. (Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I though about that but also thought a diffing algorithm would be too much and not showing complete information would be not really helpful. I can try to put up a PR for the alternative method tomorrow to check if it could yield better results |
||
} | ||
} | ||
|
||
return elem.content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
mode: ['client', 'hydrate'], | ||
recover: true, | ||
runtime_error: 'invalid_html_structure' | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<svelte:options runes /> | ||
<p></p> | ||
<tr></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you elaborate on why we're not doing this for
{@html ...}
? It may survive hydration but it's still delivering an unexpected outcome. Feels like we should just do it in all casesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It mainly goes down to self-closing-but-not-really tags.
This will yield an error (I suppose if we switch to a warning it could be fine tho)