/
utils.ts
79 lines (66 loc) · 2.33 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tablecaption/utils
*/
import type {
DocumentFragment,
DocumentSelection,
Element,
ViewElement
} from 'ckeditor5/src/engine.js';
import { getSelectionAffectedTable } from '../utils/common.js';
/**
* Checks if the provided model element is a `table`.
*
* @param modelElement Element to check if it is a table.
*/
export function isTable( modelElement: Element | DocumentFragment | null ): boolean {
return !!modelElement && modelElement.is( 'element', 'table' );
}
/**
* Returns the caption model element from a given table element. Returns `null` if no caption is found.
*
* @param tableModelElement Table element in which we will try to find a caption element.
*/
export function getCaptionFromTableModelElement( tableModelElement: Element ): Element | null {
for ( const node of tableModelElement.getChildren() ) {
if ( node.is( 'element', 'caption' ) ) {
return node;
}
}
return null;
}
/**
* Returns the caption model element for a model selection. Returns `null` if the selection has no caption element ancestor.
*
* @param selection The selection checked for caption presence.
*/
export function getCaptionFromModelSelection( selection: DocumentSelection ): Element | null {
const tableElement = getSelectionAffectedTable( selection );
if ( !tableElement ) {
return null;
}
return getCaptionFromTableModelElement( tableElement );
}
/**
* {@link module:engine/view/matcher~Matcher} pattern. Checks if a given element is a caption.
*
* There are two possible forms of the valid caption:
* - A `<figcaption>` element inside a `<figure class="table">` element.
* - A `<caption>` inside a <table>.
*
* @returns Returns the object accepted by {@link module:engine/view/matcher~Matcher} or `null` if the element cannot be matched.
*/
export function matchTableCaptionViewElement( element: ViewElement ): { name: true } | null {
const parent = element.parent;
if ( element.name == 'figcaption' && parent && parent.is( 'element', 'figure' ) && parent.hasClass( 'table' ) ) {
return { name: true };
}
if ( element.name == 'caption' && parent && parent.is( 'element', 'table' ) ) {
return { name: true };
}
return null;
}