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

96.4% Statements 268/278
90.66% Branches 68/75
100% Functions 6/6
96.33% Lines 263/273

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 2742x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3822x 3822x 3822x 3822x 3822x 3822x 3822x 3822x 3822x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 1x 33x 23x 33x 10x 10x 34x 3822x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3822x 3822x 3822x 3822x 3822x 3822x 3822x 24x 24x 24x 24x 24x 24x 2x 23x 18x 21x 4x 4x 24x 3822x 2x 2x 2x 2x 2x 2x 1015x 8x 8x 1007x 1015x 957x 957x 957x 245x 245x 712x 712x 712x 712x 712x 712x 712x 50x 50x 50x 2x 2x 2x 2x 2x 2x 2x 1082x 1082x 1082x 974x 1082x 974x 974x 108x 108x 108x 2x 2x 2x 2x 2x 2x 266x 266x 266x 266x 266x 2x 2x 2x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 2x 2x 2x 2x 2x 2x 2x 2x 700x 700x 700x 700x 700x 700x 683x 683x 683x 683x 683x 251x 251x 251x 166x 166x 251x 251x 201x 171x 171x 171x 171x 201x     201x 201x 201x 201x 201x 201x 201x 251x 50x     50x 50x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 432x 682x 4x 4x 432x 432x 432x 432x 432x 432x 432x 432x 682x   682x   432x 432x   432x       432x 432x 432x 699x 17x 17x 17x 17x 17x 17x 17x 449x 449x 449x 700x 700x 700x 700x 700x 700x 700x 700x 435x 700x 20x 20x 699x 429x 429x 700x  
/** @import { Expression, Identifier } from 'estree' */
/** @import { Attribute, ClassDirective, DelegatedEvent, ExpressionMetadata, ExpressionTag, Namespace, OnDirective, RegularElement, StyleDirective, SvelteElement, SvelteNode } from '#compiler' */
/** @import { ComponentContext } from '../../types' */
import {
	is_capture_event,
	is_passive_event,
	normalize_attribute
} from '../../../../../../utils.js';
import { get_attribute_expression } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';
import { build_getter } from '../../utils.js';
import { build_event_handler, build_template_literal, build_update } from './utils.js';
 
/**
 * Serializes each style directive into something like `$.set_style(element, style_property, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {StyleDirective[]} style_directives
 * @param {Identifier} element_id
 * @param {ComponentContext} context
 * @param {boolean} is_attributes_reactive
 * @param {boolean} force_check Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute
 */
