`) reports as captured too.
*
* The walk aborts at the first DOM node that corresponds to a Lexical
* node in `editor` — that boundary is the implicit owner of the subtree's
* selection, so a captureSelection marker above it (in non-Lexical
* scaffolding around the editor) does not leak in.
*
* DecoratorNode DOM is marked with `setDOMUnmanaged({captureSelection:
* true})` by the reconciler, so decorator subtrees also report as
* captured here.
*
* @experimental
*/
export function isDOMCapturingSelection(
elementDom: Node & LexicalPrivateDOM,
editor: LexicalEditor,
): boolean {
let dom: (Node & LexicalPrivateDOM) | null = elementDom;
while (dom != null) {
if (dom.__lexicalCapturedSelection === true) {
return true;
}
// @experimental named-slots. A decorator host's slot container is a
// key-less scaffolding wrapper made contentEditable so its Lexical-managed
// content stays editable. Walking up from inside a slot would otherwise
// reach the decorator host's captured-selection flag and misread the slot
// as foreign-captured DOM, suppressing Lexical's input / selection
// handling. The container is a capturing boundary: stop here.
if (isHTMLElement(dom) && dom.hasAttribute('data-lexical-slot')) {
return false;
}
if (getNodeKeyFromDOMNode(dom, editor) !== undefined) {
return false;
}
dom = getParentElement(dom);
}
return false;
}
/**
* @internal
*
* Object.hasOwn ponyfill
*/
/**
* @internal
*/
export function hasOwnStaticMethod(
klass: Klass
,
k: keyof Klass,
): boolean {
return hasOwnKey(klass, k) && klass[k] !== LexicalNode[k];
}
/** @internal */
function isAbstractNodeClass(klass: Klass): boolean {
if (!(klass === LexicalNode || klass.prototype instanceof LexicalNode)) {
let ownNodeType = '';
let version = '';
try {
ownNodeType = klass.getType();
} catch (_err) {
// ignore
}
try {
if (LexicalEditor.version) {
version = JSON.parse(LexicalEditor.version);
}
} catch (_err) {
// ignore
}
invariant(
false,
'%s (type %s) does not subclass LexicalNode from the lexical package used by this editor (version %s). All lexical and @lexical/* packages used by an editor must have identical versions. If you suspect the version does match, then the problem may be caused by multiple copies of the same lexical module (e.g. both esm and cjs, or included directly in multiple entrypoints).',
klass.name,
ownNodeType,
version,
);
}
return (
klass === DecoratorNode || klass === ElementNode || klass === LexicalNode
);
}
export interface OwnStaticNodeConfig {
klass: Klass;
ownNodeType: undefined | string;
ownNodeConfig:
| undefined
| StaticNodeConfigValue;
/**
* Whether `klass` declared `$config()` itself.
*
* `ownNodeConfig` is the config the class *resolves* to, which for a class
* that declared none is its ancestor's — reached through the inherited
* method, and a fresh object each time, since `$config()` builds its result
* per call. Anything walking the chain has to tell the two apart or it
* attributes an ancestor's declarations to the subclass and counts them
* twice; see {@link iterStaticNodeConfigChain}.
*/
declaresOwnConfig: boolean;
}
/**
* Everything derived once per node class: the `$config()` result and what is
* compiled from it. One record in one map, so a serialization path that needs
* a compiled table does not chase a second and third WeakMap keyed by the same
* class, and there is a single place to populate.
*
* `compiled` is filled in *after* the record is cached, because compiling walks
* the class chain and re-enters this cache for `klass` itself. It is
* `undefined` only inside that window — a record whose compilation threw is
* dropped rather than left behind — and nothing that runs during compilation
* reads it, so {@link getCompiled} treats finding it missing as the error it
* is: a `$config()` body serializing a node of the class being built.
*/
interface NodeClassRecord {
readonly config: OwnStaticNodeConfig;
composed: undefined | ComposedSchema;
compiled: undefined | CompiledNodeClass;
/** DEV only: whether validateOwnFields has run for this class. */
ownFieldsValidated: boolean;
}
/** What a class's serialization runs on, compiled once at registration. */
interface CompiledNodeClass {
/** The export-direction table (see {@link compileGetters}). */
readonly getters: readonly CompiledGetter[];
/** The import-direction table (see {@link compileSetters}). */
readonly setters: readonly CompiledSetter[];
/**
* The flat NodeStates the class carries, applied by {@link $applyJSONSetters}
* before the setters — and before a generated parser, which knows nothing of
* them: what a node carries in state is not known when code is generated,
* the same reason the export side appends it around the generated literal.
*/
readonly flatStates: readonly AnyStateConfig[];
/**
* The generated JSON functions this class runs — its own, or an ancestor's
* where they still apply — or `null` for a class that walks (see
* {@link resolveGenerated}).
*/
readonly generated: null | GeneratedJSON;
/**
* The compact form's omission test for one property, by key — the same three
* comparisons {@link $writeJSONGetters} makes, from the same table.
*
* Generated code states each property's comparison as source, which it can
* do for a default that has a literal a value could be `===`. For one that
* does not — a reference-typed default, or a literal the schema compares
* with an `isEqual` of its own — the comparison is this call instead, so the
* generated form omits exactly what the walk omits rather than having to
* choose between writing the property regardless and giving up the form.
*
* Handed to the exporter by {@link $generatedExportJSON} rather than reached
* for: the generated module holds no value import at all, since importing
* the node classes it was generated from would be a cycle.
*/
readonly isCompactDefault: CompactDefaultTest;
}
/**
* Whether the compact form omits `value` for the property named `key` — see
* {@link CompiledNodeClass.isCompactDefault}.
*
* @internal
*/
export type CompactDefaultTest = (key: string, value: unknown) => boolean;
// A WeakMap so dynamically created node classes (tests, HMR reloads) stay
// collectable — more so now that one record pins a class's composed schema,
// both compiled tables, and every prototype method they resolved.
const NODE_CLASS_CACHE = new WeakMap, NodeClassRecord>();
/**
* The cache record for a node class, building it (and injecting the class's
* synthesized statics) on first use.
*/
function getNodeClassRecord(klass: Klass): NodeClassRecord {
const cached = NODE_CLASS_CACHE.get(klass);
return cached !== undefined ? cached : buildNodeClassRecord(klass);
}
/**
* A class's compiled tables. {@link buildNodeClassRecord} fills them before it
* returns, so the one way to find them missing is to serialize a node of the
* class from inside its own `$config()`.
*/
function getCompiled(record: NodeClassRecord): CompiledNodeClass {
const {compiled} = record;
invariant(
compiled !== undefined,
'%s is still being registered: a $config() must not serialize a node of its own class',
record.config.klass.name,
);
return compiled;
}
// Brands a getType() closure that Lexical synthesized (as opposed to a
// user-defined static getType()). buildNodeClassRecord uses this to avoid
// re-entering a synthesized closure while deriving a node's type, which would
// otherwise recurse infinitely for subclasses under compiled class output.
const SYNTHESIZED_GET_TYPE: unique symbol = Symbol(
'lexical.synthesizedGetType',
);
// TextNode.length > 0 will only be true if the compiler output
// is not ES6 compliant, in which case we can not provide this
// warning. We also can't reliably provide this warning if the output
// has been optimized because `arg=undefined` parameter defaults can
// be stripped.
//
// A function declared side-effect free (so the build annotates the call
// below) rather than an inline expression: the property reads on the node
// classes are a side effect to bundlers, which would pin the classes into
// every development bundle that imports the module.
/** @__NO_SIDE_EFFECTS__ */
function isUnoptimizedDevBuild(): boolean {
return (
__DEV__ &&
// constructor(key=undefined)
TabNode.length === 0 &&
// constructor(text='', key?: NodeKey)
TextNode.length === 0 &&
// Class name mangling is another signal that this may be unreliable
TextNode.name === 'TextNode'
);
}
const IS_UNOPTIMIZED_DEV_BUILD = isUnoptimizedDevBuild();
/**
* A precompiled step for applying one of a node's serialized schema properties
* in {@link LexicalNode.updateFromJSON}: a field applied through a named setter
* (`set` by default, or the name recorded with `withAccessors`), or
* assigned directly. Compiled once per class and cached so the base
* updateFromJSON iterates an array and applies each directly, without walking
* the class chain or materializing an intermediate parsed object on every call.
*
* A flat NodeState is serialized at the top level alongside these, but is not
* one of them: it is applied through the single {@link $setState} entry point,
* from the class's own list of them (see {@link CompiledNodeClass}), before
* any of these run.
*/
type CompiledSetter =
| {
readonly kind: 'field';
readonly key: string;
readonly schema: AnySerializationSchema;
// Resolved once at compile time so applying a field is a direct call
// rather than a per-node string-keyed method lookup.
readonly setter: (this: LexicalNode, value: unknown) => LexicalNode;
}
| {
// The fast path: the property *is* a node field, declared with
// withField, so applying it is an assignment with no method call and no
// getWritable() — $applyJSONSetters already holds the writable node.
readonly kind: 'ownField';
readonly key: string;
readonly schema: AnySerializationSchema;
readonly field: string;
/** Maps the parsed value to the stored one; see {@link SchemaField}. */
readonly setterTable?: {readonly [key: string]: unknown};
};
const EMPTY_SETTERS: readonly CompiledSetter[] = [];
/**
* How `klass` reaches one direction of a serialized property: the
* {@link SchemaField} unchanged when the direct field access holds, and the
* name of the accessor it stands in for when it does not.
*
* A `SchemaField` that names a `method` is saying the two are equivalent *for
* the class that declared it*. A subclass that overrides that method has said
* otherwise, and it wins: before the property had a schema both JSON methods
* went through the accessor, so overriding one changed the node's
* serialization, and compiling the accessor away would silently take that back.
*
* The comparison resolves through each prototype chain, so it catches an
* override anywhere between the declaring class and this one.
*
* `conventional` is the `get`/`set` name for this direction, used
* when the field names no `method` of its own — which is the common case, and
* why nearly every declaration can leave it out. A class that has no such
* method defers to nothing, because both prototypes then resolve `undefined`
* and compare equal; there is no separate way to say "bypass the accessor",
* and deliberately so: a subclass that overrode one always meant to be asked.
*
* Shared with the codegen in `scripts/generate-node-json.mjs`, which has to
* make the identical choice or its literal would describe a different node.
*
* @internal
*/
export function resolveSchemaField(
klass: Klass,
key: string,
accessor: T,
conventional: string,
): T | string {
const method = accessor.method === undefined ? conventional : accessor.method;
if (__DEV__ && accessor.method !== undefined) {
// Only a name the declaration spelled out: a derived one that resolves to
// nothing is the ordinary "this property has no accessor" case, while a
// spelled one that resolves to nothing is a typo, and a typo here is
// silent — both prototypes read `undefined`, compare equal, and the field
// access is kept, quietly retiring the override guard this option exists
// to provide. DEV-only: what it catches is a mistaken intent, and the
// behavior either way is well defined.
invariant(
typeof (klass.prototype as unknown as Record)[method] ===
'function',
'%s: serialization schema field "%s" names a method %s() that the node does not have',
klass.name,
key,
method,
);
}
const declaringKlass = getComposedSchema(klass).declaredBy.get(key);
if (declaringKlass === undefined) {
return accessor;
}
const prototype = klass.prototype as unknown as Record;
const declared = declaringKlass.prototype as unknown as Record<
string,
unknown
>;
// The conventional name is guarded alongside the spelled one, because a
// spelled accessor is typically a wrapper *over* the conventional one rather
// than a replacement for it: ElementNode's `textFormat` names
// `getSerializedTextFormat`, which computes its result from `getTextFormat`.
// Guarding only the wrapper would keep the direct field read for a subclass
// that overrides `getTextFormat` — the accessor that has been public since
// long before this schema existed, and the one such a subclass would
// naturally reach for — silently exporting the stored field instead of what
// the node says the property is.
//
// Only the guard widens: the property is still read through the spelled
// accessor when it is reclaimed, since that is the one whose return type is
// the serialized form. A conventional name the class does not have costs
// nothing, as both prototypes then resolve `undefined` — which is the usual
// case for a spelled accessor (`getText` alongside TextNode's
// `getTextContent`, `getUrl` alongside LinkNode's `getURL`).
return isUnchangedFrom(prototype, declared, method) &&
isUnchangedFrom(prototype, declared, conventional)
? accessor
: method;
}
/** Whether `klass` inherits `name` from the class that declared the property. */
function isUnchangedFrom(
prototype: Record,
declared: Record,
name: string,
): boolean {
return prototype[name] === declared[name];
}
/**
* The default setter name for a serialized property, e.g. `foo` → `setFoo`.
*
* Exported for the same reason {@link resolveSchemaField} is: the codegen in
* `scripts/generate-node-json.mjs` has to derive the identical name, and a
* second copy of the rule is a second thing that can drift.
*
* @internal
*/
export function defaultSetterName(key: string): string {
return `set${key.charAt(0).toUpperCase()}${key.slice(1)}`;
}
/**
* The default getter name for a serialized property, e.g. `foo` → `getFoo`.
*
* @see {@link defaultSetterName}
* @internal
*/
export function defaultGetterName(key: string): string {
return `get${key.charAt(0).toUpperCase()}${key.slice(1)}`;
}
/**
* The serialization schema fields and flat NodeStates a node class serializes,
* composed across its config chain. Every consumer of "what does this class
* serialize" derives from this one walk so they cannot disagree about
* precedence: a subclass field overrides an ancestor's, while a re-declared
* flat state keeps the ancestor's config (matching createSharedNodeState).
*
* @internal
*/
export interface ComposedSchema {
/**
* Fields ordered most-derived first — the order properties are written in,
* which the hand-written exportJSON methods this replaces also produced for
* the core classes, and which some tests compare as strings.
*/
readonly fieldsDerivedFirst: readonly (readonly [
string,
AnySerializationSchema,
])[];
/**
* The same fields ordered ancestors first — the order setters are applied
* in, so a base property is set before the subclass properties that may
* depend on it.
*/
readonly fieldsBaseFirst: readonly (readonly [
string,
AnySerializationSchema,
])[];
/** Flat NodeStates, ancestors first. */
readonly flatStates: readonly AnyStateConfig[];
/**
* The class whose `$config` declared each field's winning schema, which is
* where {@link SchemaFieldBase.method} is measured from: a method the declaring
* class and the node's class resolve differently is one somebody overrode in
* between.
*/
readonly declaredBy: ReadonlyMap>;
/**
* The same winning schemas by key, which is what a class's generated code
* is built from at registration: the lookup tables it reads are these
* schemas' own objects, so the generated module holds no copy of any.
*/
readonly fields: ComposedSchemaFields;
}
const EMPTY_COMPOSED_SCHEMA: ComposedSchema = {
declaredBy: new Map(),
fields: new Map(),
fieldsBaseFirst: [],
fieldsDerivedFirst: [],
flatStates: [],
};
function composeSchema(klass: Klass): ComposedSchema {
// One walk of the config chain (iterStaticNodeConfigChain honors an explicit
// `extends` and severed static prototype chains, e.g. Babel's loose class
// transform), collected per class so both orderings fall out of it.
const fieldGroups: (readonly (readonly [
string,
AnySerializationSchema,
])[])[] = [];
const groupKlasses: Klass[] = [];
const stateGroups: (readonly AnyStateConfig[])[] = [];
for (const {klass: currentKlass, ownNodeConfig} of iterStaticNodeConfigChain(
klass,
)) {
const json = ownNodeConfig && ownNodeConfig.json;
groupKlasses.push(currentKlass);
if (__DEV__ && json) {
// Only a `nodeSchema` names fields. Anything else contributes nothing,
// which would silently turn off the node's whole serialization, the one
// thing declaring `json` is for.
invariant(
json.meta.kind === 'node',
'%s: $config json must be built with nodeSchema()({...}); got a %s schema',
// The class whose `$config` declared it, not the one being composed:
// naming the subclass sent a reader to a class that declared nothing
// wrong and never named the one they have to edit.
currentKlass.name,
json.meta.kind,
);
}
fieldGroups.push(
json && json.meta.kind === 'node'
? (Object.entries(json.meta.fields) as (readonly [
string,
AnySerializationSchema,
])[])
: [],
);
const flat: AnyStateConfig[] = [];
if (ownNodeConfig && ownNodeConfig.stateConfigs) {
for (const required of ownNodeConfig.stateConfigs) {
if ('stateConfig' in required && required.flat) {
flat.push(required.stateConfig);
}
}
}
stateGroups.push(flat);
}
// Most-derived first, first write wins, so a subclass field overrides an
// ancestor's and keeps the subclass's position.
const derivedFirst = new Map();
for (let i = 0; i < fieldGroups.length; i++) {
for (const [key, schema] of fieldGroups[i]) {
if (!derivedFirst.has(key)) {
derivedFirst.set(key, schema);
}
}
}
// The same winning schemas in ancestors-first order.
const baseFirst = new Map();
const declaredBy = new Map>();
const flatStates = new Map();
for (let i = fieldGroups.length - 1; i >= 0; i--) {
for (const [key, schema] of fieldGroups[i]) {
const winner = derivedFirst.get(key);
if (winner !== undefined && !baseFirst.has(key)) {
baseFirst.set(key, winner);
}
// The most basal class declaring the *winning* schema, which is not the
// same as the most basal one declaring the key: a subclass that
// re-declares an inherited field is where that field now comes from.
// Compared by identity because a subclass with no `$config` of its own
// inherits the method and so reports its ancestor's fields as its own —
// recording it here would measure SchemaField.method against a class
// that never declared the property, and every override would look like
// no override at all.
if (winner !== undefined && schema === winner && !declaredBy.has(key)) {
declaredBy.set(key, groupKlasses[i]);
}
}
for (const stateConfig of stateGroups[i]) {
if (!flatStates.has(stateConfig.key)) {
flatStates.set(stateConfig.key, stateConfig);
}
}
}
if (__DEV__) {
for (const key of flatStates.keys()) {
// Export writes the flat state over the field, import applies the field
// over the state, so such a property would flip on every round trip.
invariant(
!derivedFirst.has(key),
'%s: "%s" is declared both as a serialization schema field and as a flat NodeState; it must be one or the other',
klass.name,
key,
);
}
}
return derivedFirst.size === 0 && flatStates.size === 0
? EMPTY_COMPOSED_SCHEMA
: {
declaredBy,
fields: baseFirst,
fieldsBaseFirst: [...baseFirst],
fieldsDerivedFirst: [...derivedFirst],
flatStates: [...flatStates.values()],
};
}
/**
* The composed serialization schema of a node class, compiled once per class.
*
* @internal
*/
export function getComposedSchema(klass: Klass): ComposedSchema {
const record = getNodeClassRecord(klass);
if (record.composed === undefined) {
record.composed = composeSchema(klass);
}
return record.composed;
}
/**
* Every node-specific property of a class's serialized JSON — its composed
* serialization schema fields plus any flat NodeState whose value schema is
* introspectable — keyed by serialized property name.
*
* @internal
*/
export function getComposedSchemaFields(
klass: Klass,
): Record {
const {fieldsDerivedFirst, flatStates} = getComposedSchema(klass);
const fields: Record = {};
for (const stateConfig of flatStates) {
if (stateConfig.schema) {
fields[stateConfig.key] = stateConfig.schema;
}
}
for (const [key, schema] of fieldsDerivedFirst) {
fields[key] = schema;
}
return fields;
}
/**
* What the compact form may drop for one property, resolved with the accessor
* so writing the compact form needs no second pass over the schema: a derived
* property (`{setter: null}`) is bytes nothing will ever read, and a value
* equal to the default parsing would restore says nothing either.
*/
interface CompactRule {
/**
* The schema this entry was compiled from, carried for the same reason the
* setter side carries it: {@link sameCompiledTables} compares it by identity
* to decide whether one class's generated code describes another's node.
* Everything the emitted code says about a property beyond its kind and
* field — which accessor it calls, which predicate gates it, what its domain
* admits — comes from here, and a class that re-declares a property declares
* a new schema, so identity is what separates "inherited unchanged" from
* "restated, possibly differently".
*/
readonly schema: AnySerializationSchema;
readonly derived: boolean;
/**
* What parsing restores for an absent property, and so what the compact form
* omits. Resolved with the accessor so the write path needs no second look at
* the schema.
*/
readonly defaultValue: unknown;
/**
* The schema's own equality, hoisted, or `undefined` where identity is the
* whole answer.
*
* Kept apart from the value so the common case is a comparison rather than a
* call. Only {@link arrayValue} and {@link objectValue} declare one — every
* primitive domain, which is nearly every serialized property, compares by
* identity — and the write path checks that first either way, so a declared
* equality costs a call only for a value that is not already identical.
*/
readonly isEqual: undefined | ((a: unknown, b: unknown) => boolean);
}
/**
* The mirror of {@link CompiledSetter} for the export direction: one of a
* node's serialized properties read back through a named getter (`get`
* by default, or the name recorded with `withAccessors`). Compiled once per
* class so {@link LexicalNode.exportJSON} writes an object without walking the
* class chain on every call.
*/
type CompiledGetter = CompactRule &
(
| {
readonly kind: 'method';
readonly key: string;
// Resolved once at compile time, like the setter counterpart.
readonly getter: (this: LexicalNode) => unknown;
}
| {
// The fast path, mirroring the setter side: the property *is* a node
// field, so reading it is a property access — no method call and no
// version resolution (see $writeJSONGetters).
readonly kind: 'ownField';
readonly key: string;
readonly field: string;
/** Maps the stored value to the serialized one; see {@link SchemaField}. */
readonly getterTable?: {readonly [key: string]: unknown};
/**
* Resolved once at compile time, like the method getter: the predicate
* named by {@link SchemaGetterField.when}, which gates writing this
* property at all.
*/
readonly when?: (this: LexicalNode) => boolean;
}
);
const EMPTY_GETTERS: readonly CompiledGetter[] = [];
/**
* The accessor `klass` reads a serialized property through: `null` for a
* property declared import-only, the {@link SchemaGetterField} when the direct
* field access holds, and otherwise the name of the getter method.
*
* This is the whole of the export direction's resolution rule, in one place:
* {@link compileGetters} builds the walk's table from it, and the codegen in
* `scripts/generate-node-json.mjs` emits its literal from it, so the two
* cannot describe different nodes.
*
* @internal
*/
export function resolveGetterAccessor(
klass: Klass,
key: string,
schema: AnySerializationSchema,
): null | string | SchemaGetterField {
const declared = schema.getter;
if (declared === null) {
return null;
}
// `=== undefined`, not `||`: an empty recorded name is a mistake, not a
// request for the conventional one, and resolving it silently would apply
// some other accessor that happens to exist.
const named = declared === undefined ? defaultGetterName(key) : declared;
// A subclass override of the accessor a field stands in for reclaims the
// property; otherwise this is the field unchanged.
return isSchemaField(named)
? resolveSchemaField(klass, key, named, defaultGetterName(key))
: named;
}
/**
* The setter mirror of {@link resolveGetterAccessor}.
*
* @internal
*/
export function resolveSetterAccessor(
klass: Klass,
key: string,
schema: AnySerializationSchema,
): null | string | SchemaSetterField {
const declared = schema.setter;
if (declared === null) {
return null;
}
const named = declared === undefined ? defaultSetterName(key) : declared;
return isSchemaField(named)
? resolveSchemaField(klass, key, named, defaultSetterName(key))
: named;
}
function compileGetters(klass: Klass): readonly CompiledGetter[] {
const prototype = klass.prototype as unknown as Record;
const fields = new Map();
// Most-derived first, which reproduces the order TextNode and ElementNode's
// hand-written exportJSON produced for their own fields. It does *not*
// reproduce it for a subclass: `{...super.exportJSON(), ownProps}` put the
// subclass's properties last, and they now come first, with `type`/`version`
// appended afterwards. The JSON is equivalent — key order carries no
// meaning — but `JSON.stringify(editorState.toJSON())` is byte-different for
// an existing document, so anything comparing serialized strings sees a
// change. Composition already resolved each key to exactly one schema, so a
// subclass that re-declares an inherited field replaces it outright,
// accessor names included — TabNode repeats `getter: 'getTextContent'` for
// that reason.
for (const [key, schema] of getComposedSchema(klass).fieldsDerivedFirst) {
const getter = resolveGetterAccessor(klass, key, schema);
if (getter === null) {
// Declared import-only; the property is written by an exportJSON
// override, or not written at all.
continue;
}
if (isSchemaField(getter)) {
const getterName = getter.field;
// withField: the property *is* this node field, so reading it is a
// property access — no method call, and no getLatest() (see
// $writeJSONGetters). The field only exists on a constructed node, so
// unlike the method below it is checked on first read.
//
// `__proto__` names the prototype rather than a field, so reading it
// would write the node's whole prototype chain into the JSON (and throw
// on stringify); the setter mirror rejects it for the same reason.
invariant(
getterName !== '__proto__',
'%s: serialization schema field "%s" cannot be read from __proto__',
klass.name,
key,
);
const whenName = getter.when;
let when: undefined | ((this: LexicalNode) => boolean);
if (whenName !== undefined) {
const predicate = prototype[whenName];
// Same reasoning as the accessor check below: a predicate that does
// not resolve would silently drop the property from every export.
invariant(
typeof predicate === 'function',
'%s: serialization schema field "%s" names a predicate %s() that the node does not have',
klass.name,
key,
whenName,
);
when = predicate as (this: LexicalNode) => boolean;
}
fields.set(key, {
defaultValue: schema.defaultValue,
derived: schema.setter === null,
field: getterName,
getterTable: getter.getterTable,
isEqual: schema.isEqual,
key,
kind: 'ownField',
schema,
when,
});
continue;
}
const method = prototype[getter];
// A field the class cannot read would be silently missing from every
// export — data loss, not a degraded experience — so this fails in every
// build, not only in DEV. It runs once per class at registration.
invariant(
typeof method === 'function',
'%s: serialization schema field "%s" has no getter %s(); name one with withAccessors({getter}) or declare {getter: null} if it is deliberately not exported',
klass.name,
key,
getter,
);
fields.set(key, {
defaultValue: schema.defaultValue,
derived: schema.setter === null,
getter: method as (this: LexicalNode) => unknown,
isEqual: schema.isEqual,
key,
kind: 'method',
schema,
});
}
return fields.size === 0 ? EMPTY_GETTERS : [...fields.values()];
}
/**
* Read a node field by name. A node type has no index signature, so a dynamic
* property access needs the widening cast; keeping it in one named place
* leaves the call sites cast-free.
*/
function ownFieldRecord(node: LexicalNode): Record {
return node as unknown as Record;
}
/**
* Check every field name a class's schema declares (`withField`, or an
* accessor named `__something`) against a real instance of it. A misspelled
* one is silent, total loss of that property — nothing is ever exported, and
* importing writes a field the node does not read.
*
* Both compiled tables are checked, not just the caller's. A getter name and a
* setter name are declared independently — `withAccessors` takes them
* separately, and either direction may be `null` — so a class can carry an
* `ownField` entry on one side and not the other. Checking only the direction
* that happened to serialize first would leave the other side's name unchecked
* for the life of the process.
*
* Unlike the method-name checks in {@link compileGetters} / {@link compileSetters},
* this one is DEV-only. A field exists on a constructed node, not on the
* prototype, so it cannot be resolved when the class is registered: the check
* needs an instance, which means it can only run on a serialization path.
* Registration-time checks have no such cost — they run once, on the class
* alone — which is why those fail in every build and this does not. Running it
* once per class keeps it off the per-node path in DEV too.
*/
function validateOwnFields(record: NodeClassRecord, node: LexicalNode): void {
if (record.ownFieldsValidated) {
return;
}
const {klass} = record.config;
const fields = ownFieldRecord(node);
const {getters, setters} = getCompiled(record);
for (const entries of [getters, setters]) {
for (const entry of entries) {
if (entry.kind === 'ownField') {
// A field declared without an initializer (`__caption?: string`) is
// not an own property until something assigns it, and this cannot
// tell that from a misspelling — so the message says which fix
// applies to which.
invariant(
hasOwnKey(fields, entry.field),
'%s: serialization schema field "%s" names a node field %s that the node does not have. Check the spelling; a field declared without an initializer is not an own property until the constructor assigns it',
klass.name,
entry.key,
entry.field,
);
}
}
}
// Recorded only once the whole pass has run. The editor catches what a
// serialization path throws — parseEditorState routes it to `_onError`
// rather than rethrowing — so marking the class first would let one caught
// failure retire the check with every field after it still unexamined.
record.ownFieldsValidated = true;
}
/**
* Write the serialized properties a node's schema declares, reading each
* through its getter. A getter that returns `undefined` omits the property:
* absent and explicitly-undefined are indistinguishable once the JSON is
* stringified, so this is how an optional (or conditionally persisted)
* property is expressed.
*
* Compaction is applied here rather than as a pass over the finished object:
* with `compact`, a property the parser derives is skipped without calling its
* getter at all, and one whose value equals the schema default parsing would
* restore is simply not written. That leaves nothing for a later pass to
* inspect, so a node that generates its own `exportJSON` can inline the same
* decisions and never consult the schema at runtime.
*
* A field is read off `node` as given, with no `getLatest()`. Serializing a
* graph only needs to resolve its root: every node the walk reaches after that
* comes from the EditorState's node map — `$getRoot()`, `getChildren()`,
* `$getSlot()` — so it is already the current version. (An ephemeral node, such
* as the sliced clone the clipboard walk exports, is deliberately not in the
* map, and reading it as given is the only correct thing to do.)
*
* @internal
*/
export function $writeJSONGetters(
node: LexicalNode,
json: {[key: string]: unknown},
compact: boolean,
): void {
const record = getNodeClassRecord(node.constructor as Klass);
if (__DEV__) {
validateOwnFields(record, node);
}
$writeCompiledGetters(node, getCompiled(record).getters, json, compact);
}
/**
* The body of {@link $writeJSONGetters}, over a table the caller has already
* resolved — which {@link $exportNodeJSONOnce} has, having just asked the same
* record whether the class carries generated code.
*/
function $writeCompiledGetters(
node: LexicalNode,
getters: readonly CompiledGetter[],
json: {[key: string]: unknown},
compact: boolean,
): void {
for (let i = 0; i < getters.length; i++) {
const entry = getters[i];
if (compact && entry.derived) {
// Nothing will read it back, so the compact form does not even call the
// getter to find out what it would have written.
continue;
}
let value: unknown;
if (entry.kind === 'ownField') {
const stored = ownFieldRecord(node)[entry.field];
// hasOwnKey for the same reason the import mirror uses it: the field
// holds whatever was stored, which for a schema whose domain is wider
// than the table's keys can be a name Object.prototype also carries —
// writing that method into the JSON as a value. A genuine miss still
// yields `undefined`, exactly as the bare lookup did, which is also what
// the generated exporters emit for one.
value =
entry.getterTable === undefined
? stored
: hasOwnKey(entry.getterTable, stored as string)
? entry.getterTable[stored as string]
: undefined;
} else {
value = entry.getter.call(node);
}
if (entry.kind === 'ownField' && entry.when !== undefined) {
// A conditionally-persisted property: written only when it differs from
// the schema default *and* the node's predicate agrees. The default is
// tested first, so the predicate stays off the common path — a property
// holding what parsing would restore is omitted without asking. Written
// out rather than routed through isSchemaDefault for the same reason the
// compact comparison below is: this runs per property per node.
//
// Generated code hoists a predicate shared by several properties and so
// calls it once where this calls it per property, which is why it is
// required to be pure.
if ($isCompactDefaultFor(entry, value) || !entry.when.call(node)) {
value = undefined;
}
}
if (compact && $isCompactDefaultFor(entry, value)) {
// The compact form omits, because its output is for storage and an
// object-level consumer (a structured clone into IndexedDB) counts keys
// the way stringify counts bytes.
continue;
}
// The legacy form writes unconditionally, `undefined` included:
// JSON.stringify omits an undefined-valued property, so the serialized
// bytes are identical either way, and present-with-undefined is the shape
// the hand-written exporters always had — main's ListItemNode writes
// `checked: this.getChecked()` on every non-checklist item, TableNode
// writes `colWidths: undefined` by explicit ternary. Writing the key also
// keeps every export of a class on one object shape, and is one branch
// less per property.
json[entry.key] = value;
}
}
/**
* The compact form's rule for one property: omit it when the value is what
* parsing would restore.
*
* Inline rather than `isSchemaDefault(rule.schema, value)`: this runs per
* property per node, and for a primitive domain — nearly every serialized
* property — the whole answer is the identity comparison, with the declared
* equality reached only for a value that is not already identical.
*
* The one rule, in one place, because two implementations write this form: the
* walk calls it per property, and a generated exporter calls it through
* {@link CompiledNodeClass.isCompactDefault} for the properties whose defaults
* it could not state as source. A second copy of these three comparisons is
* exactly the drift that would make the two forms disagree.
*/
function $isCompactDefaultFor(rule: CompactRule, value: unknown): boolean {
const {defaultValue, isEqual} = rule;
return (
value === undefined ||
value === defaultValue ||
(isEqual !== undefined && isEqual(value, defaultValue))
);
}
/**
* {@link CompiledNodeClass.isCompactDefault} for one class's getter table.
*
* The key index is built on first use rather than with the table: only a
* property whose default has no literal the generated code could compare
* against ever reaches this, which no built-in node has.
*/
function compactDefaultTest(
getters: readonly CompiledGetter[],
): CompactDefaultTest {
let byKey: undefined | Map;
return (key, value) => {
if (byKey === undefined) {
byKey = new Map(getters.map(entry => [entry.key, entry]));
}
const entry = byKey.get(key);
// A key with no entry is one the generated code and the table disagree
// about, which `sameCompiledTables` is what prevents; omitting it would
// silently drop a property, so the value is written.
return entry !== undefined && $isCompactDefaultFor(entry, value);
};
}
/**
* The stored form of a compiled property's schema default — what an `setterTable`
* table maps the default to, or the default itself where there is no table.
* A table always has the entry: {@link compileSetters} refuses one without
* it when the class is registered, as {@link verifyTableCoversDomain} refuses
* it for a generated class, because a default with no stored form left the
* raw default — a string, for a numeric field — as what a miss wrote.
*/
function setterDefault(entry: {
readonly setterTable?: {readonly [key: string]: unknown};
readonly schema: AnySerializationSchema;
}): unknown {
const {setterTable, schema} = entry;
const {defaultValue} = schema;
return setterTable === undefined
? defaultValue
: setterTable[String(defaultValue)];
}
function compileSetters(klass: Klass): readonly CompiledSetter[] {
// A class instance type has no index signature, so reading a setter by
// name needs the widening cast.
const prototype = klass.prototype as unknown as Record;
const fields = new Map();
const {fieldsBaseFirst} = getComposedSchema(klass);
// Applied ancestors-first: a base property is set before the subclass
// properties that may depend on it.
for (const [key, schema] of fieldsBaseFirst) {
const setter = resolveSetterAccessor(klass, key, schema);
if (setter === null) {
// Declared export-only: the value is derived from other properties on
// the way in (ListNode's `tag` follows from `listType`).
continue;
}
if (isSchemaField(setter)) {
const setterName = setter.field;
// withField: the property *is* this node field, so applying it is an
// assignment — no method call, and no getWritable(), since the node
// $applyJSONSetters walks is writable by construction.
//
// `__proto__` would reparent the node rather than write a property, so
// it is never a field name; the rest is checked on the prototype, where
// a class field declared with an initializer is not visible, so the
// getter mirror does the per-instance check.
invariant(
setterName !== '__proto__',
'%s: serialization schema field "%s" cannot be applied to __proto__',
klass.name,
key,
);
// A parsed value the table does not map is stored as the encoded
// default, so the table has to map every value the schema can produce
// — the default first of all, since a default it does not map has no
// stored form and the walk would write the raw default into the field.
// An enum's members are every value it produces; any other schema is
// checked for its default, the one value known here. Every build, like
// the setter check below: what it prevents is a value of the wrong type
// written into a field on import, silently, in production.
if (setter.setterTable !== undefined) {
const {setterTable} = setter;
const {meta} = schema;
for (const value of meta.kind === 'enum'
? meta.values
: [schema.defaultValue]) {
invariant(
hasOwnKey(setterTable, String(value)),
'%s: serialization schema field "%s" has no setterTable entry for %s, which its schema can produce; a parsed value the table does not map is stored as the encoded default, so the table must map every value the schema produces',
klass.name,
key,
JSON.stringify(value),
);
}
}
fields.set(key, {
field: setterName,
key,
kind: 'ownField',
schema,
setterTable: setter.setterTable,
});
continue;
}
const method = prototype[setter];
// A field the class cannot apply would be silently dropped from every
// import — it exports but never comes back — so, like the getter mirror,
// this fails in every build rather than only in DEV.
invariant(
typeof method === 'function',
'%s: serialization schema field "%s" has no setter %s(); name one with withAccessors or declare {setter: null} if it is derived on import',
klass.name,
key,
setter,
);
fields.set(key, {
key,
kind: 'field',
schema,
setter: method as (this: LexicalNode, value: unknown) => LexicalNode,
});
}
return fields.size === 0 ? EMPTY_SETTERS : [...fields.values()];
}
/**
* The generated JSON functions a node class runs, or `null` for a class the
* generated code does not describe.
*
* A class declares its own through `$config`, so the association is the same
* one its schema has. A subclass inherits them along with the schema, on one
* condition: that its compiled tables are the ones the code was generated from.
* Generated code reads the fields the declaring class resolved its properties
* to and calls the methods it resolved them to; a subclass that overrides an
* accessor a field stands in for, or declares a property of its own, resolves
* differently, and for it the code would be wrong. Comparing the two classes'
* tables entry for entry is what decides ({@link sameCompiledTables}) — they
* are the tables the walk would use, so what runs is always what the walk
* would have done. The declaring class is the most basal one in the chain that
* names the same functions, for the same reason `declaredBy` is.
*
* A `$config` of its own that names an ancestor's generated code is refused in
* DEV: inheriting is automatic where it applies, and where it does not, a
* declaration that silently ran the walk would leave the class believing it
* ships the code it named.
*/
function resolveGenerated(
klass: Klass,
ownNodeConfig:
| undefined
| StaticNodeConfigValue,
tables: Pick,
): null | GeneratedJSONFactory {
// The nearest declaration up the chain. A class with no `$config` of its own
// reads its ancestor's, declaration included, as its own; one whose `$config`
// names none inherits from the first ancestor that does.
// One walk for both questions: the nearest declaration, and the most basal
// class naming that same object. The second cannot precede the first, so
// the loop records the first it sees and then keeps overwriting the class
// for as long as later ancestors name it.
let declared: undefined | GeneratedJSONFactory;
let declaringKlass = klass;
for (const {
klass: currentKlass,
ownNodeConfig: config,
} of iterStaticNodeConfigChain(klass)) {
if (config && config.generated !== undefined) {
if (declared === undefined) {
declared = config.generated;
declaringKlass = currentKlass;
} else if (config.generated === declared) {
declaringKlass = currentKlass;
}
}
}
if (declared === undefined) {
return null;
}
if (declaringKlass === klass) {
return declared;
}
if (__DEV__) {
invariant(
!(
ownNodeConfig !== undefined &&
ownNodeConfig.generated === declared &&
hasOwnKey(klass.prototype as unknown as object, PROTOTYPE_CONFIG_METHOD)
),
'%s: $config names the generated JSON code that %s declared; generated code is inherited wherever it still applies, so omit it',
klass.name,
declaringKlass.name,
);
}
return sameCompiledTables(
tables,
getCompiled(getNodeClassRecord(declaringKlass)),
)
? declared
: null;
}
/**
* Whether two classes' compiled tables would run the same generated code:
* every entry the same kind for the same key, reading or writing the same
* field through the same tables, against the same schema.
*
* The schema is compared by identity in both directions, and it is what makes
* this sound rather than a list of the details anyone remembered to compare.
* Generated code says more about a property than its kind and field: which
* accessor method it calls, which `when` predicate gates writing it, what its
* domain admits. All of that comes from the schema, none of it is on the
* compiled entry, and a class that re-declares a property declares a new
* schema — so identity separates a property inherited unchanged from one
* restated, possibly differently, and a class that restates anything takes the
* walk instead.
*
* Identity is not too strict for an *override*, which is the case inheritance
* exists for: a subclass that overrides an accessor or a predicate without
* re-declaring the property shares the schema object, and the emitted code
* calls both by name, so the override is honored the way the walk honors it.
*/
function sameCompiledTables(
a: Pick,
b: Pick,
): boolean {
if (
a.getters.length !== b.getters.length ||
a.setters.length !== b.setters.length
) {
return false;
}
for (let i = 0; i < a.getters.length; i++) {
const x = a.getters[i];
const y = b.getters[i];
if (
x.kind !== y.kind ||
x.key !== y.key ||
x.schema !== y.schema ||
x.derived !== y.derived ||
x.isEqual !== y.isEqual ||
!Object.is(x.defaultValue, y.defaultValue) ||
// Whether a table is read, not which: the code reads its tables off the
// running class's schema when it is attached, and the schema comparison
// above has already settled which schema that is.
(x.kind === 'ownField' &&
y.kind === 'ownField' &&
(x.field !== y.field ||
(x.getterTable === undefined) !== (y.getterTable === undefined)))
) {
return false;
}
}
for (let i = 0; i < a.setters.length; i++) {
const x = a.setters[i];
const y = b.setters[i];
if (
x.kind !== y.kind ||
x.key !== y.key ||
x.schema !== y.schema ||
(x.kind === 'ownField' &&
y.kind === 'ownField' &&
(x.field !== y.field ||
(x.setterTable === undefined) !== (y.setterTable === undefined)))
) {
return false;
}
}
return true;
}
/**
* The generated JSON functions `klass` runs (see {@link resolveGenerated}), or
* `null`. For tests, which need to know whether a comparison against the walk
* is comparing anything.
*
* @internal
*/
export function getGeneratedJSON(
klass: Klass,
): null | GeneratedJSON {
return getCompiled(getNodeClassRecord(klass)).generated;
}
/**
* The schema-driven walk's export of `node` in the form asked for, without
* NodeState, in the key order the generated exporters reproduce. The legacy
* form is `children` first for an element, then every property the schema
* declares through {@link $writeJSONGetters}, then `type` and `version` — the
* order it has always had. The compact form is new and leads with `type`, so
* a node reads type-first and generated code can allocate the object on its
* fixed keys; key order is part of neither format, since parsing reads
* properties by name. What {@link LexicalNode.exportJSON} runs for a class
* without generated code, and what generated code is checked against.
*
* @internal
*/
export function $walkExportJSON(
node: LexicalNode,
compact: boolean,
): {[key: string]: unknown} {
const record = getNodeClassRecord(node.constructor as Klass);
if (__DEV__) {
validateOwnFields(record, node);
}
return $walkFromCompiled(node, getCompiled(record).getters, compact);
}
/** {@link $walkExportJSON} over an already-resolved getter table. */
function $walkFromCompiled(
node: LexicalNode,
getters: readonly CompiledGetter[],
compact: boolean,
): {[key: string]: unknown} {
const json: {[key: string]: unknown} = compact ? {type: node.__type} : {};
if ($isElementNode(node)) {
// Before the schema's properties, so that an element's JSON reads
// structure-first.
json.children = [];
}
$writeCompiledGetters(node, getters, json, compact);
if (!compact) {
json.type = node.__type;
// Deprecated and ignored on the way in; written only so the legacy form
// stays readable by older versions.
json.version = 1;
}
return json;
}
/**
* What the generated exporter writes for `node` in the form asked for, or
* `undefined` when its class has no generated code for that form and the
* schema-driven walk has to run instead.
*
* A generated exporter is only ever right for the exact accessors its class
* resolves, which is what {@link resolveGenerated} settled at registration.
* Both forms are generated — which properties the compact one drops depends on
* a node's values, but the rule does not, so each form is its own
* straight-line function. NodeState is not part of either: what a node carries
* is not known when the code is generated, so {@link LexicalNode.exportJSON}
* appends it to this result and to the walk's alike.
*
* @internal
*/
/**
* What a node exports in the form asked for: the generated exporter where its
* class has one for that form, and the schema-driven walk otherwise.
*
* One function because both halves start by resolving the class record, and
* asking twice — once to find there is no generated code, once to walk —
* repeated a WeakMap read and, in DEV, the own-field validation, per node per
* export. That is the path every node whose class the generator could not
* compile takes, which is most nodes outside the core.
*
* @internal
*/
export function $exportNodeJSONOnce(
node: LexicalNode,
compact: boolean,
): {[key: string]: unknown} {
const record = getNodeClassRecord(node.constructor as Klass);
const compiled = getCompiled(record);
const {generated, isCompactDefault} = compiled;
const exporter =
generated === null
? undefined
: compact
? generated.exportCompactJSON
: generated.exportJSON;
if (__DEV__) {
validateOwnFields(record, node);
}
return exporter === undefined
? $walkFromCompiled(node, compiled.getters, compact)
: compact
? (exporter as NonNullable)(
node,
isCompactDefault,
)
: (exporter as GeneratedJSON['exportJSON'])(node);
}
export function $generatedExportJSON(
node: LexicalNode,
compact: boolean,
): undefined | {[key: string]: unknown} {
const record = getNodeClassRecord(node.constructor as Klass);
const {generated, isCompactDefault} = getCompiled(record);
if (generated === null) {
return undefined;
}
// Each form is generated separately, so this picks a function rather than
// passing the flag on. The generator emits both forms for every class it can
// export at all — a property whose default it cannot state as source
// compares through `isCompactDefault` instead — but the field stays
// optional, so a value written by hand still falls back to the walk here.
const exporter = compact ? generated.exportCompactJSON : generated.exportJSON;
if (exporter === undefined) {
return undefined;
}
if (__DEV__) {
// The check the walk would have run. Generated code reads the field names
// the schema declared, so a misspelled one is the same silent, total loss
// of a property here as it is there.
validateOwnFields(record, node);
}
// Branched rather than one call on the union: the two forms take different
// arities, and the compact one is handed the *running* class's table — not
// the declaring class's, which is what a subclass running inherited code
// needs. Most generated exporters ignore it.
return compact
? (exporter as NonNullable)(
node,
isCompactDefault,
)
: (exporter as GeneratedJSON['exportJSON'])(node);
}
/**
* Apply a serialized node to one this update has just constructed, which is
* what {@link LexicalNode.importJSON} does after building the node.
*
* The difference from {@link LexicalNode.updateFromJSON} is the `getWritable()`
* that one opens with. It has to: it is public API and may be handed any node,
* from any version, at any point in an update. A node `importJSON` just built
* is none of those things — {@link $setNodeKey} put it in the node map, in the
* dirty set and in `_cloneNotNeeded` a moment earlier, so it already *is* the
* writable latest version and `getWritable()` can only re-derive what the
* constructor established: resolve the latest by key, re-mark a node that is
* already dirty, and walk parents it does not yet have.
*
* That cost is per node and the parse path pays it for every node in the
* document, which is why this exists rather than the caller simply chaining
* `updateFromJSON`. The node a replacement returns is fresh in the same sense —
* {@link $applyNodeReplacement} requires it to carry a key of its own — so it
* qualifies too.
*
* @internal
*/
export function $applyImportJSON(
node: T,
serializedNode: LexicalParseJSON,
): T {
if (__DEV__) {
// The whole point is skipping getWritable(), so assert what it would have
// returned rather than calling it: a key in _cloneNotNeeded is one whose
// node this update created (or already cloned), which is exactly the
// branch of getWritable() that returns the node unchanged.
invariant(
$isEphemeral(node) || getActiveEditor()._cloneNotNeeded.has(node.__key),
'$applyImportJSON: node %s with key %s was not constructed by this update; use updateFromJSON instead',
node.constructor.name,
node.__key,
);
}
// Skipped entirely for the common node, which carries no state and is
// imported from JSON that has none; $updateStateFromJSON is the one other
// place the getWritable would have come from.
const self =
node.__state || serializedNode[NODE_STATE_KEY] !== undefined
? $updateStateFromJSON(node, serializedNode)
: node;
return $applyJSONSetters(self, serializedNode);
}
/**
* Apply a node's compiled serialization schema (see {@link compileSetters}),
* returning the (writable) node: flat NodeState first, then the schema's
* properties — through the class's generated parser when it has one, and
* otherwise by calling each property's setter with its parsed value. Used by
* the base {@link LexicalNode.updateFromJSON} so a node that declares a
* serialization schema needs no `updateFromJSON` boilerplate.
*
* @internal
*/
export function $applyJSONSetters(
node: T,
serializedNode: {readonly [key: string]: unknown},
): T {
const record = getNodeClassRecord(node.constructor as Klass);
if (__DEV__) {
validateOwnFields(record, node);
}
const {flatStates, generated, setters} = getCompiled(record);
const self = $applyFlatStates(node, serializedNode, flatStates);
if (generated !== null && generated.updateFromJSON !== undefined) {
// The generated parser applies the same properties in the same order and
// ignores a setter's return the same way, so what it hands back is what
// the walk would have: the node passed in.
return generated.updateFromJSON(self, serializedNode) as T;
}
return $walkSetters(self, serializedNode, setters);
}
/**
* {@link $applyJSONSetters} with the generated parser left out — the
* schema-driven walk alone — which is what generated code is checked against.
*
* @internal
*/
export function $walkJSONSetters(
node: T,
serializedNode: {readonly [key: string]: unknown},
): T {
const record = getNodeClassRecord(node.constructor as Klass);
if (__DEV__) {
validateOwnFields(record, node);
}
const {flatStates, setters} = getCompiled(record);
return $walkSetters(
$applyFlatStates(node, serializedNode, flatStates),
serializedNode,
setters,
);
}
/**
* Flat state first, matching the order in which $updateStateFromJSON ran
* before a node's own setters — and by the walk whether or not the class has a
* generated parser, which is handed the node with its state already applied.
* What a node carries in state is not known when code is generated; this is
* the mirror of exportJSON appending `__state.toJSON()` around the generated
* literal.
*/
function $applyFlatStates(
node: T,
serializedNode: {readonly [key: string]: unknown},
flatStates: readonly AnyStateConfig[],
): T {
let self = node;
for (let i = 0; i < flatStates.length; i++) {
const stateConfig = flatStates[i];
// A bare read, as a schema property gets — but a *presence* test rather
// than a guard on the read, and the one place in this walk where absent
// and default are different answers. A flat state may arrive at the top
// level or inside the nested `$` blob, which `$updateStateFromJSON`
// applied before this ran: parsing an absent key to its default here
// would overwrite the value the blob just supplied, so a document
// spelling a state the nested way would lose it. Present at the top
// level wins, absent leaves the blob's value alone.
//
// JSON carries no `undefined`, so `!== undefined` is that question for
// anything `JSON.parse` produced. The names it cannot tell apart from an
// absent key are `Object.prototype`'s, which `createSharedNodeState`
// refuses as flat state keys.
const raw = serializedNode[stateConfig.key];
if (raw !== undefined) {
const parsed = stateConfig.parse(raw);
// Wrapped in an updater thunk so a parse that returns a function value
// is stored verbatim instead of being invoked as an updater.
self = $setState(self, stateConfig, () => parsed);
}
}
return self;
}
/** The walk over a class's compiled setters: one property at a time. */
function $walkSetters(
node: T,
serializedNode: {readonly [key: string]: unknown},
setters: readonly CompiledSetter[],
): T {
for (let i = 0; i < setters.length; i++) {
const entry = setters[i];
// A bare read. An absent property is `undefined`, which is exactly what
// the schema maps to the property's default, so an own-key test would
// only change the answer for a key the object *inherits* — and the one
// prototype a serialized node has is `Object.prototype`, whose members
// `nodeSchema` refuses as property names. `Object.prototype.hasOwnProperty
// .call` is not inlined by V8 in this shape and measured ~13 ns per
// property against a whole-node parse of ~17 ns, so it was most of the
// cost of the walk it guarded.
const parsed = entry.schema(serializedNode[entry.key]);
if (entry.kind === 'ownField') {
// `node` is writable already — the walk is only ever reached from
// getWritable() or from a node this update constructed — so this is the
// whole of applying the property.
//
// The table is reached with hasOwnKey, as the codegen's emitted lookup
// is (`v in TABLE ? TABLE[v] : ` over a null-prototype table).
// The schema has already reduced the value to its own domain, but that
// domain can be wider than the table's keys — `withField(stringValue(),
// {setterTable})` admits any string — and a bare lookup would then resolve
// `'toString'` to Object.prototype's method and store *that* in the
// field. A genuine miss still yields `undefined`, exactly as the bare
// lookup did.
ownFieldRecord(node)[entry.field] =
entry.setterTable === undefined
? parsed
: hasOwnKey(entry.setterTable, parsed as string)
? entry.setterTable[parsed as string]
: // The stored form of the schema's *default*, not `undefined`: a
// miss means the parse landed on a domain member the table has
// no stored form for, and writing `undefined` put a value into a
// typed field that the field's type does not admit — and then
// dropped the property on the next export. The generated parser
// falls back to exactly this (`... : 0` for TextNode's `mode`),
// so writing anything else made the two disagree.
setterDefault(entry);
} else {
// The return is not read. A setter's own `getWritable()` hands back the
// node it was called on, since that node is already writable, so
// following the return only ever reassigned the node to itself — and a
// `void` setter had to be given a meaning ("unchanged") it never
// needed. The `ownField` branch above has always assumed exactly this,
// writing the field with no getWritable() of its own.
entry.setter.call(node, parsed);
}
}
return node;
}
/** @internal */
export function getStaticNodeConfig(
klass: Klass,
): OwnStaticNodeConfig {
return getNodeClassRecord(klass).config;
}
/**
* Derive everything this class needs once: read its `$config()`, inject the
* statics it did not declare, then compile its accessor tables.
*/
function buildNodeClassRecord(klass: Klass): NodeClassRecord {
const nodeConfigRecord =
klass.prototype != null && PROTOTYPE_CONFIG_METHOD in klass.prototype
? klass.prototype[PROTOTYPE_CONFIG_METHOD]()
: undefined;
const isAbstract = isAbstractNodeClass(klass);
// Only trust a *user-defined* own static getType() to derive the node type.
// A getType() that we synthesized (branded with SYNTHESIZED_GET_TYPE) must
// not be called here: the synthesized closure defers to
// LexicalNode.getType.call(this) for a foreign `this`, which re-enters
// getStaticNodeConfig and — when the closure is inherited/own-copied onto a
// subclass by the compiled class output — causes infinite recursion
// (RangeError: Maximum call stack size exceeded). For such a class the type
// is derived from the $config record below instead. (#8867 follow-up.)
const ownGetType =
!isAbstract && hasOwnStaticMethod(klass, 'getType')
? klass.getType
: undefined;
const nodeType =
ownGetType && !(SYNTHESIZED_GET_TYPE in ownGetType)
? ownGetType.call(klass)
: undefined;
let ownNodeConfig:
| undefined
| StaticNodeConfigValue;
let ownNodeType = nodeType;
if (nodeConfigRecord) {
if (nodeType) {
ownNodeConfig = nodeConfigRecord[nodeType];
} else {
// No static getType(): derive the type and config from the $config
// record. The common case is a concrete node keyed by its string `type`.
for (const [k, v] of Object.entries(nodeConfigRecord)) {
ownNodeType = k;
ownNodeConfig = v;
}
// Fall back to a well-known symbol key (e.g. Symbol.for('ElementNode'))
// for an abstract base class that has no concrete node type, using the
// first symbol whose value is a config record.
if (!ownNodeConfig) {
for (const symbolKey of Object.getOwnPropertySymbols(
nodeConfigRecord,
)) {
const symbolConfig = nodeConfigRecord[symbolKey];
if (symbolConfig) {
ownNodeConfig = symbolConfig;
break;
}
}
}
}
}
const record: NodeClassRecord = {
compiled: undefined,
composed: undefined,
config: {
declaresOwnConfig: hasOwnKey(
klass.prototype as unknown as object,
PROTOTYPE_CONFIG_METHOD,
),
klass,
ownNodeConfig,
ownNodeType,
},
ownFieldsValidated: false,
};
// Cached before compiling, because compileSetters walks this class chain
// (which includes klass) and re-enters this cache, which must hit rather
// than recurse.
NODE_CLASS_CACHE.set(klass, record);
// Compiled eagerly rather than on first export/import so that a schema
// naming an accessor the class does not have — or a `$config` naming
// generated code that is not its own — fails while the class is being
// registered, where the error names the class that is misconfigured, and
// not later, out of an autosave or a copy handler.
//
// Everything that can throw is inside the try, injection included: that is
// where the DEV clone-arity invariant lives. Dropping the record on the way
// out is what keeps the error attributable — without it a second
// createEditor() finds the cached record, never reaches this block again,
// and registers the broken class in silence, leaving the throw to whichever
// serialization call happens to come first.
//
// Injection goes last within the try because a class cannot be un-mutated:
// nothing compiled above reads a synthesized static (compileSetters and
// compileGetters resolve names off `klass.prototype` and walk the config
// chain, never `klass.getType` or `klass.clone`), so ordering it here leaves
// the class untouched on every failure path rather than half-registered.
try {
const setters = compileSetters(klass);
const getters = compileGetters(klass);
const composed = getComposedSchema(klass);
// Built here, once per class, from the class's own composed schema: the
// generated module reads its lookup tables from these schemas rather
// than carrying copies, so a subclass that inherits the code runs it
// over its own tables.
const generated = resolveGenerated(klass, ownNodeConfig, {
getters,
setters,
});
record.compiled = {
// Non-flat states live under NODE_STATE_KEY and are applied by
// $updateStateFromJSON; these are the ones serialized as top-level
// properties alongside the schema's.
flatStates: composed.flatStates,
generated: generated === null ? null : generated(composed.fields),
getters,
isCompactDefault: compactDefaultTest(getters),
setters,
};
injectSynthesizedStatics(klass, isAbstract, ownNodeType, ownNodeConfig);
// Not gated on `isAbstract`: an abstract base that declares schema fields
// stores them on every subclass instance, so its clone has to carry them
// too.
injectSynthesizedAfterCloneFrom(klass);
injectDeclaredIsInline(klass, ownNodeConfig);
} catch (error) {
NODE_CLASS_CACHE.delete(klass);
throw error;
}
return record;
}
function synthesizedIsInlineTrue(): boolean {
return true;
}
function synthesizedIsInlineFalse(): boolean {
return false;
}
/**
* Whether `isInline` (an `isInline` method found on a prototype) was
* synthesized from a `$config({inline})` declaration.
*
* @internal
*/
export function isSynthesizedIsInline(isInline: unknown): boolean {
return (
isInline === synthesizedIsInlineTrue ||
isInline === synthesizedIsInlineFalse
);
}
/**
* A class that declares `$config({inline})` and does not write its own
* `isInline()` gets one returning the declaration, so classes that declare
* nothing run exactly the code they ran before (ElementNode.isInline is
* unchanged).
*/
function injectDeclaredIsInline(
klass: Klass,
ownNodeConfig:
| undefined
| StaticNodeConfigValue,
): void {
if (
!ownNodeConfig ||
ownNodeConfig.inline === undefined ||
!hasOwnKey(klass.prototype as unknown as object, PROTOTYPE_CONFIG_METHOD) ||
Object.prototype.hasOwnProperty.call(klass.prototype, 'isInline')
) {
return;
}
Object.defineProperty(klass.prototype, 'isInline', {
configurable: true,
value: ownNodeConfig.inline
? synthesizedIsInlineTrue
: synthesizedIsInlineFalse,
writable: true,
});
}
/**
* Give a concrete node class the statics it did not define for itself:
* `getType`, `clone`, `importJSON` and `importDOM`, each derived from what its
* `$config()` declared. A class that defines its own keeps it.
*/
function injectSynthesizedStatics(
klass: Klass,
isAbstract: boolean,
ownNodeType: undefined | string,
ownNodeConfig:
| undefined
| StaticNodeConfigValue,
): void {
if (!isAbstract && ownNodeType) {
if (!hasOwnStaticMethod(klass, 'getType')) {
// Guard against subclass inheritance: a subclass that does not define its
// own static getType() (nor its own $config()-derived type yet) would
// otherwise *inherit* this synthesized closure via the prototype chain and
// return the superclass's hardcoded `ownNodeType`. When that happens the
// subclass registers under the superclass's type, colliding with it
// (e.g. `CodeHighlightNode`/`HashtagNode` resolving to type 'text' and
// clashing with `TextNode`). Only return the captured type when invoked on
// the exact class it was synthesized for; otherwise defer to the base
// LexicalNode.getType(), which resolves the correct type for `this`.
const synthesizedForKlass = klass;
const synthesizedGetType = function (this: Klass): string {
if (this !== synthesizedForKlass) {
return LexicalNode.getType.call(this);
}
return ownNodeType;
};
// Brand the closure so buildNodeClassRecord can recognize it and avoid
// calling it to derive the node type (which would recurse). See the note
// at the `ownGetType` computation above.
(synthesizedGetType as {[SYNTHESIZED_GET_TYPE]?: true})[
SYNTHESIZED_GET_TYPE
] = true;
klass.getType = synthesizedGetType;
}
if (!hasOwnStaticMethod(klass, 'clone')) {
// TextNode.length > 0 will only be true if the compiler output
// is not ES6 compliant, in which case we can not provide this
// warning. We also can't reliably provide this warning if the output
// has been optimized.
if (__DEV__ && IS_UNOPTIMIZED_DEV_BUILD) {
invariant(
klass.length === 0,
'%s (type %s) must implement a static clone method since its constructor has %s required arguments (expecting 0). Use an explicit default in the first argument of your constructor(prop: T=X, nodeKey?: NodeKey).',
klass.name,
ownNodeType,
String(klass.length),
);
}
klass.clone = (
prevNode: LexicalNode,
internalSkipAfterCloneFrom?: typeof INTERNAL_SKIP_AFTER_CLONE_FROM,
) => {
setPendingNodeToClone(prevNode);
const node = new klass();
// The internal clone wrappers ($cloneWithProperties / $copyNode) pass
// the module-private INTERNAL_SKIP_AFTER_CLONE_FROM sentinel because
// they call afterCloneFrom themselves. When this synthesized clone is
// instead called directly — e.g. `NodeClass.clone(node)`, an idiomatic
// pre-$config() pattern — the sentinel is absent, so we call
// afterCloneFrom here to preserve the documented clone() contract and
// avoid silent property loss. afterCloneFrom is not guaranteed
// idempotent, so this must run exactly once (see the sentinel
// definition for the full rationale).
if (internalSkipAfterCloneFrom !== INTERNAL_SKIP_AFTER_CLONE_FROM) {
node.afterCloneFrom(prevNode);
}
return node;
};
}
if (!hasOwnStaticMethod(klass, 'importJSON')) {
if (__DEV__ && IS_UNOPTIMIZED_DEV_BUILD) {
invariant(
klass.length === 0,
'%s (type %s) must implement a static importJSON method since its constructor has %s required arguments (expecting 0). Use an explicit default in the first argument of your constructor(prop: T=X, nodeKey?: NodeKey).',
klass.name,
ownNodeType,
String(klass.length),
);
}
klass.importJSON =
(ownNodeConfig && ownNodeConfig.$importJSON) ||
synthesizeImportJSON(klass);
}
if (!hasOwnStaticMethod(klass, 'importDOM') && ownNodeConfig) {
const {importDOM} = ownNodeConfig;
if (importDOM) {
klass.importDOM = () => importDOM;
}
}
// Inject a delegated getTextContentSize if only getTextContent is overridden
const proto = klass.prototype;
if (
hasOwnKey(proto, 'getTextContent') &&
!hasOwnKey(proto, 'getTextContentSize')
) {
klass.prototype.getTextContentSize =
LexicalNode.prototype.getTextContentSize;
}
}
}
/**
* The node fields a schema names, in either direction.
*
* Both directions, and the *declared* field rather than the one
* {@link resolveGetterAccessor} resolves to: which accessor serialization uses
* is a question about this class's methods, and a subclass that overrides
* `getStyle()` still stores its value in `__style`. A clone carries storage, so
* it wants every field name the schema knows, whichever direction named it and
* whether or not an override sends the serialization through a method instead.
*
* A property with no `field` at all — declared through accessor methods on both
* sides — names no storage here, and is left to the class (see
* {@link injectSynthesizedAfterCloneFrom}).
*/
function schemaFieldNames(schema: AnySerializationSchema): readonly string[] {
const names: string[] = [];
for (const accessor of [schema.getter, schema.setter]) {
if (
isSchemaField(accessor) &&
// Rejected on both accessor paths for the same reason: it names the
// prototype rather than a field, so copying it would re-parent the clone.
accessor.field !== '__proto__' &&
!names.includes(accessor.field)
) {
names.push(accessor.field);
}
}
return names;
}
/**
* Give each class in this one's chain the `afterCloneFrom` its schema implies,
* unless it wrote one for itself.
*
* A schema field is persisted state, so it has to survive
* {@link $cloneWithProperties} — the clone `getWritable()` makes on the first
* write of every update — or the node loses it on the next edit rather than
* failing anywhere. Declaring the field is already saying so, so the copy is
* derived from the same declaration `exportJSON` and `updateFromJSON` come
* from rather than written a third time.
*
* Per declaring class, not per registered class: each class copies the fields
* its own `$config` declared and delegates the rest to its superclass, which is
* what a hand-written `afterCloneFrom` does with its `super` call, and it means
* a base class shared by several subclasses is fixed up once. A field an
* ancestor declares too is left to that ancestor — see
* {@link ownSchemaFields} — so a class that only re-declares inherited
* properties gets no method of its own at all.
*
* A class that defines its own `afterCloneFrom` keeps it and is trusted with
* its own fields, the same rule the synthesized statics follow. That is what
* lets `ElementNode` — whose clone also has to carry `__first`/`__last`/`__size`
* and the slot bookkeeping, none of which any schema describes — keep a
* hand-written method, and it is why {@link $cloneWithProperties} still checks
* that an override called `super`.
*/
function injectSynthesizedAfterCloneFrom(klass: Klass): void {
for (const {klass: currentKlass, ownNodeConfig} of iterStaticNodeConfigChain(
klass,
)) {
const prototype = currentKlass.prototype as unknown as object;
if (hasOwnKey(prototype, 'afterCloneFrom')) {
// Hand-written, or synthesized by an earlier registration.
continue;
}
// Registration is not idempotent for the question a code generator asks —
// "does this class own an `afterCloneFrom`, or would it take a synthesized
// one" — because after the first registration every class in the chain
// owns one either way. Marking what this installs is what keeps the two
// apart; see `declaresOwnAfterCloneFrom`.
// From this class's own composition rather than the registered subclass's,
// so what it copies does not depend on which class was registered first: a
// subclass that re-declares an inherited field owns that field in its own
// composition and in every subclass's, but the ancestor still owns it in
// the composition that has no such subclass in it.
const fields = ownSchemaFields(currentKlass);
if (fields.length === 0) {
// Nothing of its own to carry — including every class with no `$config`
// of its own, which reports its ancestor's fields but declares none.
continue;
}
// Resolved when the clone runs, not now: a base class in this chain may
// still be waiting for its own synthesized method, and registration order
// is whatever order the classes were passed to `createEditor`.
const superPrototype = Object.getPrototypeOf(prototype) as LexicalNode;
// The straight-line function the build generated for this class when it
// has one, and otherwise a walk of the field names — the same pairing as
// the export and import directions, for the same reason: the fields are
// fixed once the schema is written, and a keyed store whose key changes
// every iteration is what generating the code removes.
// The class's own generated code, bound to its own schema by its record
// — the record this registration is building when `currentKlass` is the
// class being registered, and otherwise the ancestor's, built here if it
// has not been.
const generated =
ownNodeConfig && ownNodeConfig.generated !== undefined
? getCompiled(getNodeClassRecord(currentKlass)).generated
: null;
const copyFields =
(generated !== null && generated.afterCloneFrom) ||
((node: LexicalNode, prevNode: LexicalNode): void => {
const self = node as unknown as Record;
const prev = prevNode as unknown as Record;
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
self[field] = prev[field];
}
});
(
prototype as {afterCloneFrom: LexicalNode['afterCloneFrom']}
).afterCloneFrom = function (
this: LexicalNode,
prevNode: LexicalNode,
): void {
superPrototype.afterCloneFrom.call(this, prevNode);
copyFields(this, prevNode);
};
(
prototype as {afterCloneFrom: {[SYNTHESIZED_AFTER_CLONE_FROM]?: true}}
).afterCloneFrom[SYNTHESIZED_AFTER_CLONE_FROM] = true;
}
}
/**
* The key marking an `afterCloneFrom` this module synthesized, as opposed to
* one the class wrote.
*
* A string rather than a symbol: a `Symbol()` call at module scope is a side
* effect no bundler will drop, and this module is one every editor imports.
*/
const SYNTHESIZED_AFTER_CLONE_FROM = '__lexicalSynthesizedAfterCloneFrom';
/**
* Whether `klass` wrote its own `afterCloneFrom`, rather than taking the one
* its schema implies.
*
* For a code generator deciding whether to *attach* the copy half: a class that
* wrote its own keeps it, so a generated function handed to it through
* `$config` would be shipped and never called. The function is still worth
* generating — `ElementNode` and `CodeNode` import and call it, so what they
* write by hand is only the fields no schema describes — it is the attachment
* that is pointless. Asked of the class itself rather than of the prototype,
* because after one registration every class in a chain has an own
* `afterCloneFrom` and only the marker says which kind.
*
* @internal
*/
export function declaresOwnAfterCloneFrom(klass: Klass): boolean {
const prototype = klass.prototype as unknown as {
afterCloneFrom?: {[SYNTHESIZED_AFTER_CLONE_FROM]?: true};
};
if (!hasOwnKey(prototype, 'afterCloneFrom')) {
return false;
}
const own = prototype.afterCloneFrom;
return own === undefined || own[SYNTHESIZED_AFTER_CLONE_FROM] !== true;
}
/**
* The node fields a class's own `$config` declares, deduplicated.
*
* Raw, in the sense that it says nothing about which class ends up carrying
* each one: {@link ownSchemaFields} is what answers that.
*/
function declaredSchemaFields(klass: Klass): readonly string[] {
const {declaredBy, fieldsBaseFirst} = getComposedSchema(klass);
const fields: string[] = [];
for (const [key, schema] of fieldsBaseFirst) {
if (declaredBy.get(key) !== klass) {
continue;
}
for (const field of schemaFieldNames(schema)) {
if (!fields.includes(field)) {
// Two properties may name one field (a field exported under a second
// key), and it is copied once.
fields.push(field);
}
}
}
return fields;
}
/**
* The node fields a class carries across a clone: the ones its own `$config`
* declares, less any an ancestor declares too.
*
* A class re-declares an inherited property to change how it is *serialized* —
* `TabNode` restates `text`, `detail` and `mode` for their accessors and
* narrower domains — which makes it the owner of that key in its own
* composition and in every subclass's. Where it is *stored* does not change,
* though, and the ancestor's `afterCloneFrom` already ran by then and is
* responsible for the fields it declares, so assigning them a second time
* writes the same values again for nothing.
*
* The ancestor's declarations are what is subtracted, not the fields it
* actually assigns, and the two are the same set transitively: an ancestor
* that skipped a field skipped it because *its* own ancestor declares it, and
* that class is in this chain too.
*
* Shared with the codegen in `scripts/shared/generateNodeJSON.mjs`, which emits
* the straight-line form of exactly this list, so the two cannot disagree about
* which class carries which field.
*
* @internal
*/
export function ownSchemaFields(klass: Klass): readonly string[] {
const declared = declaredSchemaFields(klass);
if (declared.length === 0) {
return declared;
}
const inherited = new Set();
for (const {klass: currentKlass} of iterStaticNodeConfigChain(klass)) {
if (currentKlass !== klass) {
for (const field of declaredSchemaFields(currentKlass)) {
inherited.add(field);
}
}
}
return inherited.size === 0
? declared
: declared.filter(field => !inherited.has(field));
}
/**
* The `importJSON` a class gets when it declares none: build the node, then
* apply the serialized properties to it. A generated parser, when the class
* has one, is reached through {@link $applyJSONSetters} the way every other
* path reaches it.
*
* {@link $applyImportJSON} is the base `updateFromJSON` minus a `getWritable()`
* the fresh node does not need, so it may only stand in for that method when
* the node has not overridden it. A class is free to declare a schema *and* an
* `updateFromJSON` — to migrate an older payload, or to apply something the
* schema cannot describe — and calling the schema directly would drop that work
* on the import path while leaving it in place everywhere else.
*
* The node `$create` returns is what is asked, not `klass`: a replacement
* registered for this type is a different class, with its own override or lack
* of one.
*/
function synthesizeImportJSON(
klass: Klass,
): (
serializedNode: SerializedPartial &
Record,
) => LexicalNode {
return serializedNode => {
const node = $create(klass);
return node.updateFromJSON === LexicalNode.prototype.updateFromJSON
? $applyImportJSON(node, serializedNode)
: node.updateFromJSON(serializedNode);
};
}
/**
* Collect all configuration for this class and its superclasses
*
* @internal
*/
export function* iterStaticNodeConfigChain(
klass: Klass,
): Iterable {
for (
let current: null | Klass = klass;
current && (current === LexicalNode || $isLexicalNode(current.prototype));
) {
const config = getStaticNodeConfig(current);
// A class that declared no `$config()` of its own contributes nothing to
// the chain: what `getStaticNodeConfig` resolved for it is its ancestor's
// config, and the ancestor is the next link, which yields that config
// itself. Yielding it here too would attribute the ancestor's `json`,
// `$transform`, `stateConfigs` and `slots` to the subclass and present
// them twice — and a `$transform` written inline is a fresh closure per
// `$config()` call, so the `Set` that collects them cannot tell the two
// copies apart and the transform runs twice per node.
//
// `extends` goes with it, which is also what keeps the walk from jumping
// to the ancestor's parent and skipping the ancestor.
const declared = config.declaresOwnConfig;
yield declared ? config : {...config, ownNodeConfig: undefined};
current =
(declared && config.ownNodeConfig && config.ownNodeConfig.extends) ||
getSuperclassOf(current);
}
}
/**
* Build a map from each registered node type to the set of registered node
* types that are it or extend it (including the type itself). For every node
* class in `nodes`, its prototype chain is walked and the class's own type is
* added to the bucket of each registered ancestor type it inherits from.
*
* The result lets callers expand a base node type to all of its registered
* subclass types up front, so a subclass instance can be matched by type
* without a runtime `instanceof`.
*
* @experimental
*/
export function getRegisteredSubtypeMap(
nodes: Iterable>,
): Map> {
const subtypes = new Map>();
const klassByType = new Map>();
for (const klass of nodes) {
const {ownNodeType} = getStaticNodeConfig(klass);
if (ownNodeType) {
klassByType.set(ownNodeType, klass);
subtypes.set(ownNodeType, new Set());
}
}
for (const [type, klass] of klassByType) {
for (const {ownNodeType} of iterStaticNodeConfigChain(klass)) {
const bucket = ownNodeType && subtypes.get(ownNodeType);
if (bucket) {
bucket.add(type);
}
}
}
return subtypes;
}
/**
* Create an node from its class.
*
* This directly constructs the final `withKlass` node type, skipping the
* intermediate steps where each replaced node would be created and then
* immediately discarded — once per configured replacement of that node.
*
* A deprecated `replace` given without a `withKlass` is the one case that
* cannot be resolved ahead of construction, since only its `with` function
* knows what to build. Such a replacement is still applied, the old way, to
* the node this constructs.
*
* This does not support any arguments to the constructor.
* Setters can be used to initialize your node, and they can
* be chained. You can of course write your own mutliple-argument functions
* to wrap that.
*
* @example
* ```ts
* function $createTokenText(text: string): TextNode {
* return $create(TextNode).setTextContent(text).setMode('token');
* }
* ```
*/
export function $create(klass: Klass): T {
const editor = $getEditor();
errorOnReadOnly();
const registeredNode = editor.resolveRegisteredNodeAfterReplacements(
editor.getRegisteredNode(klass),
);
const node = new registeredNode.klass() as T;
// The resolve above follows `withKlass` as far as it goes, so a `replace`
// still set on the node it stopped at has no `withKlass` to follow: it is a
// deprecated one, and its `with` can only be given a constructed node.
return registeredNode.replace === null
? node
: ($applyNodeReplacement(node) as T);
}
/**
* Starts with a node and moves up the tree (toward the root node) to find a matching node based on
* the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be
* passed as an argument. eg. if( (node) => node.__type === 'div') ) return true; otherwise return false
* @param startingNode - The node where the search starts.
* @param findFn - A testing function that returns true if the current node satisfies the testing parameters.
* @returns `startingNode` or one of its ancestors that matches the `findFn` predicate and is not the `RootNode`, or `null` if no match was found.
*/
export const $findMatchingParent: {
(
startingNode: LexicalNode,
findFn: (node: LexicalNode) => node is T,
): T | null;
(
startingNode: LexicalNode,
findFn: (node: LexicalNode) => boolean,
): LexicalNode | null;
} = (
startingNode: LexicalNode,
findFn: (node: LexicalNode) => boolean,
): LexicalNode | null => {
let curr: ElementNode | LexicalNode | null = startingNode;
while (curr != null && !$isRootNode(curr)) {
if (findFn(curr)) {
return curr;
}
curr = curr.getParent();
}
return null;
};
/** Builds an ordered array of child node keys for the given ElementNode by walking its linked-list pointers. */
export function $createChildrenArray(
element: ElementNode,
nodeMap: null | NodeMap,
): NodeKey[] {
const children = [];
let nodeKey = element.__first;
while (nodeKey !== null) {
const node =
nodeMap === null ? $getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
if (node === null || node === undefined) {
invariant(false, '$createChildrenArray: node does not exist in nodeMap');
}
children.push(nodeKey);
nodeKey = node.__next;
}
return children;
}
/**
* Look up the superclass of this class, prefer
* {@link iterStaticNodeConfigChain} when implementing loops.
*
* @internal
*/
export function getSuperclassOf(
klass: Klass,
): null | Klass {
const viaStatic = Object.getPrototypeOf(klass);
if (typeof viaStatic === 'function' && viaStatic !== Function.prototype) {
return viaStatic; // healthy static chain
}
// static link severed by the loose transform — use the instance chain
const parentProto = klass.prototype && Object.getPrototypeOf(klass.prototype);
return parentProto ? parentProto.constructor : null;
}