All files / src/internal/client render.js

92.2% Statements 331/359
89.58% Branches 43/48
83.33% Functions 10/12
91.93% Lines 319/347

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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 3482x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 283x 283x 2x 2x 2x 2x 2x 2x 2x 16715x 16715x 16715x 16715x 2978x 2978x 2978x 16715x 9947x 9947x 9947x 9947x 16715x 2x 2x 2x 2x 2x 2x 2x 2x 644x 40x 30x 30x 644x 604x 604x 644x 2x 2x 2x 2x 2x 2x 35549x 35549x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1535x 1535x 1535x 1533x 1535x 1535x 1535x 1535x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1467x 1464x 3x 3x 1464x 1464x     1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 1464x 8x 2x     2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 1464x 1464x 1464x 1464x 1464x 1464x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 3592x 1831x 1831x 1256x 1256x 1256x 1256x 1256x 1256x 1256x 1256x 1256x     1256x 1256x 1256x 1256x 1256x 1256x 1256x 1256x 1256x     1256x 1256x 1256x 1831x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 4x 4x 4x 4x 2997x 2997x 2x 2x 2x 2997x 2997x 2997x 2997x 2997x 2997x 2997x 4x 4x 2997x 2997x 2997x 2961x 1206x 1206x 2961x 2961x 2997x 2997x 2997x 2997x 2997x 2997x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2969x 2969x 8x 8x 8x 8x 2969x 2969x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x                         2x 2x 2x 2x                  
import { DEV } from 'esm-env';
import {
	append_child,
	clear_text_content,
	create_element,
	empty,
	init_operations
} from './dom/operations.js';
import { HYDRATION_ERROR, HYDRATION_START, PassiveDelegatedEvents } from '../../constants.js';
import { flush_sync, push, pop, current_component_context } from './runtime.js';
import { effect_root, branch } from './reactivity/effects.js';
import {
	hydrate_anchor,
	hydrate_nodes,
	hydrating,
	set_hydrate_nodes,
	set_hydrating
} from './dom/hydration.js';
import { array_from } from './utils.js';
import { handle_event_propagation } from './dom/elements/events.js';
import { reset_head_anchor } from './dom/blocks/svelte-head.js';
import * as w from './warnings.js';
import * as e from './errors.js';
import { validate_component } from '../shared/validate.js';
 
/** @type {Set<string>} */
export const all_registered_events = new Set();
 
/** @type {Set<(events: Array<string>) => void>} */
export const root_event_handles = new Set();
 
/**
 * This is normally true — block effects should run their intro transitions —
 * but is false during hydration and mounting (unless `options.intro` is `true`)
 * and when creating the children of a `<svelte:element>` that just changed tag
 */
export let should_intro = true;
 
/** @param {boolean} value */
export function set_should_intro(value) {
	should_intro = value;
}
 
/**
 * @param {Element} dom
 * @param {string} value
 * @returns {void}
 */
export function set_text(dom, value) {
	// @ts-expect-error need to add __value to patched prototype
	const prev_node_value = dom.__nodeValue;
	const next_node_value = stringify(value);
	if (hydrating && dom.nodeValue === next_node_value) {
		// In case of hydration don't reset the nodeValue as it's already correct.
		// @ts-expect-error need to add __nodeValue to patched prototype
		dom.__nodeValue = next_node_value;
	} else if (prev_node_value !== next_node_value) {
		dom.nodeValue = next_node_value;
		// @ts-expect-error need to add __className to patched prototype
		dom.__nodeValue = next_node_value;
	}
}
 
/**
 * @param {Comment} anchor
 * @param {void | ((anchor: Comment, slot_props: Record<string, unknown>) => void)} slot_fn
 * @param {Record<string, unknown>} slot_props
 * @param {null | ((anchor: Comment) => void)} fallback_fn
 */
