/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import type {ContextRecord} from '../types'; import type { DOMImportExtensionOutput, DOMPreprocessContext, DOMPreprocessFn, GenerateNodesFromDOMOptions, ImportContextPairOrUpdater, } from './types'; import {$getExtensionOutput} from '@lexical/extension'; import {defineExtension, type LexicalNode, shallowMergeConfig} from 'lexical'; import {DOMImportContextSymbol, DOMImportExtensionName} from '../constants'; import { $withFullContext, contextFromPairs, getContextRecord, } from '../ContextRecord'; import {type CompiledDispatch, compileImportRules} from './compileImportRules'; import {defineImportRule} from './defineImportRule'; import { type DOMImportRuleEntry, flattenRuleEntries, } from './defineOverlayRules'; import {ImportSessionImpl} from './ImportContext'; import {$inlineStylesFromStyleSheets} from './inlineStylesFromStyleSheets'; import {$runImport} from './runImport'; import {selBase} from './sel'; /** * Configuration for {@link DOMImportExtension}. * * @experimental */ export interface DOMImportConfig { /** * The ordered list of rules compiled into the import dispatcher. * Entries can be raw {@link DOMImportRule}s or a * {@link CompiledOverlayRules} produced by {@link defineOverlayRules} * (the latter is inlined at its position in the list — useful for * libraries that already publish a compiled overlay). * * **Rules are evaluated in list order.** For a given DOM node the * dispatcher visits every rule whose `match` accepts that node, front * to back, and the first one that returns without calling `$next()` * decides the outcome. "Higher priority" and "earlier in this list" * mean the same thing. * * **Composition prepends.** `mergeConfig` puts `partial.rules` in * FRONT of the rules accumulated so far, and configs are merged in * dependency order — a dependency's contribution is merged before the * contribution of the extension that depends on it. So: * * - Each contributor's array is inlined as one contiguous chunk that * keeps its own internal order: within a single * `configExtension(DOMImportExtension, {rules})` call the first * entry has the highest priority. * - The chunks are ordered from the most dependent contributor to the * least. An extension's rules therefore outrank the rules * contributed by its dependencies, and rules passed directly to * `buildEditorFromExtensions` (merged last of all) outrank every * extension's. This extension's own default entry * ({@link DefaultHoistRule}) is the base of the list and so is * always tried last. * * The relative order of two extensions where neither transitively * depends on the other falls out of the topological sort and is not * part of the API. Make the intended precedence explicit by having the * overriding extension depend on the one whose rules it overrides. */ readonly rules: readonly DOMImportRuleEntry[]; /** * Default context pairs applied to every `$generateNodesFromDOM` call. * Per-call overrides can be supplied via * {@link GenerateNodesFromDOMOptions.context}. */ readonly contextDefaults: readonly ImportContextPairOrUpdater[]; /** * Middleware run on the DOM before walking begins, mutating it in * place. Each function receives `$next` and may wrap or short-circuit * the rest of the chain (see {@link DOMPreprocessFn}). The default * config registers {@link $inlineStylesFromStyleSheets} (resolves * `