All files / src/compiler/phases/3-transform/client/visitors/shared fragment.js

97.43% Statements 152/156
98.03% Branches 50/51
100% Functions 7/7
97.4% Lines 150/154

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 1552x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5935x 5935x 5935x 5935x 5935x 5935x 5935x 5935x 5935x 5935x 5935x 4563x 3775x 3775x 3775x 2868x 2868x 2868x 2868x 2868x 907x 907x 907x 907x 907x 907x 907x 907x 907x 3775x 145x 3775x 704x 756x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 907x 907x 907x 4563x 788x 788x 788x 788x 788x 788x 788x 788x 788x 42x 788x 706x 746x 40x 40x 788x 788x 788x 788x 4563x 5935x 5935x 10640x 10640x 10640x 6487x 10640x 4153x 1764x 1764x 1764x 4153x 4153x 4153x 4153x 4153x 4153x         4153x 4153x 92x 92x 4153x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4061x 4153x 4153x 10640x 10639x 5933x 2799x 2799x 2799x 25x 25x 2799x 2799x 2799x 5935x 2x 2x 2x 2x 2x 2x 5756x 5756x 5756x 5756x 5241x 5241x 5241x 5241x 5756x 5756x  
/** @import { Expression } from 'estree' */
/** @import { ExpressionTag, SvelteNode, Text } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../../types' */
import * as b from '../../../../../utils/builders.js';
import { build_template_literal, build_update } from './utils.js';
 
/**
 * Processes an array of template nodes, joining sibling text/expression nodes
 * (e.g. `{a} b {c}`) into a single update function. Along the way it creates
 * corresponding template node references these updates are applied to.
 * @param {SvelteNode[]} nodes
 * @param {(is_text: boolean) => Expression} expression
 * @param {boolean} is_element
 * @param {ComponentContext} context
 */
export function process_children(nodes, expression, is_element, { visit, state }) {
	const within_bound_contenteditable = state.metadata.bound_contenteditable;
 
	/** @typedef {Array<Text | ExpressionTag>} Sequence */
 
	/** @type {Sequence} */
	let sequence = [];
 
	/**
	 * @param {Sequence} sequence
	 */
	function flush_sequence(sequence) {
		if (sequence.length === 1) {
			const node = sequence[0];
 
			if (node.type === 'Text') {
				let prev = expression;
				expression = () => b.call('$.sibling', prev(true));
				state.template.push(node.raw);
				return;
			}
 
			state.template.push(' ');
 
			const text_id = get_node_id(expression(true), state, 'text');
 
			const update = b.stmt(
				b.call('$.set_text', text_id, /** @type {Expression} */ (visit(node.expression, state)))
			);
 
			if (node.metadata.expression.has_call && !within_bound_contenteditable) {
				state.init.push(build_update(update));
			} else if (node.metadata.expression.has_state && !within_bound_contenteditable) {
				state.update.push(update);
			} else {
				state.init.push(
					b.stmt(
						b.assignment(
							'=',
							b.member(text_id, b.id('nodeValue')),
							/** @type {Expression} */ (visit(node.expression))
						)
					)
				);
			}
 
			expression = (is_text) =>
				is_text ? b.call('$.sibling', text_id, b.true) : b.call('$.sibling', text_id);
		} else {
			const text_id = get_node_id(expression(true), state, 'text');
 
			state.template.push(' ');
 
			const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
 
			const update = b.stmt(b.call('$.set_text', text_id, value));
 
			if (has_call && !within_bound_contenteditable) {
				state.init.push(build_update(update));
			} else if (has_state && !within_bound_contenteditable) {
				state.update.push(update);
			} else {
				state.init.push(b.stmt(b.assignment('=', b.member(text_id, b.id('nodeValue')), value)));
			}
 
			expression = (is_text) =>
				is_text ? b.call('$.sibling', text_id, b.true) : b.call('$.sibling', text_id);
		}
	}
 
	for (let i = 0; i < nodes.length; i += 1) {
		const node = nodes[i];
 
		if (node.type === 'Text' || node.type === 'ExpressionTag') {
			sequence.push(node);
		} else {
			if (sequence.length > 0) {
				flush_sequence(sequence);
				sequence = [];
			}
 
			if (
				node.type === 'SvelteHead' ||
				node.type === 'TitleElement' ||
				node.type === 'SnippetBlock'
			) {
				// These nodes do not contribute to the sibling/child tree
				// TODO what about e.g. ConstTag and all the other things that
				// get hoisted inside clean_nodes?
				visit(node, state);
			} else {
				if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
					node.metadata.is_controlled = true;
					visit(node, state);
				} else {
					const id = get_node_id(
						expression(false),
						state,
						node.type === 'RegularElement' ? node.name : 'node'
					);
 
					expression = (is_text) =>
						is_text ? b.call('$.sibling', id, b.true) : b.call('$.sibling', id);
 
					visit(node, {
						...state,
						node: id
					});
				}
			}
		}
	}
 
	if (sequence.length > 0) {
		// if the final item in a fragment is static text,
		// we need to force `hydrate_node` to advance
		if (sequence.length === 1 && sequence[0].type === 'Text' && nodes.length > 1) {
			state.init.push(b.stmt(b.call('$.next')));
		}
 
		flush_sequence(sequence);
	}
}
 
/**
 * @param {Expression} expression
 * @param {ComponentClientTransformState} state
 * @param {string} name
 */
function get_node_id(expression, state, name) {
	let id = expression;
 
	if (id.type !== 'Identifier') {
		id = b.id(state.scope.generate(name));
 
		state.init.push(b.var(id, expression));
	}
	return id;
}