Home / Function/ element() — svelte Function Reference

element() — svelte Function Reference

Architecture documentation for the element() function in element.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb["element()"]
  206889ff_1f9f_b6c1_d530_059d001e1cf4["element.js"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|defined in| 206889ff_1f9f_b6c1_d530_059d001e1cf4
  02a8b5a2_13c5_e5b3_1ed2_617763d0904d["is_void()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 02a8b5a2_13c5_e5b3_1ed2_617763d0904d
  eccc1c60_292d_ee64_1357_7174560eb0ee["void_element_invalid_content()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| eccc1c60_292d_ee64_1357_7174560eb0ee
  313a36c6_761f_13b2_e8d6_c0f84cd81a9a["is_element_node()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 313a36c6_761f_13b2_e8d6_c0f84cd81a9a
  2f2a18df_cf0f_6e23_cd90_0dcb551f1613["element_implicitly_closed()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 2f2a18df_cf0f_6e23_cd90_0dcb551f1613
  d2699b04_05cb_4016_61a6_52a7e0cb377d["element_invalid_closing_tag_autoclosed()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| d2699b04_05cb_4016_61a6_52a7e0cb377d
  cbab1d0d_9a0e_4253_9f0a_85a4e0a77028["element_invalid_closing_tag()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| cbab1d0d_9a0e_4253_9f0a_85a4e0a77028
  0905a037_af41_826b_7a01_9368b74b61a8["read_tag()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 0905a037_af41_826b_7a01_9368b74b61a8
  1c919600_8962_1b47_93ee_c3464e15f952["svelte_meta_invalid_tag()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 1c919600_8962_1b47_93ee_c3464e15f952
  5439801b_d67f_6bbb_bae7_1c9abc2aa128["list()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 5439801b_d67f_6bbb_bae7_1c9abc2aa128
  ac47797f_fb63_5e09_884b_ead7ef41ba5b["tag_invalid_name()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| ac47797f_fb63_5e09_884b_ead7ef41ba5b
  660946ce_1d33_45c4_d09d_604c0d8529c4["svelte_meta_duplicate()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 660946ce_1d33_45c4_d09d_604c0d8529c4
  d712f530_8f33_de2c_7086_64a24896443c["svelte_meta_invalid_placement()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| d712f530_8f33_de2c_7086_64a24896443c
  50640043_db84_c406_bc20_47c7f6a3c889["parent_is_head()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 50640043_db84_c406_bc20_47c7f6a3c889
  style be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/1-parse/state/element.js lines 54–422

export default function element(parser) {
	const start = parser.index++;

	let parent = parser.current();

	if (parser.eat('!--')) {
		const data = parser.read_until(regex_closing_comment);
		parser.eat('-->', true);

		parser.append({
			type: 'Comment',
			start,
			end: parser.index,
			data
		});

		return;
	}

	if (parser.eat('/')) {
		const name = parser.read_until(regex_whitespace_or_slash_or_closing_tag);

		parser.allow_whitespace();
		parser.eat('>', true);

		if (is_void(name)) {
			e.void_element_invalid_content(start);
		}

		// close any elements that don't have their own closing tags, e.g. <div><p></div>
		while (/** @type {AST.RegularElement} */ (parent).name !== name) {
			if (parser.loose) {
				// If the previous element did interpret the next opening tag as an attribute, backtrack
				if (is_element_node(parent)) {
					const last = parent.attributes.at(-1);
					if (last?.type === 'Attribute' && last.name === `<${name}`) {
						parser.index = last.start;
						parent.attributes.pop();
						break;
					}
				}
			}

			if (parent.type === 'RegularElement') {
				if (!parser.last_auto_closed_tag || parser.last_auto_closed_tag.tag !== name) {
					const end = parent.fragment.nodes[0]?.start ?? start;
					w.element_implicitly_closed(
						{ start: parent.start, end },
						`</${name}>`,
						`</${parent.name}>`
					);
				}
			} else if (!parser.loose) {
				if (parser.last_auto_closed_tag && parser.last_auto_closed_tag.tag === name) {
					e.element_invalid_closing_tag_autoclosed(start, name, parser.last_auto_closed_tag.reason);
				} else {
					e.element_invalid_closing_tag(start, name);
				}
			}

			parent.end = start;
			parser.pop();

			parent = parser.current();
		}

		parent.end = parser.index;
		parser.pop();

		if (parser.last_auto_closed_tag && parser.stack.length < parser.last_auto_closed_tag.depth) {
			parser.last_auto_closed_tag = undefined;
		}

		return;
	}

	const tag = read_tag(parser, regex_whitespace_or_slash_or_closing_tag);

	if (tag.name.startsWith('svelte:') && !meta_tags.has(tag.name)) {
		const bounds = { start: start + 1, end: start + 1 + tag.name.length };
		e.svelte_meta_invalid_tag(bounds, list(Array.from(meta_tags.keys())));

Domain

Subdomains

Frequently Asked Questions

What does element() do?
element() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/state/element.js.
Where is element() defined?
element() is defined in packages/svelte/src/compiler/phases/1-parse/state/element.js at line 54.
What does element() call?
element() calls 28 function(s): attribute_duplicate, closing_tag_omitted, create_fragment, element_implicitly_closed, element_invalid_closing_tag, element_invalid_closing_tag_autoclosed, get_attribute_expression, is_element_node, and 20 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free