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

100% Statements 159/159
100% Branches 58/58
100% Functions 6/6
100% Lines 156/156

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 1572x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5762x 5762x 5762x 5762x 5762x 5762x 5762x 5762x 5762x 5762x 5353x 3563x 3563x 1790x 1790x 1790x 1790x 5353x 5353x 5353x 5353x 5762x 5762x 5762x 5762x 5762x 5762x 5165x 5165x 5165x 5165x 4647x 4647x 4647x 5165x 5165x 5165x 5165x 5165x 5165x 5762x 5762x 5762x 5762x 5762x 4387x 2752x 2752x 2752x 2752x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 1635x 4387x 189x 4387x 1419x 1446x 27x 27x 4387x 5762x 5762x 10452x 6273x 10452x 4179x 1776x 1776x 1776x 4179x 4179x 4179x 4179x 556x 4179x 93x 3623x 3530x 3530x 3530x 4179x 4179x 4179x 10452x 5761x 5761x 2611x 2611x 5761x 5761x 5761x 5761x 188x 188x 188x 5762x 2x 2x 2x 2x 2x 4179x 4179x 4178x 1220x 4178x 1162x 420x 420x 742x 1162x 156x 156x 586x 1162x 60x 60x 526x 1162x 26x 26x 500x 1162x 2x 2x 1162x 556x 556x 556x  
/** @import { Expression } from 'estree' */
/** @import { ExpressionTag, SvelteNode, Text } from '#compiler' */
/** @import { 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;
	let prev = initial;
	let skipped = 0;
 
	/** @typedef {Array<Text | ExpressionTag>} Sequence */
	/** @type {Sequence} */
	let sequence = [];
 
	/** @param {boolean} is_text */
	function get_node(is_text) {
		if (skipped === 0) {
			return prev(is_text);
		}
 
		return b.call(
			'$.sibling',
			prev(false),
			(is_text || skipped !== 1) && b.literal(skipped),
			is_text && b.true
		);
	}
 
	/**
	 * @param {boolean} is_text
	 * @param {string} name
	 */
	function flush_node(is_text, name) {
		const expression = get_node(is_text);
		let id = expression;
 
		if (id.type !== 'Identifier') {
			id = b.id(state.scope.generate(name));
			state.init.push(b.var(id, expression));
		}
 
		prev = () => id;
		skipped = 1; // the next node is `$.sibling(id)`
 
		return id;
	}
 
	/**
	 * @param {Sequence} sequence
	 */
	function flush_sequence(sequence) {
		if (sequence.length === 1 && sequence[0].type === 'Text') {
			skipped += 1;
			state.template.push(sequence[0].raw);
			return;
		}
 
		state.template.push(' ');
 
		const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
 
		// 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 is_text = sequence.length === 1;
		const id = flush_node(is_text, 'text');
 
		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 (const node of nodes) {
		if (node.type === 'Text' || node.type === 'ExpressionTag') {
			sequence.push(node);
		} else {
			if (sequence.length > 0) {
				flush_sequence(sequence);
				sequence = [];
			}
 
			let child_state = state;
 
			if (is_static_element(node)) {
				skipped += 1;
			} else if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
				node.metadata.is_controlled = true;
			} else {
				const id = flush_node(false, node.type === 'RegularElement' ? node.name : 'node');
				child_state = { ...state, node: id };
			}
 
			visit(node, child_state);
		}
	}
 
	if (sequence.length > 0) {
		flush_sequence(sequence);
	}
 
	// if there are trailing static text nodes/elements,
	// traverse to the last (n - 1) one when hydrating
	if (skipped > 1) {
		skipped -= 1;
		state.init.push(b.stmt(get_node(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; // we're setting all attributes on custom elements through properties
		}
	}
 
	return true;
}