import type { Descendant, Element, PropertyValueDescriptor, PropertyValueOf, SchemaElement, SchemaElementProperty, SchemaProperty, SchemaTextProperty, Text, } from '../../facade'; import type { ElementContentRootSlot } from '../types'; import type { HtmlContentToken, HtmlElementPatch, HtmlMatcher, HtmlNodeSpec, HtmlPrepareDiagnosticInput, HtmlWrapperSpec, } from './HtmlNodeMapping'; import type { AnyBasePluginDefinition } from './PluginDefinition'; import type { HtmlMappingDiagnosticInput, PluginFormatContext, PluginFormatModelView, } from './PluginFormatContext'; import type { InferExactPluginSchemaContribution, InferPluginDocumentType, } from './pluginSchemaModel.internal'; type HtmlPluginFormatContext = PluginFormatContext & Readonly<{ report: (diagnostic: HtmlMappingDiagnosticInput) => void; }>; type HtmlDecodeContext = HtmlPluginFormatContext & Readonly<{ element: Readonly; /** * Claim that the decoded result represents these attributes of * `element` (or of a descendant element passed as the first argument), * such as `'data-editor-html-list-type'`. Claims count only when the * decoded result is kept. Parsing reports each `data-editor-html-*` * attribute that no decoder claims on its owning element. */ preserve: { (...attributes: readonly string[]): void; (element: globalThis.Element, ...attributes: readonly string[]): void; }; }>; type HtmlElementDecodeResult = Readonly< Partial & { children?: readonly Descendant[]; /** * The HTML element whose child nodes hold each content root's blocks, by * slot. A slot left out decodes as an empty root. A container is not * decoded as the element's own children. */ contentRoots?: Readonly>; } >; type HtmlModelContext< C extends AnyBasePluginDefinition, TNode, > = HtmlPluginFormatContext & Omit & Readonly<{ node: Readonly }>; type HtmlPreserve = Readonly<{ /** * Claim that the returned output represents these properties of `node`. * Claims count only when the encoder returns output; unclaimed content * properties are reported as omitted. */ preserve: ( ...keys: ReadonlyArray, string>> ) => void; }>; type HtmlElementEncodeContext< C extends AnyBasePluginDefinition, TNode extends Element, > = HtmlModelContext & HtmlPreserve & Readonly<{ content: HtmlContentToken; /** * A token the output places where the blocks of the content root that * `slot` names on `node` render. Serializing reports each document root * no mapping read. */ contentRoot: (slot: ElementContentRootSlot) => HtmlContentToken; }>; /** * A returned wrapper, or a patch that writes an attribute or style, represents * `value`; return `null` to leave it unrepresented. */ type HtmlPropertyEncodeContext< C extends AnyBasePluginDefinition, TNode, TValue, > = HtmlModelContext & Readonly<{ value: TValue }>; type HtmlPropertiesEncodeContext< C extends AnyBasePluginDefinition, TNode, TValues extends object, > = HtmlModelContext & HtmlPreserve & Readonly<{ values: Readonly }>; type HtmlRuleDirections< TDecodeContext, TDecodeResult, TEncodeContext, TEncodeResult, > = | Readonly<{ decode: (context: TDecodeContext) => TDecodeResult | undefined; decodeOnly?: never; encode: (context: TEncodeContext) => TEncodeResult | null; }> | Readonly<{ decode: (context: TDecodeContext) => TDecodeResult | undefined; decodeOnly: true; encode?: never; }>; type HtmlRuleBase = Readonly<{ match: readonly [HtmlMatcher, ...HtmlMatcher[]]; priority?: number; }>; type HtmlContribution = InferExactPluginSchemaContribution; type HtmlContributionElements = HtmlContribution extends Readonly<{ elements: infer TElements extends Readonly>; }> ? TElements : Readonly>; export type HtmlContributionProperties = HtmlContribution extends Readonly<{ properties: ReadonlyArray; }> ? TProperty : never; type HtmlPropertyName = TKey extends string ? TKey : never; type HtmlPropertyMap = Readonly<{ [ TMember in TProperty as TMember extends SchemaProperty ? HtmlPropertyName : never ]?: TMember extends SchemaProperty ? PropertyValueOf : never; }>; type HtmlElementSchema = InferPluginDocumentType extends keyof HtmlContributionElements ? HtmlContributionElements[InferPluginDocumentType] : never; // A plugin without an element schema owns no element properties; checking // `never` directly would infer a string index from the constraint. type HtmlElementOwnedProperties = [ HtmlElementSchema, ] extends [never] ? Readonly> : HtmlElementSchema extends Readonly<{ properties?: infer TProperties extends Readonly< Record >; }> ? Readonly<{ [TKey in keyof TProperties]?: PropertyValueOf; }> : Readonly>; type HtmlOwnedPropertyMap = HtmlElementOwnedProperties & HtmlPropertyMap>; type HtmlHasOnlyExactPropertyKeys = [ Exclude< HtmlContributionProperties, SchemaProperty & Readonly<{ key: string }> >, ] extends [never] ? true : false; type IsUnion = T extends TWhole ? [TWhole] extends [T] ? false : true : never; type HtmlSoleExactProperty = HtmlContributionProperties extends infer TProperty ? [TProperty] extends [never] ? never : IsUnion extends true ? never : TProperty extends SchemaProperty & { key: string } ? TProperty : never : never; type HtmlPropertyDecode = [ HtmlSoleExactProperty, ] extends [never] ? HtmlPropertyMap> : HtmlSoleExactProperty extends SchemaProperty ? PropertyValueOf['value']> : never; type HtmlPropertyEncodeContextFor = [ HtmlSoleExactProperty, ] extends [never] ? HtmlPropertiesEncodeContext> : HtmlSoleExactProperty extends SchemaProperty ? HtmlPropertyEncodeContext< C, Element, PropertyValueOf['value']> > : never; type HtmlTextEncodeContextFor = [ HtmlSoleExactProperty, ] extends [never] ? HtmlPropertiesEncodeContext> : HtmlSoleExactProperty extends SchemaProperty ? HtmlPropertyEncodeContext< C, Text, PropertyValueOf['value']> > : never; type HtmlElementRule = HtmlRuleBase & HtmlRuleDirections< HtmlDecodeContext, HtmlElementDecodeResult>, HtmlElementEncodeContext< C, Element & Readonly<{ type: InferPluginDocumentType }> & HtmlOwnedPropertyMap >, HtmlNodeSpec > & { continuation?: never; createsElement?: never; unwrap?: never; }; type HtmlElementPropertyRule = HtmlRuleBase & (HtmlElementPropertyPatchRule | HtmlElementPropertyCreateRule); type HtmlElementPropertyPatchRule = HtmlRuleBase & HtmlRuleDirections< HtmlDecodeContext, HtmlPropertyDecode, HtmlPropertyEncodeContextFor, HtmlElementPatch > & { continuation?: never; createsElement?: never; unwrap?: never; }; export type HtmlElementPropertyCreateRule = HtmlRuleBase & HtmlRuleDirections< HtmlDecodeContext, HtmlElementDecodeResult>, HtmlElementEncodeContext>, HtmlNodeSpec > & { /** * Optional element properties merged onto each non-nested trailing block * decoded after the created item's head block (applied wherever the * block's schema admits each returned property, overriding any existing * value on that block). */ continuation?: ( context: HtmlDecodeContext & Readonly<{ block: Readonly; properties: Readonly>; }> ) => Readonly> | undefined; /** * Matched content becomes the plugin's primary target, the schema's * default block when that is a target, carrying the decoded properties; * a matched element holding a leading block those properties apply to * carries them on that block instead, and any trailing non-nested blocks * receive the properties returned by `continuation`. Encoding writes the * output in place of the primary target's element and around the HTML of * every other target. */ createsElement: true; /** * Optional filter over non-owned, schema-admitted element properties when * the matched element unwraps a non-text leading block that cannot carry * this rule's properties. Returning `undefined` keeps no extra * properties; omitting `unwrap` keeps all schema-admitted properties. */ unwrap?: ( context: HtmlDecodeContext & Readonly<{ block: Readonly; properties: Readonly>; }> ) => Readonly> | undefined; }; type HtmlTextPropertyRule = HtmlRuleBase & HtmlRuleDirections< HtmlDecodeContext, HtmlPropertyDecode, HtmlTextEncodeContextFor, HtmlWrapperSpec > & { continuation?: never; createsElement?: never; unwrap?: never; }; type HtmlForeignElementPropertyRule = HtmlRuleBase & HtmlRuleDirections< HtmlDecodeContext, HtmlPropertyDecode, HtmlPropertyEncodeContextFor, HtmlElementPatch > & { continuation?: never; createsElement?: never; unwrap?: never; }; export type HtmlSelfRule = HtmlHasOnlyExactPropertyKeys extends false ? never : [HtmlElementSchema] extends [never] ? [HtmlContributionProperties] extends [never] ? never : [Exclude, SchemaTextProperty>] extends [ never, ] ? HtmlTextPropertyRule : [ Exclude, SchemaElementProperty>, ] extends [never] ? HtmlElementPropertyRule : never : [ Exclude, SchemaElementProperty>, ] extends [never] ? C extends Readonly<{ schema: Readonly<{ element: unknown }>; }> ? HtmlElementRule : HtmlElementPropertyRule : never; export type HtmlSelfNonCreatingRule = HtmlHasOnlyExactPropertyKeys extends false ? never : [HtmlElementSchema] extends [never] ? [HtmlContributionProperties] extends [never] ? never : [Exclude, SchemaTextProperty>] extends [ never, ] ? HtmlTextPropertyRule : [ Exclude, SchemaElementProperty>, ] extends [never] ? HtmlElementPropertyPatchRule : never : [ Exclude, SchemaElementProperty>, ] extends [never] ? HtmlElementRule : never; export type HtmlForeignRule = HtmlHasOnlyExactPropertyKeys extends false ? never : [HtmlElementSchema] extends [never] ? [HtmlContributionProperties] extends [never] ? never : [Exclude, SchemaTextProperty>] extends [ never, ] ? HtmlTextPropertyRule : [ Exclude, SchemaElementProperty>, ] extends [never] ? HtmlForeignElementPropertyRule : never : [ Exclude, SchemaElementProperty>, ] extends [never] ? HtmlElementRule : never; export type HtmlPrepareDocument = Readonly<{ prepareDocument?: ( context: PluginFormatContext & Readonly<{ document: Document; report: (diagnostic: HtmlPrepareDiagnosticInput) => void; }> ) => void; }>; export type PluginHtmlMappingInput< C extends AnyBasePluginDefinition, TRule, > = TRule & HtmlPrepareDocument;