export function slot(anchor, slot_fn, slot_props, fallback_fn) {
	if (slot_fn === undefined) {
		if (fallback_fn !== null) {
			fallback_fn(anchor);
		}
	} else {
		slot_fn(anchor, slot_props);
	}
}
 
/**
 * @param {unknown} value
 * @returns {string}
 */
export function stringify(value) {
	return typeof value === 'string' ? value : value == null ? '' : value + '';
}
 
/**
 * Mounts a component to the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
 *
 * @template {Record<string, any>} Props
 * @template {Record<string, any>} Exports
 * @template {Record<string, any>} Events
 * @param {import('../../index.js').ComponentType<import('../../index.js').SvelteComponent<Props, Events>>} component
 * @param {{
 * 		target: Document | Element | ShadowRoot;
 * 		anchor?: Node;
 * 		props?: Props;
 * 		events?: { [Property in keyof Events]: (e: Events[Property]) => any };
 * 		context?: Map<any, any>;
 * 		intro?: boolean;
 * 	}} options
 * @returns {Exports}
 */
export function mount(component, options) {
	if (DEV) {
		validate_component(component);
	}
 
	const anchor = options.anchor ?? options.target.appendChild(empty());
	// Don't flush previous effects to ensure order of outer effects stays consistent
	return flush_sync(() => _mount(component, { ...options, anchor }), false);
}
 
/**
 * Hydrates a component on the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
 *
 * @template {Record<string, any>} Props
 * @template {Record<string, any>} Exports
 * @template {Record<string, any>} Events
 * @param {import('../../index.js').ComponentType<import('../../index.js').SvelteComponent<Props, Events>>} component
 * @param {{
 * 		target: Document | Element | ShadowRoot;
 * 		props?: Props;
 * 		events?: { [Property in keyof Events]: (e: Events[Property]) => any };
 *  	context?: Map<any, any>;
 * 		intro?: boolean;
 * 		recover?: boolean;
 * 	}} options
 * @returns {Exports}
 */
export function hydrate(component, options) {
	if (DEV) {
		validate_component(component);
	}
 
	const target = options.target;
	const previous_hydrate_nodes = hydrate_nodes;
 
	let hydrated = false;
 
	try {
		// Don't flush previous effects to ensure order of outer effects stays consistent
		return flush_sync(() => {
			set_hydrating(true);
 
			var node = target.firstChild;
			while (
				node &&
				(node.nodeType !== 8 || /** @type {Comment} */ (node).data !== HYDRATION_START)
			) {
				node = node.nextSibling;
			}
 
			if (!node) {
				throw HYDRATION_ERROR;
			}
 
			const anchor = hydrate_anchor(node);
			const instance = _mount(component, { ...options, anchor });
 
			// flush_sync will run this callback and then synchronously run any pending effects,
			// which don't belong to the hydration phase anymore - therefore reset it here
			set_hydrating(false);
			hydrated = true;
 
			return instance;
		}, false);
	} catch (error) {
		if (error === HYDRATION_ERROR) {
			if (options.recover === false) {
				e.hydration_failed();
			}
 
			// If an error occured above, the operations might not yet have been initialised.
			init_operations();
			clear_text_content(target);
 
			set_hydrating(false);
			return mount(component, options);
		}
 
		throw error;
	} finally {
		set_hydrating(!!previous_hydrate_nodes);
		set_hydrate_nodes(previous_hydrate_nodes);
		reset_head_anchor();
	}
}
 
/**
 * @template {Record<string, any>} Exports
 * @param {import('../../index.js').ComponentType<import('../../index.js').SvelteComponent<any>>} Component
 * @param {{
 * 		target: Document | Element | ShadowRoot;
 * 		anchor: Node;
 * 		props?: any;
 * 		events?: any;
 * 		context?: Map<any, any>;
 * 		intro?: boolean;
 * 	}} options
 * @returns {Exports}
 */