export function build_style_directives(
	style_directives,
	element_id,
	context,
	is_attributes_reactive,
	force_check
) {
	const state = context.state;
 
	for (const directive of style_directives) {
		let value =
			directive.value === true
				? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
				: build_attribute_value(directive.value, context).value;
 
		const update = b.stmt(
			b.call(
				'$.set_style',
				element_id,
				b.literal(directive.name),
				value,
				/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined),
				force_check ? b.true : undefined
			)
		);
 
		const { has_state, has_call } = directive.metadata.expression;
 
		if (!is_attributes_reactive && has_call) {
			state.init.push(build_update(update));
		} else if (is_attributes_reactive || has_state || has_call) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * Serializes each class directive into something like `$.class_toogle(element, class_name, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {ClassDirective[]} class_directives
 * @param {Identifier} element_id
 * @param {ComponentContext} context
 * @param {boolean} is_attributes_reactive
 */
export function build_class_directives(
	class_directives,
	element_id,
	context,
	is_attributes_reactive
) {
	const state = context.state;
	for (const directive of class_directives) {
		const value = /** @type {Expression} */ (context.visit(directive.expression));
		const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));
 
		const { has_state, has_call } = directive.metadata.expression;
 
		if (!is_attributes_reactive && has_call) {
			state.init.push(build_update(update));
		} else if (is_attributes_reactive || has_state || has_call) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * @param {Attribute['value']} value
 * @param {ComponentContext} context
 */
export function build_attribute_value(value, context) {
	if (value === true) {
		return { has_state: false, has_call: false, value: b.literal(true) };
	}
 
	if (!Array.isArray(value) || value.length === 1) {
		const chunk = Array.isArray(value) ? value[0] : value;
 
		if (chunk.type === 'Text') {
			return { has_state: false, has_call: false, value: b.literal(chunk.data) };
		}
 
		return {
			has_state: chunk.metadata.expression.has_call,
			has_call: chunk.metadata.expression.has_call,
			value: /** @type {Expression} */ (context.visit(chunk.expression))
		};
	}
 
	return build_template_literal(value, context.visit, context.state);
}
 
/**
 * @param {RegularElement | SvelteElement} element
 * @param {Attribute} attribute
 * @param {{ state: { metadata: { namespace: Namespace }}}} context
 */
export function get_attribute_name(element, attribute, context) {
	if (
		!element.metadata.svg &&
		!element.metadata.mathml &&
		context.state.metadata.namespace !== 'foreign'
	) {
		return normalize_attribute(attribute.name);
	}
 
	return attribute.name;
}
 
/**
 * @param {Attribute & { value: ExpressionTag | [ExpressionTag] }} node
 * @param {ComponentContext} context
 */
export function build_event_attribute(node, context) {
	/** @type {string[]} */
	const modifiers = [];
 
	let event_name = node.name.slice(2);
	if (is_capture_event(event_name)) {
		event_name = event_name.slice(0, -7);
		modifiers.push('capture');
	}
 
	build_event(
		{
			name: event_name,
			expression: get_attribute_expression(node),
			modifiers,
			delegated: node.metadata.delegated
		},
		!Array.isArray(node.value) && node.value?.type === 'ExpressionTag'
			? node.value.metadata.expression
			: null,
		context
	);
}
 
/**
 * Serializes an event handler function of the `on:` directive or an attribute starting with `on`
 * @param {{name: string;modifiers: string[];expression: Expression | null;delegated?: DelegatedEvent | null;}} node
 * @param {null | ExpressionMetadata} metadata
 * @param {ComponentContext} context
 */
export function build_event(node, metadata, context) {
	const state = context.state;
 
	/** @type {Expression} */
	let expression;
 
	if (node.expression) {
		let handler = build_event_handler(node, metadata, context);
		const event_name = node.name;
		const delegated = node.delegated;
 
		if (delegated != null) {
			let delegated_assignment;
 
			if (!state.events.has(event_name)) {
				state.events.add(event_name);
			}
			// Hoist function if we can, otherwise we leave the function as is
			if (delegated.type === 'hoistable') {
				if (delegated.function === node.expression) {
					const func_name = context.state.scope.root.unique('on_' + event_name);
					state.hoisted.push(b.var(func_name, handler));
					handler = func_name;
				}
				if (node.modifiers.includes('once')) {
					handler = b.call('$.once', handler);
				}
				const hoistable_params = /** @type {Expression[]} */ (
					delegated.function.metadata.hoistable_params
				);
				// When we hoist a function we assign an array with the function and all
				// hoisted closure params.
				const args = [handler, ...hoistable_params];
				delegated_assignment = b.array(args);
			} else {
				if (node.modifiers.includes('once')) {
					handler = b.call('$.once', handler);
				}
				delegated_assignment = handler;
			}
 
			state.init.push(
				b.stmt(
					b.assignment(
						'=',
						b.member(context.state.node, b.id('__' + event_name)),
						delegated_assignment
					)
				)
			);
			return;
		}
 
		if (node.modifiers.includes('once')) {
			handler = b.call('$.once', handler);
		}
 
		const args = [
			b.literal(event_name),
			context.state.node,
			handler,
			b.literal(node.modifiers.includes('capture'))
		];
 
		if (node.modifiers.includes('passive')) {
			args.push(b.literal(true));
		} else if (node.modifiers.includes('nonpassive')) {
			args.push(b.literal(false));
		} else if (
			is_passive_event(node.name) &&
			/** @type {OnDirective} */ (node).type !== 'OnDirective'
		) {
			// For on:something events we don't apply passive behaviour to match Svelte 4.
			args.push(b.literal(true));
		}
 
		// Events need to run in order with bindings/actions
		expression = b.call('$.event', ...args);
	} else {
		expression = b.call(
			'$.event',
			b.literal(node.name),
			state.node,
			build_event_handler(node, metadata, context)
		);
	}
 
	const parent = /** @type {SvelteNode} */ (context.path.at(-1));
	const has_action_directive =
		parent.type === 'RegularElement' && parent.attributes.find((a) => a.type === 'UseDirective');
	const statement = b.stmt(
		has_action_directive ? b.call('$.effect', b.thunk(expression)) : expression
	);
 
	if (
		parent.type === 'SvelteDocument' ||
		parent.type === 'SvelteWindow' ||
		parent.type === 'SvelteBody'
	) {
		// These nodes are above the component tree, and its events should run parent first
		state.before_init.push(statement);
	} else {
		state.after_update.push(statement);
	}
}