1 | /**
|
2 | * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 | /**
|
6 | * Checks if view element is a list type (ul or ol).
|
7 | *
|
8 | * @internal
|
9 | */
|
10 | export function isListView(viewElement) {
|
11 | return viewElement.is('element', 'ol') || viewElement.is('element', 'ul');
|
12 | }
|
13 | /**
|
14 | * Checks if view element is a list item (li).
|
15 | *
|
16 | * @internal
|
17 | */
|
18 | export function isListItemView(viewElement) {
|
19 | return viewElement.is('element', 'li');
|
20 | }
|
21 | /**
|
22 | * Calculates the indent value for a list item. Handles HTML compliant and non-compliant lists.
|
23 | *
|
24 | * Also, fixes non HTML compliant lists indents:
|
25 | *
|
26 | * ```
|
27 | * before: fixed list:
|
28 | * OL OL
|
29 | * |-> LI (parent LIs: 0) |-> LI (indent: 0)
|
30 | * |-> OL |-> OL
|
31 | * |-> OL |
|
32 | * | |-> OL |
|
33 | * | |-> OL |
|
34 | * | |-> LI (parent LIs: 1) |-> LI (indent: 1)
|
35 | * |-> LI (parent LIs: 1) |-> LI (indent: 1)
|
36 | *
|
37 | * before: fixed list:
|
38 | * OL OL
|
39 | * |-> OL |
|
40 | * |-> OL |
|
41 | * |-> OL |
|
42 | * |-> LI (parent LIs: 0) |-> LI (indent: 0)
|
43 | *
|
44 | * before: fixed list:
|
45 | * OL OL
|
46 | * |-> LI (parent LIs: 0) |-> LI (indent: 0)
|
47 | * |-> OL |-> OL
|
48 | * |-> LI (parent LIs: 0) |-> LI (indent: 1)
|
49 | * ```
|
50 | *
|
51 | * @internal
|
52 | */
|
53 | export function getIndent(listItem) {
|
54 | let indent = 0;
|
55 | let parent = listItem.parent;
|
56 | while (parent) {
|
57 | // Each LI in the tree will result in an increased indent for HTML compliant lists.
|
58 | if (isListItemView(parent)) {
|
59 | indent++;
|
60 | }
|
61 | else {
|
62 | // If however the list is nested in other list we should check previous sibling of any of the list elements...
|
63 | const previousSibling = parent.previousSibling;
|
64 | // ...because the we might need increase its indent:
|
65 | // before: fixed list:
|
66 | // OL OL
|
67 | // |-> LI (parent LIs: 0) |-> LI (indent: 0)
|
68 | // |-> OL |-> OL
|
69 | // |-> LI (parent LIs: 0) |-> LI (indent: 1)
|
70 | if (previousSibling && isListItemView(previousSibling)) {
|
71 | indent++;
|
72 | }
|
73 | }
|
74 | parent = parent.parent;
|
75 | }
|
76 | return indent;
|
77 | }
|
78 | /**
|
79 | * Creates a list attribute element (ol or ul).
|
80 | *
|
81 | * @internal
|
82 | */
|
83 | export function createListElement(writer, indent, type, id = getViewElementIdForListType(type, indent)) {
|
84 | // Negative priorities so that restricted editing attribute won't wrap lists.
|
85 | return writer.createAttributeElement(getViewElementNameForListType(type), null, {
|
86 | priority: 2 * indent / 100 - 100,
|
87 | id
|
88 | });
|
89 | }
|
90 | /**
|
91 | * Creates a list item attribute element (li).
|
92 | *
|
93 | * @internal
|
94 | */
|
95 | export function createListItemElement(writer, indent, id) {
|
96 | // Negative priorities so that restricted editing attribute won't wrap list items.
|
97 | return writer.createAttributeElement('li', null, {
|
98 | priority: (2 * indent + 1) / 100 - 100,
|
99 | id
|
100 | });
|
101 | }
|
102 | /**
|
103 | * Returns a view element name for the given list type.
|
104 | *
|
105 | * @internal
|
106 | */
|
107 | export function getViewElementNameForListType(type) {
|
108 | return type == 'numbered' ? 'ol' : 'ul';
|
109 | }
|
110 | /**
|
111 | * Returns a view element ID for the given list type and indent.
|
112 | *
|
113 | * @internal
|
114 | */
|
115 | export function getViewElementIdForListType(type, indent) {
|
116 | return `list-${type}-${indent}`;
|
117 | }
|