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

100% Statements 174/174
98.27% Branches 57/58
100% Functions 6/6
100% Lines 171/171

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 1722x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 3791x 3560x 3560x 231x 231x 231x 231x 3791x 3791x 3791x 5758x 5758x 5758x 5758x 5758x 5758x 5758x 5161x 5161x 5161x 5161x 4644x 4644x 4644x 5161x 5161x 1558x 1558x 1558x 1558x 1558x 1558x 5161x 5161x 5161x 5161x 5161x 5161x 5758x 5758x 5758x 5758x 5758x 4385x 3619x 3619x 3619x 2751x 2751x 2751x 2751x 3619x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 1634x 4385x 189x 4385x 1418x 1445x 27x 27x 4385x 5758x 5758x 10447x 10447x 10447x 6271x 10447x 4176x 1775x 1775x 1775x 4176x 4176x 556x 556x 4176x 3620x 93x 93x 3620x 3527x 3527x 3527x 3527x 3620x 4176x 10447x 5757x 5757x 2610x 2610x 5757x 5757x 5757x 5757x 188x 188x 5758x 2x 2x 2x 2x 2x 4176x 4176x 4175x 1219x 4175x 1161x 420x 420x 741x 1161x 156x 156x 585x 1161x 60x 60x 525x 1161x 26x 26x 499x 1161x 1x 1x 1161x 556x 556x 556x  
/** @import { Expression } from 'estree' */
/** @import { ExpressionTag, SvelteNode, Text } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../../types' */
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
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} initial
 * @param {boolean} is_element
 * @param {ComponentContext} context
 */
export function process_children(nodes, initial, is_element, { visit, state }) {
	const within_bound_contenteditable = state.metadata.bound_contenteditable;
 
	/** @typedef {Array<Text | ExpressionTag>} Sequence */
 
	/** @type {Sequence} */
	let sequence = [];
 
	let skipped = 0;
 
	/** @param {boolean} check */
	let expression = (check) => {
		if (skipped === 0) {
			return initial(check);
		}
 
		return b.call(
			'$.sibling',
			initial(false),
			(check || skipped !== 1) && b.literal(skipped),
			check && b.true
		);
	};
 
	/**
	 * @param {boolean} check
	 * @param {string} name
	 */
	function flush_node(check, name) {
		let init = expression(check);
		let id = init;
 
		if (id.type !== 'Identifier') {
			id = b.id(state.scope.generate(name));
			state.init.push(b.var(id, init));
		}
 
		expression = (check) => {
			return b.call(
				'$.sibling',
				id,
				(check || skipped !== 1) && b.literal(skipped),
				check && b.true
			);
		};
 
		skipped = 1;
 
		return id;
	}
 
	/**
	 * @param {Sequence} sequence
	 */
	function flush_sequence(sequence) {
		if (sequence.length === 1) {
			const node = sequence[0];
 
			if (node.type === 'Text') {
				skipped += 1;
				state.template.push(node.raw);
				return;
			}
		}
 
		// 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
		const needs_hydration_check = sequence.length === 1;
		const id = flush_node(needs_hydration_check, 'text');
 
		state.template.push(' ');
 
		const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
 
		const update = b.stmt(b.call('$.set_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(id, 'nodeValue'), value)));
		}
	}
 
	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 (is_static_element(node)) {
				visit(node, state);
				skipped += 1;
			} else {
				if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
					node.metadata.is_controlled = true;
					visit(node, state);
				} else {
					const id = flush_node(false, node.type === 'RegularElement' ? node.name : 'node');
 
					visit(node, { ...state, node: id });
				}
			}
		}
	}
 
	if (sequence.length > 0) {
		flush_sequence(sequence);
	}
 
	skipped -= 1;
 
	if (skipped > 0) {
		state.init.push(b.stmt(expression(false)));
	}
}
 
/**
 *
 * @param {SvelteNode} node
 */
function is_static_element(node) {
	if (node.type !== 'RegularElement') return false;
	if (node.fragment.metadata.dynamic) return false;
 
	for (const attribute of node.attributes) {
		if (attribute.type !== 'Attribute') {
			return false;
		}
 
		if (is_event_attribute(attribute)) {
			return false;
		}
 
		if (attribute.value !== true && !is_text_attribute(attribute)) {
			return false;
		}
 
		if (node.name === 'option' && attribute.name === 'value') {
			return false;
		}
 
		if (node.name.includes('-')) {
			return false; // TODO this feels unnecessary, but tests break
		}
	}
 
	return true;
}