All files / src/internal/client/dom/blocks svelte-element.js

97.45% Statements 153/157
85.71% Branches 30/35
100% Functions 2/2
97.31% Lines 145/149

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 1502x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 64x 64x 64x 64x 64x 133x 56x 56x 56x 133x 64x     64x 2x 2x 2x 2x 2x 2x 2x 2x 2x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 199x 207x 207x 207x 68x 8x 8x 8x 8x 8x 8x 68x     60x 60x 60x 60x 60x 68x 207x 207x 193x 193x 193x 193x 193x 132x 193x 193x 193x 173x 173x 173x 173x 173x 173x 173x 168x 168x 168x 168x 168x 168x 173x 193x 193x 193x 193x 64x 64x 64x 193x 193x 207x 207x 207x 207x 207x 207x 133x 133x 133x 133x 133x 133x 133x  
import { namespace_svg } from '../../../../constants.js';
import { hydrate_anchor, hydrate_nodes, hydrating } from '../hydration.js';
import { empty } from '../operations.js';
import {
	block,
	branch,
	destroy_effect,
	pause_effect,
	render_effect,
	resume_effect
} from '../../reactivity/effects.js';
import { is_array } from '../../utils.js';
import { set_should_intro } from '../../render.js';
import { current_each_item, set_current_each_item } from './each.js';
import { current_effect } from '../../runtime.js';
 
/**
 * @param {import('#client').Effect} effect
 * @param {Element} from
 * @param {Element} to
 * @returns {void}
 */
function swap_block_dom(effect, from, to) {
	const dom = effect.dom;
 
	if (is_array(dom)) {
		for (let i = 0; i < dom.length; i++) {
			if (dom[i] === from) {
				dom[i] = to;
				break;
			}
		}
	} else if (dom === from) {
		effect.dom = to;
	}
}
 
/**
 * @param {Comment} anchor
 * @param {() => string} get_tag
 * @param {boolean | null} is_svg `null` == not statically known
 * @param {undefined | ((element: Element, anchor: Node) => void)} render_fn
 * @returns {void}
 */
export function element(anchor, get_tag, is_svg, render_fn) {
	const parent_effect = /** @type {import('#client').Effect} */ (current_effect);
 
	render_effect(() => {
		/** @type {string | null} */
		let tag;
 
		/** @type {string | null} */
		let current_tag;
 
		/** @type {null | Element} */
		let element = null;
 
		/** @type {import('#client').Effect | null} */
		let effect;
 
		/**
		 * The keyed `{#each ...}` item block, if any, that this element is inside.
		 * We track this so we can set it when changing the element, allowing any
		 * `animate:` directive to bind itself to the correct block
		 */
		let each_item_block = current_each_item;
 
		block(() => {
			const next_tag = get_tag() || null;
			if (next_tag === tag) return;
 
			// See explanation of `each_item_block` above
			var previous_each_item = current_each_item;
			set_current_each_item(each_item_block);
 
			// We try our best infering the namespace in case it's not possible to determine statically,
			// but on the first render on the client (without hydration) the parent will be undefined,
			// since the anchor is not attached to its parent / the dom yet.
			const ns =
				is_svg || next_tag === 'svg'
					? namespace_svg
					: is_svg === false || anchor.parentElement?.tagName === 'foreignObject'
						? null
						: anchor.parentElement?.namespaceURI ?? null;
 
			if (effect) {
				if (next_tag === null) {
					// start outro
					pause_effect(effect, () => {
						effect = null;
						current_tag = null;
						element?.remove();
					});
				} else if (next_tag === current_tag) {
					// same tag as is currently rendered — abort outro
					resume_effect(effect);
				} else {
					// tag is changing — destroy immediately, render contents without intro transitions
					destroy_effect(effect);
					set_should_intro(false);
				}
			}
 
			if (next_tag && next_tag !== current_tag) {
				effect = branch(() => {
					const prev_element = element;
					element = hydrating
						? /** @type {Element} */ (hydrate_nodes[0])
						: ns
							? document.createElementNS(ns, next_tag)
							: document.createElement(next_tag);
 
					if (render_fn) {
						// If hydrating, use the existing ssr comment as the anchor so that the
						// inner open and close methods can pick up the existing nodes correctly
						var child_anchor = hydrating
							? element.firstChild && hydrate_anchor(/** @type {Comment} */ (element.firstChild))
							: element.appendChild(empty());
 
						if (child_anchor) {
							// `child_anchor` can be undefined if this is a void element with children,
							// i.e. `<svelte:element this={"hr"}>...</svelte:element>`. This is
							// user error, but we warn on it elsewhere (in dev) so here we just
							// silently ignore it
							render_fn(element, child_anchor);
						}
					}
 
					anchor.before(element);
 
					if (prev_element) {
						swap_block_dom(parent_effect, prev_element, element);
						prev_element.remove();
					}
				});
			}
 
			tag = next_tag;
			if (tag) current_tag = tag;
			set_should_intro(true);
 
			set_current_each_item(previous_each_item);
		});
 
		return () => {
			element?.remove();
		};
	});
}