/** * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options */ /** * @module ai/toolkit/elementmatcher */ import { type ModelDocumentFragment, type ModelElement, type ModelSchema } from "@ckeditor/ckeditor5-engine"; /** * Similarity threshold from which two paired blocks (paragraphs, headings, list items) are treated as the same element. * * The value is low on purpose to also match "containment cases". A paragraph kept whole and grown `k` times scores `2 / ( 1 + k )`, * so `0.3` keeps the identity of a paragraph grown up to about five times. At the same time, `0.3` means pairing two texts that share about * a third of their characters in words. * * Widgets (limit elements) have no such bar. A table kept in the same place is considered the same table, changed inside. */ export declare const PAIR_THRESHOLD = .3; /** * Fixed value by which every similarity rate is decreased. * * It skews the alignment towards identical and almost identical pairs. An identical pair (`similarity = 1.0`) counts as `0.7`, while a * pair at `similarity = 0.5` counts as `0.2`. When elements are matched, one strong pair wins over many weak ones. * * It must not exceed {@link ~PAIR_THRESHOLD}. */ export declare const PAIR_COST: number; /** * A very minimal score, attributed to any pair of elements that can be compared. * * The reason for this is to prefer making any pair than not having a pair at all. Two comparable elements standing at same positions are * scored with `MIN_PAIR_WEIGHT` rather than `0`, and will be paired if there are no better candidates. */ export declare const MIN_PAIR_WEIGHT = .001; /** * Penalty per position off the diagonal in the alignment objective. * * This is to decide tie-breaks between pairs that score the same. When multiple pairs with same score are considered, we prefer elements * that are closer to each other. */ export declare const POSITION_PENALTY = 1e-9; /** * Band size describes how many elements we compare, so that we don't compare all elements with each other. * * For an element from "before" document, which is at position `i`, we compare it with all "after elements" at positions from * `i - band_size` to `i + band_size`. * * The band size can be dynamically enlarged during the matching algorithm. * * `BAND_START` only describes the *base* starting value. The actual band size is additionally increased by the difference between * the length of before elements and after elements lists. This is to make sure that the band "catches" all insertions/deletions. */ export declare const BAND_START = 8; /** * Maximum base band width. * * This is not the maximum band width, as band width is always increased by the difference between the length of before elements and * after elements lists. So, the band starts at `BAND_START + difference` and can be at most `BAND_MAX + difference`. */ export declare const BAND_MAX = 512; /** * Weight of an element that holds no words (e.g. an inline image, a merge field). Such element is "as important" as a word with * `ELEMENT_TOKEN_WEIGHT` characters. (Small words are less important for the similarity measure than big words.) */ export declare const ELEMENT_TOKEN_WEIGHT = 8; /** * Model attributes ignored by the algorithm. These are attributes with random or meaningless values, generated by the editor when the * document data is read. */ export declare const IGNORED_ATTRIBUTES: ReadonlySet; /** * Model elements ignored by the algorithm. */ export declare const IGNORED_ELEMENTS: ReadonlySet; /** * Attributes that decide element identity. * * Two elements whose identity attributes agree are always treated as the same element. Two elements that differ on identity attribute * are always treated as different elements. * * For example, an image is identified by what it shows (`src`), a media embed by what it points to (`url`), a merge field by its `id`, etc. * These attributes unambiguously decide what the element "is", so the identity attribute is treated as the content. */ export declare const DEFAULT_IDENTITY_ATTRIBUTES: Record>; /** * Splits text into words: anything that is not a letter or a digit separates words. */ export declare const WORD_SEPARATOR: RegExp; /** * Options for {@link ~ElementMatcher}. */ export interface ElementMatcherOptions { /** * Additional attributes that decide identity, by element name. An entry may be a dotted path into an object value. */ identityAttributes?: Record>; } /** * Builds a comparison of two document fragments. Create an instance per comparison and call {@link #match}. * * Two model document fragments -- "before" and "after" states -- are compared by their content similarity. As a result, elements that * are decided to be the same elements on both sides (however, possibly modified) receive the same ID, while all elements that are marked * as removed or added gain unique IDs, that do not pair with anything on the other side. * * This makes model fragments that originally did not have IDs ready to be diffed by {@link module:ai/toolkit/documentdiffer~DocumentDiffer} * which requires the IDs to be set on both sides. */ export declare class ElementMatcher { constructor(schema: ModelSchema, options?: ElementMatcherOptions); /** * Pairs the elements of the two fragments and returns the ID every element should carry. */ match(before: ModelDocumentFragment, after: ModelDocumentFragment): Map; }