Home / Function/ get_ancestor_elements() — svelte Function Reference

get_ancestor_elements() — svelte Function Reference

Architecture documentation for the get_ancestor_elements() function in css-prune.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  87b673b4_96c8_f795_369b_b570e339b73c["get_ancestor_elements()"]
  cb1bf043_dade_7352_cc2b_976ffa2968d8["css-prune.js"]
  87b673b4_96c8_f795_369b_b570e339b73c -->|defined in| cb1bf043_dade_7352_cc2b_976ffa2968d8
  a482078a_f217_b205_783e_29603b509866["apply_combinator()"]
  a482078a_f217_b205_783e_29603b509866 -->|calls| 87b673b4_96c8_f795_369b_b570e339b73c
  style 87b673b4_96c8_f795_369b_b570e339b73c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js lines 750–813

function get_ancestor_elements(node, adjacent_only, seen = new Set()) {
	/** @type {Array<Compiler.AST.RegularElement | Compiler.AST.SvelteElement>} */
	const ancestors = [];

	const path = node.metadata.path;
	let i = path.length;

	while (i--) {
		const parent = path[i];

		if (parent.type === 'SnippetBlock') {
			if (!seen.has(parent)) {
				seen.add(parent);

				for (const site of parent.metadata.sites) {
					ancestors.push(...get_ancestor_elements(site, adjacent_only, seen));
				}
			}

			break;
		}

		if (parent.type === 'RegularElement' || parent.type === 'SvelteElement') {
			// Special handling for <option> inside <select>: elements inside <option> should
			// also be considered descendants of <selectedcontent>, which clones the selected option's content
			if (parent.type === 'RegularElement' && parent.name === 'option') {
				const is_direct_child = ancestors.length === 0;

				const select_element = path.findLast(
					(element, j) => element.type === 'RegularElement' && element.name === 'select' && j < i
				);

				if (select_element && (!adjacent_only || is_direct_child)) {
					/** @type {Compiler.AST.RegularElement | null} */
					let selectedcontent_element = null;
					walk(select_element, null, {
						RegularElement(child, context) {
							if (child.name === 'selectedcontent') {
								selectedcontent_element = child;
								context.stop();
								return;
							}
							context.next();
						}
					});

					if (adjacent_only && is_direct_child && selectedcontent_element) {
						return [selectedcontent_element, parent];
					} else if (selectedcontent_element) {
						ancestors.push(selectedcontent_element);
					}
				}
			}

			ancestors.push(parent);

			if (adjacent_only) {
				break;
			}
		}
	}

	return ancestors;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does get_ancestor_elements() do?
get_ancestor_elements() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js.
Where is get_ancestor_elements() defined?
get_ancestor_elements() is defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js at line 750.
What calls get_ancestor_elements()?
get_ancestor_elements() is called by 1 function(s): apply_combinator.

Analyze Your Own Codebase

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

Try Supermodel Free