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

98.8% Statements 166/168
94.44% Branches 34/36
100% Functions 1/1
98.73% Lines 156/158

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 1592x 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 147x 147x 147x 69x 69x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 66x 66x 66x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 147x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 70x 8x 8x 8x 8x 8x 70x     62x 62x 62x 62x 62x 70x 223x 223x 209x 209x 209x 209x 143x 209x 209x 209x 18x 18x 18x 18x 18x 18x 18x 18x 18x 209x 209x 209x 209x 189x 189x 189x 189x 189x 189x 189x 57x 6x 57x 51x 51x 57x 189x 189x 189x 189x 189x 189x 189x 209x 209x 209x 209x 209x 209x 209x 223x 223x 223x 223x 223x 223x 147x 147x 147x 69x 69x 69x 147x  
/** @import { Effect, EffectNodes, TemplateNode } from '#client' */
import { namespace_svg } from '../../../../constants.js';
import {
	hydrate_next,
	hydrate_node,
	hydrating,
	set_hydrate_node,
	set_hydrating
} from '../hydration.js';
import { empty } from '../operations.js';
import {
	block,
	branch,
	destroy_effect,
	pause_effect,
	resume_effect
} from '../../reactivity/effects.js';
import { set_should_intro } from '../../render.js';
import { current_each_item, set_current_each_item } from './each.js';
import { current_component_context, current_effect } from '../../runtime.js';
import { DEV } from 'esm-env';
import { EFFECT_TRANSPARENT } from '../../constants.js';
import { assign_nodes } from '../template.js';
 
/**
 * @param {Comment | Element} node
 * @param {() => string} get_tag
 * @param {boolean} is_svg
 * @param {undefined | ((element: Element, anchor: Node | null) => void)} render_fn,
 * @param {undefined | (() => string)} get_namespace
 * @param {undefined | [number, number]} location
 * @returns {void}
 */
export function element(node, get_tag, is_svg, render_fn, get_namespace, location) {
	let was_hydrating = hydrating;
 
	if (hydrating) {
		hydrate_next();
	}
 
	var filename = DEV && location && current_component_context?.function.filename;
 
	/** @type {string | null} */
	var tag;
 
	/** @type {string | null} */
	var current_tag;
 
	/** @type {null | Element} */
	var element = null;
 
	if (hydrating && hydrate_node.nodeType === 1) {
		element = /** @type {Element} */ (hydrate_node);
		hydrate_next();
	}
 
	var anchor = /** @type {TemplateNode} */ (hydrating ? hydrate_node : node);
 
	/** @type {Effect | null} */
	var 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
	 */
	var each_item_block = current_each_item;
 
	block(() => {
		const next_tag = get_tag() || null;
		var ns = get_namespace ? get_namespace() : is_svg || next_tag === 'svg' ? namespace_svg : null;
 
		// Assumption: Noone changes the namespace but not the tag (what would that even mean?)
		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);
 
		if (effect) {
			if (next_tag === null) {
				// start outro
				pause_effect(effect, () => {
					effect = null;
					current_tag = null;
				});
			} 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(() => {
				element = hydrating
					? /** @type {Element} */ (element)
					: ns
						? document.createElementNS(ns, next_tag)
						: document.createElement(next_tag);
 
				if (DEV && location) {
					// @ts-expect-error
					element.__svelte_meta = {
						loc: {
							file: filename,
							line: location[0],
							column: location[1]
						}
					};
				}
 
				assign_nodes(element, element);
 
				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 = /** @type {TemplateNode} */ (
						hydrating ? element.firstChild : element.appendChild(empty())
					);
 
					if (hydrating) {
						if (child_anchor === null) {
							set_hydrating(false);
						} else {
							set_hydrate_node(child_anchor);
						}
					}
 
					// `child_anchor` is undefined if this is a void element, but we still
					// need to call `render_fn` in order to run actions etc. If the element
					// contains children, it's a user error (which is warned on elsewhere)
					// and the DOM will be silently discarded
					render_fn(element, child_anchor);
				}
 
				// we do this after calling `render_fn` so that child effects don't override `nodes.end`
				/** @type {Effect & { nodes: EffectNodes }} */ (current_effect).nodes.end = element;
 
				anchor.before(element);
			});
		}
 
		tag = next_tag;
		if (tag) current_tag = tag;
		set_should_intro(true);
 
		set_current_each_item(previous_each_item);
	}, EFFECT_TRANSPARENT);
 
	if (was_hydrating) {
		set_hydrating(true);
		set_hydrate_node(anchor);
	}
}