Skip to content

Commit 356dd05

Browse files
committed
move push_element and pop_element into new dev.js file
1 parent 6b3618e commit 356dd05

File tree

2 files changed

+24
-122
lines changed

2 files changed

+24
-122
lines changed

packages/svelte/src/internal/server/index.js

Lines changed: 14 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
11
import { is_promise, noop } from '../shared/utils.js';
22
import { subscribe_to_store } from '../../store/utils.js';
3-
import {
4-
UNINITIALIZED,
5-
DOMBooleanAttributes,
6-
RawTextElements,
7-
disallowed_paragraph_contents,
8-
interactive_elements,
9-
is_tag_valid_with_parent
10-
} from '../../constants.js';
3+
import { UNINITIALIZED, DOMBooleanAttributes, RawTextElements } from '../../constants.js';
114
import { escape_html } from '../../escaping.js';
125
import { DEV } from 'esm-env';
136
import { current_component, pop, push } from './context.js';
147
import { BLOCK_CLOSE, BLOCK_OPEN } from './hydration.js';
158
import { validate_store } from '../shared/validate.js';
169

17-
/**
18-
* @typedef {{
19-
* tag: string;
20-
* parent: null | Element;
21-
* file: string;
22-
* }} Element
23-
*/
24-
2510
/**
2611
* @typedef {{
2712
* head: string;
2813
* html: string;
2914
* }} RenderOutput
3015
*/
3116

32-
/**
33-
* @typedef {{
34-
* out: string;
35-
* anchor: number;
36-
* head: {
37-
* title: string;
38-
* out: string;
39-
* anchor: number;
40-
* };
41-
* }} Payload
42-
*/
43-
4417
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
4518
// https://infra.spec.whatwg.org/#noncharacter
4619
const INVALID_ATTR_NAME_CHAR_REGEX =
@@ -65,19 +38,14 @@ export const VoidElements = new Set([
6538
'wbr'
6639
]);
6740

68-
/**
69-
* @type {Element | null}
70-
*/
71-
let current_element = null;
72-
73-
/** @returns {Payload} */
41+
/** @returns {import('#server').Payload} */
7442
function create_payload() {
7543
return { out: '', head: { title: '', out: '', anchor: 0 }, anchor: 0 };
7644
}
7745

7846
/**
79-
* @param {Payload} to_copy
80-
* @returns {Payload}
47+
* @param {import('#server').Payload} to_copy
48+
* @returns {import('#server').Payload}
8149
*/
8250
export function copy_payload(to_copy) {
8351
return {
@@ -88,8 +56,8 @@ export function copy_payload(to_copy) {
8856

8957
/**
9058
* Assigns second payload to first
91-
* @param {Payload} p1
92-
* @param {Payload} p2
59+
* @param {import('#server').Payload} p1
60+
* @param {import('#server').Payload} p2
9361
* @returns {void}
9462
*/
9563
export function assign_payload(p1, p2) {
@@ -99,85 +67,7 @@ export function assign_payload(p1, p2) {
9967
}
10068

10169
/**
102-
* @param {Payload} payload
103-
* @param {string} message
104-
*/
105-
function error_on_client(payload, message) {
106-
message =
107-
`Svelte SSR validation error:\n\n${message}\n\n` +
108-
'Ensure your components render valid HTML as the browser will try to repair invalid HTML, ' +
109-
'which may result in content being shifted around and will likely result in a hydration mismatch.';
110-
// eslint-disable-next-line no-console
111-
console.error(message);
112-
payload.head.out += `<script>console.error(\`${message}\`)</script>`;
113-
}
114-
115-
/**
116-
* @param {string} file
117-
*/
118-
function print_file(file) {
119-
return file ? `(${file})` : '';
120-
}
121-
122-
/**
123-
* @param {string} tag
124-
* @param {Payload} payload
125-
*/
126-
export function push_element(tag, payload) {
127-
var file;
128-
if (DEV) {
129-
if (current_component !== null) {
130-
const filename = current_component.function.filename;
131-
if (filename) {
132-
file = filename.split('/').at(-1);
133-
}
134-
}
135-
if (current_element !== null && !is_tag_valid_with_parent(tag, current_element.tag)) {
136-
error_on_client(
137-
payload,
138-
`<${tag}> ${print_file(file)} is not a valid child element of <${current_element.tag}> ${print_file(current_element.file)}`
139-
);
140-
}
141-
if (interactive_elements.has(tag)) {
142-
let element = current_element;
143-
while (element !== null) {
144-
if (interactive_elements.has(element.tag)) {
145-
error_on_client(
146-
payload,
147-
`<${tag}> ${print_file(file)} is not a valid child element of <${element.tag}> ${print_file(element.file)}`
148-
);
149-
}
150-
element = element.parent;
151-
}
152-
}
153-
if (disallowed_paragraph_contents.includes(tag)) {
154-
let element = current_element;
155-
while (element !== null) {
156-
if (element.tag === 'p') {
157-
error_on_client(
158-
payload,
159-
`<${tag}> ${print_file(file)} is not a valid child element of <p> ${print_file(element.file)}`
160-
);
161-
}
162-
element = element.parent;
163-
}
164-
}
165-
}
166-
current_element = {
167-
tag,
168-
parent: current_element,
169-
file
170-
};
171-
}
172-
173-
export function pop_element() {
174-
if (current_element !== null) {
175-
current_element = current_element.parent;
176-
}
177-
}
178-
179-
/**
180-
* @param {Payload} payload
70+
* @param {import('#server').Payload} payload
18171
* @param {string} tag
18272
* @param {() => void} attributes_fn
18373
* @param {() => void} children_fn
@@ -241,8 +131,8 @@ export function render(component, options) {
241131
}
242132

243133
/**
244-
* @param {Payload} payload
245-
* @param {(head_payload: Payload['head']) => void} fn
134+
* @param {import('#server').Payload} payload
135+
* @param {(head_payload: import('#server').Payload['head']) => void} fn
246136
* @returns {void}
247137
*/
248138
export function head(payload, fn) {
@@ -266,7 +156,7 @@ export function attr(name, value, boolean) {
266156
}
267157

268158
/**
269-
* @param {Payload} payload
159+
* @param {import('#server').Payload} payload
270160
* @param {boolean} is_html
271161
* @param {Record<string, string>} props
272162
* @param {() => void} component
@@ -524,8 +414,8 @@ export async function value_or_fallback_async(value, fallback) {
524414
}
525415

526416
/**
527-
* @param {Payload} payload
528-
* @param {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn
417+
* @param {import('#server').Payload} payload
418+
* @param {void | ((payload: import('#server').Payload, props: Record<string, unknown>) => void)} slot_fn
529419
* @param {Record<string, unknown>} slot_props
530420
* @param {null | (() => void)} fallback_fn
531421
* @returns {void}
@@ -648,6 +538,8 @@ export function once(get_value) {
648538

649539
export { push, pop } from './context.js';
650540

541+
export { push_element, pop_element } from './dev.js';
542+
651543
export {
652544
add_snippet_symbol,
653545
validate_component,

packages/svelte/src/internal/server/types.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ export interface Component {
1010
*/
1111
function?: any;
1212
}
13+
14+
export interface Payload {
15+
out: string;
16+
anchor: number;
17+
head: {
18+
title: string;
19+
out: string;
20+
anchor: number;
21+
};
22+
}

0 commit comments

Comments
 (0)