Home / File/ swap-functions.ts — astro Source File

swap-functions.ts — astro Source File

Architecture documentation for swap-functions.ts, a typescript file in the astro codebase.

Entity Profile

Relationship Graph

Source Code

export type SavedFocus = {
	activeElement: HTMLElement | null;
	start?: number | null;
	end?: number | null;
};

const PERSIST_ATTR = 'data-astro-transition-persist';

const NON_OVERRIDABLE_ASTRO_ATTRS = ['data-astro-transition', 'data-astro-transition-fallback'];

const scriptsAlreadyRan = new Set<string>();
export function detectScriptExecuted(script: HTMLScriptElement) {
	const key = script.src ? new URL(script.src, location.href).href : script.textContent!;
	if (scriptsAlreadyRan.has(key)) return true;
	scriptsAlreadyRan.add(key);
	return false;
}

/*
 * 	Mark new scripts that should not execute
 */
export function deselectScripts(doc: Document) {
	for (const s2 of doc.scripts) {
		if (
			// Check if the script should be rerun regardless of it being the same
			!s2.hasAttribute('data-astro-rerun') &&
			// Check if the script has already been executed
			detectScriptExecuted(s2)
		) {
			// the old script is in the new document and doesn't have the rerun attribute
			// we mark it as executed to prevent re-execution
			s2.dataset.astroExec = '';
		}
	}
}

/*
 * swap attributes of the html element
 * delete all attributes from the current document
 * insert all attributes from doc
 * reinsert all original attributes that are referenced in NON_OVERRIDABLE_ASTRO_ATTRS'
 */
export function swapRootAttributes(newDoc: Document) {
	const currentRoot = document.documentElement;
	const nonOverridableAstroAttributes = [...currentRoot.attributes].filter(
		({ name }) => (currentRoot.removeAttribute(name), NON_OVERRIDABLE_ASTRO_ATTRS.includes(name)),
	);
	[...newDoc.documentElement.attributes, ...nonOverridableAstroAttributes].forEach(
		({ name, value }) => currentRoot.setAttribute(name, value),
	);
}

/*
 * make the old head look like the new one
 */
export function swapHeadElements(doc: Document) {
	for (const el of Array.from(document.head.children)) {
		const newEl = persistedHeadElement(el as HTMLElement, doc);
		// If the element exists in the document already, remove it
		// from the new document and leave the current node alone
// ... (132 more lines)

Domain

Subdomains

Types

Frequently Asked Questions

What does swap-functions.ts do?
swap-functions.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in swap-functions.ts?
swap-functions.ts defines 12 function(s): attachShadowRoots, deselectScripts, detectScriptExecuted, isSameProps, persistedHeadElement, restoreFocus, saveFocus, shouldCopyProps, swap, swapBodyElement, and 2 more.
Where is swap-functions.ts in the architecture?
swap-functions.ts is located at packages/astro/src/transitions/swap-functions.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/transitions).

Analyze Your Own Codebase

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

Try Supermodel Free