/
listwalker.ts
296 lines (255 loc) · 8.25 KB
/
listwalker.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @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 list/list/utils/listwalker
*/
import { first, toArray, type ArrayOrItem } from 'ckeditor5/src/utils.js';
import { isListItemBlock, type ListElement } from './model.js';
import type { Element, Node } from 'ckeditor5/src/engine.js';
/**
* Document list blocks iterator.
*/
export default class ListWalker {
/**
* The start list item block element.
*/
private _startElement: Node;
/**
* The reference indent. Initialized by the indent of the start block.
*/
private _referenceIndent: number;
/**
* The iterating direction.
*/
private _isForward: boolean;
/**
* Whether start block should be included in the result (if it's matching other criteria).
*/
private _includeSelf: boolean;
/**
* Additional attributes that must be the same for each block.
*/
private _sameAttributes: Array<string>;
/**
* Whether blocks with the same indent level as the start block should be included in the result.
*/
private _sameIndent: boolean;
/**
* Whether blocks with a lower indent level than the start block should be included in the result.
*/
private _lowerIndent: boolean;
/**
* Whether blocks with a higher indent level than the start block should be included in the result.
*/
private _higherIndent: boolean;
/**
* Creates a document list iterator.
*
* @param startElement The start list item block element.
* @param options.direction The iterating direction.
* @param options.includeSelf Whether start block should be included in the result (if it's matching other criteria).
* @param options.sameAttributes Additional attributes that must be the same for each block.
* @param options.sameIndent Whether blocks with the same indent level as the start block should be included
* in the result.
* @param options.lowerIndent Whether blocks with a lower indent level than the start block should be included
* in the result.
* @param options.higherIndent Whether blocks with a higher indent level than the start block should be included
* in the result.
*/
constructor(
startElement: Node,
options: ListWalkerOptions
) {
this._startElement = startElement;
this._referenceIndent = startElement.getAttribute( 'listIndent' ) as number;
this._isForward = options.direction == 'forward';
this._includeSelf = !!options.includeSelf;
this._sameAttributes = toArray( options.sameAttributes || [] );
this._sameIndent = !!options.sameIndent;
this._lowerIndent = !!options.lowerIndent;
this._higherIndent = !!options.higherIndent;
}
/**
* Performs only first step of iteration and returns the result.
*
* @param startElement The start list item block element.
* @param options.direction The iterating direction.
* @param options.includeSelf Whether start block should be included in the result (if it's matching other criteria).
* @param options.sameAttributes Additional attributes that must be the same for each block.
* @param options.sameIndent Whether blocks with the same indent level as the start block should be included
* in the result.
* @param options.lowerIndent Whether blocks with a lower indent level than the start block should be included
* in the result.
* @param options.higherIndent Whether blocks with a higher indent level than the start block should be included
* in the result.
*/
public static first(
startElement: Node,
options: ListWalkerOptions
): ListElement | null {
const walker = new this( startElement, options );
const iterator = walker[ Symbol.iterator ]();
return first( iterator );
}
/**
* Iterable interface.
*/
public* [ Symbol.iterator ](): Iterator<ListElement> {
const nestedItems: Array<ListElement> = [];
for ( const { node } of iterateSiblingListBlocks( this._getStartNode(), this._isForward ? 'forward' : 'backward' ) ) {
const indent = node.getAttribute( 'listIndent' );
// Leaving a nested list.
if ( indent < this._referenceIndent ) {
// Abort searching blocks.
if ( !this._lowerIndent ) {
break;
}
// While searching for lower indents, update the reference indent to find another parent in the next step.
this._referenceIndent = indent;
}
// Entering a nested list.
else if ( indent > this._referenceIndent ) {
// Ignore nested blocks.
if ( !this._higherIndent ) {
continue;
}
// Collect nested blocks to verify if they are really nested, or it's a different item.
if ( !this._isForward ) {
nestedItems.push( node );
continue;
}
}
// Same indent level block.
else {
// Ignore same indent block.
if ( !this._sameIndent ) {
// While looking for nested blocks, stop iterating while encountering first same indent block.
if ( this._higherIndent ) {
// No more nested blocks so yield nested items.
if ( nestedItems.length ) {
yield* nestedItems;
nestedItems.length = 0;
}
break;
}
continue;
}
// Abort if item has any additionally specified attribute different.
if ( this._sameAttributes.some( attr => node.getAttribute( attr ) !== this._startElement.getAttribute( attr ) ) ) {
break;
}
}
// There is another block for the same list item so the nested items were in the same list item.
if ( nestedItems.length ) {
yield* nestedItems;
nestedItems.length = 0;
}
yield node;
}
}
/**
* Returns the model element to start iterating.
*/
private _getStartNode() {
if ( this._includeSelf ) {
return this._startElement;
}
return this._isForward ?
this._startElement.nextSibling :
this._startElement.previousSibling;
}
}
/**
* Iterates sibling list blocks starting from the given node.
*
* @internal
* @param node The model node.
* @param direction Iteration direction.
* @returns The object with `node` and `previous` {@link module:engine/model/element~Element blocks}.
*/
export function* iterateSiblingListBlocks(
node: Node | null,
direction: 'forward' | 'backward' = 'forward'
): IterableIterator<ListIteratorValue> {
const isForward = direction == 'forward';
const previousNodesByIndent: Array<ListElement> = []; // Last seen nodes of lower indented lists.
let previous = null;
while ( isListItemBlock( node ) ) {
let previousNodeInList = null; // It's like `previous` but has the same indent as current node.
if ( previous ) {
const nodeIndent = node.getAttribute( 'listIndent' );
const previousNodeIndent = previous.getAttribute( 'listIndent' );
// Let's find previous node for the same indent.
// We're going to need that when we get back to previous indent.
if ( nodeIndent > previousNodeIndent ) {
previousNodesByIndent[ previousNodeIndent ] = previous;
}
// Restore the one for given indent.
else if ( nodeIndent < previousNodeIndent ) {
previousNodeInList = previousNodesByIndent[ nodeIndent ];
previousNodesByIndent.length = nodeIndent;
}
// Same indent.
else {
previousNodeInList = previous;
}
}
yield { node, previous, previousNodeInList };
previous = node;
node = isForward ? node.nextSibling : node.previousSibling;
}
}
/**
* The iterable protocol over the list elements.
*
* @internal
*/
export class ListBlocksIterable {
private _listHead: Element;
/**
* @param listHead The head element of a list.
*/
constructor( listHead: Element ) {
this._listHead = listHead;
}
/**
* List blocks iterator.
*
* Iterates over all blocks of a list.
*/
public [ Symbol.iterator ](): Iterator<ListIteratorValue> {
return iterateSiblingListBlocks( this._listHead, 'forward' );
}
}
/**
* Object returned by `iterateSiblingListBlocks()` when traversing a list.
*
* @internal
*/
export interface ListIteratorValue {
/**
* The current list node.
*/
node: ListElement;
/**
* The previous list node.
*/
previous: ListElement | null;
/**
* The previous list node at the same indent as current node.
*/
previousNodeInList: ListElement | null;
}
/**
* Document list blocks iterator options.
*/
export type ListWalkerOptions = {
direction?: 'forward' | 'backward';
includeSelf?: boolean;
sameAttributes?: ArrayOrItem<string>;
sameIndent?: boolean;
lowerIndent?: boolean;
higherIndent?: boolean;
};