fix: correctly validate head snippets on the server

pull/15755/head
paoloricciuti 1 year ago
parent 3153384928
commit 587c1b1b68

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: correctly validate head snippets on the server

@ -6,7 +6,7 @@ import {
} from '../../html-tree-validation.js';
import { current_component } from './context.js';
import { invalid_snippet_arguments } from '../shared/errors.js';
import { Payload } from './payload.js';
import { HeadPayload, Payload } from './payload.js';
/**
* @typedef {{
@ -105,7 +105,11 @@ export function pop_element() {
* @param {Payload} payload
*/
export function validate_snippet_args(payload) {
if (typeof payload !== 'object' || !(payload instanceof Payload)) {
if (
typeof payload !== 'object' ||
// for some reason typescript consider the type of payload as never after the first instanceof
!(payload instanceof Payload || /** @type {any} */ (payload) instanceof HeadPayload)
) {
invalid_snippet_arguments();
}
}

@ -1,16 +1,36 @@
export class HeadPayload {
/** @type {Set<{ hash: string; code: string }>} */
css = new Set();
out = '';
uid = () => '';
title = '';
constructor(css = new Set(), out = '', title = '', uid = () => '') {
this.css = css;
this.out = out;
this.title = title;
this.uid = uid;
}
clone() {
const payload = new HeadPayload();
payload.out = this.out;
payload.css = new Set(this.css);
payload.title = this.title;
payload.uid = this.uid;
return payload;
}
}
export class Payload {
/** @type {Set<{ hash: string; code: string }>} */
css = new Set();
out = '';
uid = () => '';
head = {
/** @type {Set<{ hash: string; code: string }>} */
css: new Set(),
title: '',
out: '',
uid: () => ''
};
head = new HeadPayload();
constructor(id_prefix = '') {
this.uid = props_id_generator(id_prefix);
@ -30,12 +50,7 @@ export function copy_payload({ out, css, head, uid }) {
payload.css = new Set(css);
payload.uid = uid;
payload.head = {
title: head.title,
out: head.out,
css: new Set(head.css),
uid: head.uid
};
payload.head = head.clone();
return payload;
}

@ -0,0 +1,11 @@
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
mode: ['server'],
async test({ errors, assert }) {
assert.equal(errors, []);
}
});

@ -0,0 +1,7 @@
{#snippet head()}
<title>Cool</title>
{/snippet}
<svelte:head>
{@render head()}
</svelte:head>
Loading…
Cancel
Save