mirror of https://github.com/sveltejs/svelte
Merge 62a6a15b8a
into 50de8c5317
commit
380c85e551
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: throw runtime warning when template returns different html
|
@ -1,6 +1,28 @@
|
||||
/** @param {string} html */
|
||||
import { DEV } from 'esm-env';
|
||||
import * as w from '../warnings.js';
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
*/
|
||||
export function create_fragment_from_html(html) {
|
||||
var elem = document.createElement('template');
|
||||
elem.innerHTML = html;
|
||||
if (DEV) {
|
||||
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) {
|
||||
w.invalid_html_structure(remove_attributes_and_text_input, remove_attributes_and_text_output);
|
||||
}
|
||||
}
|
||||
|
||||
return elem.content;
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
mode: ['hydrate', 'client'],
|
||||
recover: true,
|
||||
needs_import_logs: true,
|
||||
test({ warnings, assert, variant }) {
|
||||
const expected_warnings = [
|
||||
'This html structure `<p></p><tr></tr>` would be corrected like this `<p></p>` by the browser making this component impossible to hydrate properly'
|
||||
];
|
||||
if (variant === 'hydrate') {
|
||||
expected_warnings.push(
|
||||
'Hydration failed because the initial UI does not match what was rendered on the server'
|
||||
);
|
||||
}
|
||||
assert.deepEqual(warnings, expected_warnings);
|
||||
}
|
||||
});
|
@ -0,0 +1,3 @@
|
||||
<svelte:options runes />
|
||||
<p></p>
|
||||
<tr></tr>
|
Loading…
Reference in new issue