Home / File/ customizable-select.js — svelte Source File

customizable-select.js — svelte Source File

Architecture documentation for customizable-select.js, a javascript file in the svelte codebase. 8 imports, 0 dependents.

File javascript ClientRuntime Hydration 8 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  4871424a_ea24_562e_42c1_423f3652e63b["customizable-select.js"]
  f3948b0d_b92a_0767_ba6c_832767f4e2bb["hydration.js"]
  4871424a_ea24_562e_42c1_423f3652e63b --> f3948b0d_b92a_0767_ba6c_832767f4e2bb
  ace40ef0_8734_2f17_9d6e_35e08f332e08["reset"]
  4871424a_ea24_562e_42c1_423f3652e63b --> ace40ef0_8734_2f17_9d6e_35e08f332e08
  40f27ad3_30bb_8f2a_3fb3_757088cf7428["set_hydrate_node"]
  4871424a_ea24_562e_42c1_423f3652e63b --> 40f27ad3_30bb_8f2a_3fb3_757088cf7428
  f5b61c69_d41c_bdb7_b931_5b8b3374332c["set_hydrating"]
  4871424a_ea24_562e_42c1_423f3652e63b --> f5b61c69_d41c_bdb7_b931_5b8b3374332c
  9a9bbc27_46b6_021c_6d77_f736ed4b40f0["operations.js"]
  4871424a_ea24_562e_42c1_423f3652e63b --> 9a9bbc27_46b6_021c_6d77_f736ed4b40f0
  ad2d3e5b_92c4_44cd_6972_84b884432025["create_comment"]
  4871424a_ea24_562e_42c1_423f3652e63b --> ad2d3e5b_92c4_44cd_6972_84b884432025
  da569db2_9d2a_e7c6_6b96_4551a150836c["attachments.js"]
  4871424a_ea24_562e_42c1_423f3652e63b --> da569db2_9d2a_e7c6_6b96_4551a150836c
  4133c9ed_6ce6_0847_e62e_62aaf8690ab4["attach"]
  4871424a_ea24_562e_42c1_423f3652e63b --> 4133c9ed_6ce6_0847_e62e_62aaf8690ab4
  style 4871424a_ea24_562e_42c1_423f3652e63b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { hydrating, reset, set_hydrate_node, set_hydrating } from '../hydration.js';
import { create_comment } from '../operations.js';
import { attach } from './attachments.js';

/** @type {boolean | null} */
let supported = null;

/**
 * Checks if the browser supports rich HTML content inside `<option>` elements.
 * Modern browsers preserve HTML elements inside options, while older browsers
 * strip them during parsing, leaving only text content.
 * @returns {boolean}
 */
function is_supported() {
	if (supported === null) {
		var select = document.createElement('select');
		select.innerHTML = '<option><span>t</span></option>';
		supported = /** @type {Element} */ (select.firstChild)?.firstChild?.nodeType === 1;
	}

	return supported;
}

/**
 *
 * @param {HTMLElement} element
 * @param {(new_element: HTMLElement) => void} update_element
 */
export function selectedcontent(element, update_element) {
	// if it's not supported no need for special logic
	if (!is_supported()) return;

	// we use the attach function directly just to make sure is executed when is mounted to the dom
	attach(element, () => () => {
		const select = element.closest('select');
		if (!select) return;

		const observer = new MutationObserver((entries) => {
			var selected = false;

			for (const entry of entries) {
				if (entry.target === element) {
					// the `<selectedcontent>` already changed, no need to replace it
					return;
				}

				// if the changes doesn't include the selected `<option>` we don't need to do anything
				selected ||= !!entry.target.parentElement?.closest('option')?.selected;
			}

			if (selected) {
				// replace the `<selectedcontent>` with a clone
				element.replaceWith((element = /** @type {HTMLElement} */ (element.cloneNode(true))));
				update_element(element);
			}
		});

		observer.observe(select, {
			childList: true,
			characterData: true,
			subtree: true
		});

		return () => {
			observer.disconnect();
		};
	});
}

/**
 * Handles rich HTML content inside `<option>`, `<optgroup>`, or `<select>` elements with browser-specific branching.
 * Modern browsers preserve HTML inside options, while older browsers strip it to text only.
 *
 * @param {HTMLOptionElement | HTMLOptGroupElement | HTMLSelectElement} element The element to process
 * @param {() => void} rich_fn Function to process rich HTML content (modern browsers)
 */
export function customizable_select(element, rich_fn) {
	var was_hydrating = hydrating;

	if (!is_supported()) {
		set_hydrating(false);
		element.textContent = '';
		element.append(create_comment(''));
	}

	try {
		rich_fn();
	} finally {
		if (was_hydrating) {
			if (hydrating) {
				reset(element);
			} else {
				set_hydrating(true);
				set_hydrate_node(element);
			}
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does customizable-select.js do?
customizable-select.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in customizable-select.js?
customizable-select.js defines 3 function(s): customizable_select, is_supported, selectedcontent.
What does customizable-select.js depend on?
customizable-select.js imports 8 module(s): attach, attachments.js, create_comment, hydration.js, operations.js, reset, set_hydrate_node, set_hydrating.
Where is customizable-select.js in the architecture?
customizable-select.js is located at packages/svelte/src/internal/client/dom/elements/customizable-select.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dom/elements).

Analyze Your Own Codebase

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

Try Supermodel Free