function _mount(Component, { target, anchor, props = {}, events, context, intro = false }) {
	init_operations();
 
	const registered_events = new Set();
 
	const bound_event_listener = handle_event_propagation.bind(null, target);
	const bound_document_event_listener = handle_event_propagation.bind(null, document);
 
	/** @param {Array<string>} events */
	const event_handle = (events) => {
		for (let i = 0; i < events.length; i++) {
			const event_name = events[i];
			if (!registered_events.has(event_name)) {
				registered_events.add(event_name);
				// Add the event listener to both the container and the document.
				// The container listener ensures we catch events from within in case
				// the outer content stops propagation of the event.
				target.addEventListener(
					event_name,
					bound_event_listener,
					PassiveDelegatedEvents.includes(event_name)
						? {
								passive: true
							}
						: undefined
				);
				// The document listener ensures we catch events that originate from elements that were
				// manually moved outside of the container (e.g. via manual portals).
				document.addEventListener(
					event_name,
					bound_document_event_listener,
					PassiveDelegatedEvents.includes(event_name)
						? {
								passive: true
							}
						: undefined
				);
			}
		}
	};
 
	event_handle(array_from(all_registered_events));
	root_event_handles.add(event_handle);
 
	/** @type {Exports} */
	// @ts-expect-error will be defined because the render effect runs synchronously
	let component = undefined;
 
	const unmount = effect_root(() => {
		branch(() => {
			if (context) {
				push({});
				var ctx = /** @type {import('#client').ComponentContext} */ (current_component_context);
				ctx.c = context;
			}
 
			if (events) {
				// We can't spread the object or else we'd lose the state proxy stuff, if it is one
				/** @type {any} */ (props).$$events = events;
			}
 
			should_intro = intro;
			// @ts-expect-error the public typings are not what the actual function looks like
			component = Component(anchor, props) || {};
			should_intro = true;
 
			if (context) {
				pop();
			}
		});
 
		return () => {
			for (const event_name of registered_events) {
				target.removeEventListener(event_name, bound_event_listener);
			}
			root_event_handles.delete(event_handle);
			mounted_components.delete(component);
		};
	});
 
	mounted_components.set(component, unmount);
	return component;
}
 
/**
 * References of the components that were mounted or hydrated.
 * Uses a `WeakMap` to avoid memory leaks.
 */
let mounted_components = new WeakMap();
 
/**
 * Unmounts a component that was previously mounted using `mount` or `hydrate`.
 * @param {Record<string, any>} component
 */
export function unmount(component) {
	const fn = mounted_components.get(component);
	if (DEV && !fn) {
		w.lifecycle_double_unmount();
		// eslint-disable-next-line no-console
		console.trace('stack trace');
	}
	fn?.();
}
 
/**
 * @param {Record<string, any>} props
 * @returns {Record<string, any>}
 */
export function sanitize_slots(props) {
	const sanitized = { ...props.$$slots };
	if (props.children) sanitized.default = props.children;
	return sanitized;
}
 
/**
 * @param {Node} target
 * @param {string} style_sheet_id
 * @param {string} styles
 */
export async function append_styles(target, style_sheet_id, styles) {
	// Wait a tick so that the template is added to the dom, else getRootNode() will yield wrong results
	// If it turns out that this results in noticeable flickering, we need to do something like doing the
	// append outside and adding code in mount that appends all stylesheets (similar to how we do it with event delegation)
	await Promise.resolve();
	const append_styles_to = get_root_for_style(target);
	if (!append_styles_to.getElementById(style_sheet_id)) {
		const style = create_element('style');
		style.id = style_sheet_id;
		style.textContent = styles;
		append_child(/** @type {Document} */ (append_styles_to).head || append_styles_to, style);
	}
}
 
/**
 * @param {Node} node
 */
function get_root_for_style(node) {
	if (!node) return document;
	const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
	if (root && /** @type {ShadowRoot} */ (root).host) {
		return /** @type {ShadowRoot} */ (root);
	}
	return /** @type {Document} */ (node.ownerDocument);
}