pull/16100/head
Rich Harris 3 months ago
parent 57b13d363a
commit c2a62070ee

@ -331,7 +331,7 @@ export function RegularElement(node, context) {
trimmed.some((node) => node.type === 'ExpressionTag'); trimmed.some((node) => node.type === 'ExpressionTag');
if (use_text_content) { if (use_text_content) {
const { value } = build_template_chunk(trimmed, context.visit, child_state); const { value } = build_template_chunk(trimmed, context, child_state);
const empty_string = value.type === 'Literal' && value.value === ''; const empty_string = value.type === 'Literal' && value.value === '';
if (!empty_string) { if (!empty_string) {

@ -10,8 +10,7 @@ import { build_template_chunk } from './shared/utils.js';
export function TitleElement(node, context) { export function TitleElement(node, context) {
const { has_state, value } = build_template_chunk( const { has_state, value } = build_template_chunk(
/** @type {any} */ (node.fragment.nodes), /** @type {any} */ (node.fragment.nodes),
context.visit, context
context.state
); );
const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value)); const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value));

@ -129,7 +129,7 @@ export function build_attribute_value(value, context, memoize = (value) => value
}; };
} }
return build_template_chunk(value, context.visit, context.state, memoize); return build_template_chunk(value, context, context.state, memoize);
} }
/** /**

@ -16,8 +16,8 @@ import { build_template_chunk } from './utils.js';
* @param {boolean} is_element * @param {boolean} is_element
* @param {ComponentContext} context * @param {ComponentContext} context
*/ */
export function process_children(nodes, initial, is_element, { visit, state }) { export function process_children(nodes, initial, is_element, context) {
const within_bound_contenteditable = state.metadata.bound_contenteditable; const within_bound_contenteditable = context.state.metadata.bound_contenteditable;
let prev = initial; let prev = initial;
let skipped = 0; let skipped = 0;
@ -48,8 +48,8 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
let id = expression; let id = expression;
if (id.type !== 'Identifier') { if (id.type !== 'Identifier') {
id = b.id(state.scope.generate(name)); id = b.id(context.state.scope.generate(name));
state.init.push(b.var(id, expression)); context.state.init.push(b.var(id, expression));
} }
prev = () => id; prev = () => id;
@ -64,13 +64,13 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
function flush_sequence(sequence) { function flush_sequence(sequence) {
if (sequence.every((node) => node.type === 'Text')) { if (sequence.every((node) => node.type === 'Text')) {
skipped += 1; skipped += 1;
state.template.push_text(sequence); context.state.template.push_text(sequence);
return; return;
} }
state.template.push_text([{ type: 'Text', data: ' ', raw: ' ', start: -1, end: -1 }]); context.state.template.push_text([{ type: 'Text', data: ' ', raw: ' ', start: -1, end: -1 }]);
const { has_state, value } = build_template_chunk(sequence, visit, state); const { has_state, value } = build_template_chunk(sequence, context);
// if this is a standalone `{expression}`, make sure we handle the case where // if this is a standalone `{expression}`, make sure we handle the case where
// no text node was created because the expression was empty during SSR // no text node was created because the expression was empty during SSR
@ -80,9 +80,9 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
const update = b.stmt(b.call('$.set_text', id, value)); const update = b.stmt(b.call('$.set_text', id, value));
if (has_state && !within_bound_contenteditable) { if (has_state && !within_bound_contenteditable) {
state.update.push(update); context.state.update.push(update);
} else { } else {
state.init.push(b.stmt(b.assignment('=', b.member(id, 'nodeValue'), value))); context.state.init.push(b.stmt(b.assignment('=', b.member(id, 'nodeValue'), value)));
} }
} }
@ -95,18 +95,18 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
sequence = []; sequence = [];
} }
let child_state = state; let child_state = context.state;
if (is_static_element(node, state)) { if (is_static_element(node, context.state)) {
skipped += 1; skipped += 1;
} else if (node.type === 'EachBlock' && nodes.length === 1 && is_element) { } else if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
node.metadata.is_controlled = true; node.metadata.is_controlled = true;
} else { } else {
const id = flush_node(false, node.type === 'RegularElement' ? node.name : 'node'); const id = flush_node(false, node.type === 'RegularElement' ? node.name : 'node');
child_state = { ...state, node: id }; child_state = { ...context.state, node: id };
} }
visit(node, child_state); context.visit(node, child_state);
} }
} }
@ -118,7 +118,7 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
// traverse to the last (n - 1) one when hydrating // traverse to the last (n - 1) one when hydrating
if (skipped > 1) { if (skipped > 1) {
skipped -= 1; skipped -= 1;
state.init.push(b.stmt(b.call('$.next', skipped !== 1 && b.literal(skipped)))); context.state.init.push(b.stmt(b.call('$.next', skipped !== 1 && b.literal(skipped))));
} }
} }

@ -31,15 +31,15 @@ export function get_expression_id(expressions, value) {
/** /**
* @param {Array<AST.Text | AST.ExpressionTag>} values * @param {Array<AST.Text | AST.ExpressionTag>} values
* @param {(node: AST.SvelteNode, state: any) => any} visit * @param {ComponentContext} context
* @param {ComponentClientTransformState} state * @param {ComponentClientTransformState} state
* @param {(value: Expression, metadata: ExpressionMetadata) => Expression} memoize * @param {(value: Expression, metadata: ExpressionMetadata) => Expression} memoize
* @returns {{ value: Expression, has_state: boolean }} * @returns {{ value: Expression, has_state: boolean }}
*/ */
export function build_template_chunk( export function build_template_chunk(
values, values,
visit, context,
state, state = context.state,
memoize = (value, metadata) => memoize = (value, metadata) =>
metadata.has_call ? get_expression_id(state.expressions, value) : value metadata.has_call ? get_expression_id(state.expressions, value) : value
) { ) {
@ -66,7 +66,7 @@ export function build_template_chunk(
state.scope.get('undefined') state.scope.get('undefined')
) { ) {
let value = memoize( let value = memoize(
/** @type {Expression} */ (visit(node.expression, state)), /** @type {Expression} */ (context.visit(node.expression, state)),
node.metadata.expression node.metadata.expression
); );

Loading…
Cancel
Save