Home / Function/ get_descendant_elements() — svelte Function Reference

get_descendant_elements() — svelte Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js lines 820–881

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

	/**
	 * @param {Compiler.AST.SvelteNode} node
	 */
	function walk_children(node) {
		walk(node, null, {
			_(node, context) {
				if (node.type === 'RegularElement' || node.type === 'SvelteElement') {
					descendants.push(node);

					if (!adjacent_only) {
						context.next();
					}
				} else if (node.type === 'RenderTag') {
					for (const snippet of node.metadata.snippets) {
						if (seen.has(snippet)) continue;

						seen.add(snippet);
						walk_children(snippet.body);
					}
				} else {
					context.next();
				}
			}
		});
	}

	walk_children(node.type === 'RenderTag' ? node : node.fragment);

	// Special handling for <selectedcontent>: it clones the content of the selected <option>,
	// so descendants of <option> elements in the same <select> should also be considered descendants
	if (node.type === 'RegularElement' && node.name === 'selectedcontent') {
		const path = node.metadata.path;
		const select_element = path.findLast(
			(/** @type {Compiler.AST.SvelteNode} */ element) =>
				element.type === 'RegularElement' && element.name === 'select'
		);

		if (select_element) {
			walk(
				select_element,
				{ inside_option: false },
				{
					_(child, context) {
						if (child.type === 'RegularElement' && child.name === 'option') {
							context.next({ inside_option: true });
						} else if (context.state.inside_option) {
							walk_children(child);
						} else {
							context.next();
						}
					}
				}
			);
		}
	}

	return descendants;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does get_descendant_elements() do?
get_descendant_elements() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js.
Where is get_descendant_elements() defined?
get_descendant_elements() is defined in packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js at line 820.
What calls get_descendant_elements()?
get_descendant_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