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 | 1×
1×
1×
1×
1×
29×
29×
29×
29×
29×
29×
29×
29×
29×
29×
29×
27×
27×
27×
27×
8×
8×
19×
16×
16×
16×
16×
16×
15×
16×
27×
27×
27×
27×
27×
27×
27×
27×
27×
27×
29×
1×
54×
54×
54×
54×
54×
54×
54×
1×
27×
27×
259×
60×
199×
38×
27×
27×
27×
1×
97×
165×
165×
231×
70×
231×
15×
66×
27×
15×
15×
15×
15×
4×
15×
2×
15×
15×
4×
15×
15×
97×
1×
44×
44×
42×
50×
15×
15×
44×
1×
34×
73×
29×
29×
29×
34×
1×
49×
59×
7×
7×
59×
59×
2×
2×
59×
48×
24×
48×
48×
1×
1×
48×
77×
15×
62×
5×
5×
57×
26×
1×
26×
26×
31×
11×
11×
1×
27×
96×
96×
96×
126×
126×
96×
23×
23×
1×
38×
38×
44×
1×
43×
14×
1×
1×
1×
1×
1×
1×
1×
| define(["require", "exports", "@syncfusion/ej2-base", "./../../selection/index", "./nodecutter", "./dom-node", "./insert-methods", "./isformatted", "../../common/util"], function (require, exports, ej2_base_1, index_1, nodecutter_1, dom_node_1, insert_methods_1, isformatted_1, util_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ClearFormat = (function () {
function ClearFormat() {
}
ClearFormat.clear = function (docElement, endNode, enterAction, selector, command) {
this.domNode = new dom_node_1.DOMNode(endNode, docElement);
this.defaultTag = enterAction === 'P' ? this.defaultTag : 'div';
var nodeSelection = new index_1.NodeSelection(endNode);
var nodeCutter = new nodecutter_1.NodeCutter();
var range = nodeSelection.getRange(docElement);
var nodes = range.collapsed ? nodeSelection.getSelectionNodeCollection(range) :
nodeSelection.getSelectionNodeCollectionBr(range);
var save = nodeSelection.save(range, docElement);
var cursorRange = false;
Iif (range.collapsed && command !== 'ClearFormat') {
cursorRange = true;
range = nodeCutter.GetCursorRange(docElement, range, nodes[0]);
}
var isCollapsed = range.collapsed;
if (!isCollapsed) {
var preNode = void 0;
Iif (nodes.length > 0 && nodes[0].nodeName === 'BR' && ej2_base_1.closest(nodes[0], 'table')) {
preNode = nodeCutter.GetSpliceNode(range, ej2_base_1.closest(nodes[0], 'table'));
}
else {
preNode = nodeCutter.GetSpliceNode(range, nodes[nodes.length > 1 && nodes[0].nodeName === 'IMG' ? 1 : 0]);
}
if (nodes.length === 1) {
nodeSelection.setSelectionContents(docElement, preNode);
range = nodeSelection.getRange(docElement);
}
else if (nodes.length > 1) {
var i = 1;
var lastText = nodes[nodes.length - i];
while (nodes.length <= i && nodes[nodes.length - i].nodeName === 'BR') {
i++;
lastText = nodes[nodes.length - i];
}
var lasNode = nodeCutter.GetSpliceNode(range, lastText);
if (lasNode) {
nodeSelection.setSelectionText(docElement, preNode, lasNode, 0, (lasNode.nodeType === 3) ?
lasNode.textContent.length : lasNode.childNodes.length);
}
range = nodeSelection.getRange(docElement);
}
var exactNodes = nodeSelection.getNodeCollection(range);
var cloneSelectNodes = exactNodes.slice();
this.clearInlines(nodeSelection.getSelectionNodes(cloneSelectNodes), cloneSelectNodes, nodeSelection.getRange(docElement), nodeCutter, endNode);
this.reSelection(docElement, save, exactNodes);
range = nodeSelection.getRange(docElement);
exactNodes = nodeSelection.getNodeCollection(range);
var cloneParentNodes = exactNodes.slice();
this.clearBlocks(docElement, cloneParentNodes, endNode, nodeCutter, nodeSelection);
Iif (util_1.isIDevice()) {
util_1.setEditFrameFocus(endNode, selector);
}
this.reSelection(docElement, save, exactNodes);
}
Iif (cursorRange) {
nodeSelection.setCursorPoint(docElement, range.endContainer, range.endOffset);
}
};
ClearFormat.reSelection = function (docElement, save, exactNodes) {
var selectionNodes = save.getInsertNodes(exactNodes);
save.startContainer = save.getNodeArray(selectionNodes[0], true, docElement);
save.startOffset = 0;
save.endContainer = save.getNodeArray(selectionNodes[selectionNodes.length - 1], false, docElement);
var endIndexNode = selectionNodes[selectionNodes.length - 1];
save.endOffset = (endIndexNode.nodeType === 3) ? endIndexNode.textContent.length
: endIndexNode.childNodes.length;
save.restore();
};
ClearFormat.clearBlocks = function (docElement, nodes, endNode, nodeCutter, nodeSelection) {
var parentNodes = [];
for (var index = 0; index < nodes.length; index++) {
if (this.BLOCK_TAGS.indexOf(nodes[index].nodeName.toLocaleLowerCase()) > -1
&& parentNodes.indexOf(nodes[index]) === -1) {
parentNodes.push(nodes[index]);
}
else if ((this.BLOCK_TAGS.indexOf(nodes[index].parentNode.nodeName.toLocaleLowerCase()) > -1)
&& parentNodes.indexOf(nodes[index].parentNode) === -1
&& endNode !== nodes[index].parentNode) {
parentNodes.push(nodes[index].parentNode);
}
}
parentNodes = this.spliceParent(parentNodes, nodes)[0];
parentNodes = this.removeParent(parentNodes);
this.unWrap(docElement, parentNodes, nodeCutter, nodeSelection);
};
ClearFormat.spliceParent = function (parentNodes, nodes) {
for (var index1 = 0; index1 < parentNodes.length; index1++) {
var len = parentNodes[index1].childNodes.length;
for (var index2 = 0; index2 < len; index2++) {
if ((nodes.indexOf(parentNodes[index1].childNodes[index2]) > 0)
&& (parentNodes[index1].childNodes[index2].childNodes.length > 0)) {
nodes = this.spliceParent([parentNodes[index1].childNodes[index2]], nodes)[1];
}
if ((nodes.indexOf(parentNodes[index1].childNodes[index2]) <= -1) &&
(parentNodes[index1].childNodes[index2].textContent.trim() !== '')) {
for (var index3 = 0; index3 < len; index3++) {
if (nodes.indexOf(parentNodes[index1].childNodes[index3]) > -1) {
nodes.splice(nodes.indexOf(parentNodes[index1].childNodes[index3]), 1);
}
}
index2 = parentNodes[index1].childNodes.length;
var parentIndex = parentNodes.indexOf(parentNodes[index1].parentNode);
var nodeIndex = nodes.indexOf(parentNodes[index1].parentNode);
if (parentIndex > -1) {
parentNodes.splice(parentIndex, 1);
}
if (nodeIndex > -1) {
nodes.splice(nodeIndex, 1);
}
var elementIndex = nodes.indexOf(parentNodes[index1]);
if (elementIndex > -1) {
nodes.splice(elementIndex, 1);
}
parentNodes.splice(index1, 1);
index1--;
}
}
}
return [parentNodes, nodes];
};
ClearFormat.removeChild = function (parentNodes, parentNode) {
var count = parentNode.childNodes.length;
if (count > 0) {
for (var index = 0; index < count; index++) {
if (parentNodes.indexOf(parentNode.childNodes[index]) > -1) {
parentNodes = this.removeChild(parentNodes, parentNode.childNodes[index]);
parentNodes.splice(parentNodes.indexOf(parentNode.childNodes[index]), 1);
}
}
}
return parentNodes;
};
ClearFormat.removeParent = function (parentNodes) {
for (var index = 0; index < parentNodes.length; index++) {
if (parentNodes.indexOf(parentNodes[index].parentNode) > -1) {
parentNodes = this.removeChild(parentNodes, parentNodes[index]);
parentNodes.splice(index, 1);
index--;
}
}
return parentNodes;
};
ClearFormat.unWrap = function (docElement, parentNodes, nodeCutter, nodeSelection) {
for (var index1 = 0; index1 < parentNodes.length; index1++) {
if (this.NONVALID_TAGS.indexOf(parentNodes[index1].nodeName.toLowerCase()) > -1
&& parentNodes[index1].parentNode
&& this.NONVALID_PARENT_TAGS.indexOf(parentNodes[index1].parentNode.nodeName.toLowerCase()) > -1) {
nodeSelection.setSelectionText(docElement, parentNodes[index1], parentNodes[index1], 0, parentNodes[index1].childNodes.length);
insert_methods_1.InsertMethods.unwrap(nodeCutter.GetSpliceNode(nodeSelection.getRange(docElement), parentNodes[index1].parentNode));
}
var blockquoteNode = ej2_base_1.closest(parentNodes[index1], 'blockquote');
if (parentNodes[index1].nodeName.toLocaleLowerCase() !== 'blockquote' && !ej2_base_1.isNullOrUndefined(blockquoteNode) && blockquoteNode.textContent === parentNodes[index1].textContent) {
var blockNodes = this.removeParent([blockquoteNode]);
this.unWrap(docElement, blockNodes, nodeCutter, nodeSelection);
}
if (parentNodes[index1].nodeName.toLocaleLowerCase() !== 'p') {
if (this.NONVALID_PARENT_TAGS.indexOf(parentNodes[index1].nodeName.toLowerCase()) < 0
&& !((parentNodes[index1].nodeName.toLocaleLowerCase() === 'blockquote'
|| parentNodes[index1].nodeName.toLocaleLowerCase() === 'li')
&& this.IGNORE_PARENT_TAGS.indexOf(parentNodes[index1].childNodes[0].nodeName.toLocaleLowerCase()) > -1)
&& !(parentNodes[index1].childNodes.length === 1
&& parentNodes[index1].childNodes[0].nodeName.toLocaleLowerCase() === 'p')) {
insert_methods_1.InsertMethods.Wrap(parentNodes[index1], docElement.createElement(this.defaultTag));
}
var childNodes = insert_methods_1.InsertMethods.unwrap(parentNodes[index1]);
if (childNodes.length === 1
&& childNodes[0].parentNode.nodeName.toLocaleLowerCase() === 'p') {
insert_methods_1.InsertMethods.Wrap(parentNodes[index1], docElement.createElement(this.defaultTag));
insert_methods_1.InsertMethods.unwrap(parentNodes[index1]);
}
for (var index2 = 0; index2 < childNodes.length; index2++) {
if (this.NONVALID_TAGS.indexOf(childNodes[index2].nodeName.toLowerCase()) > -1) {
this.unWrap(docElement, [childNodes[index2]], nodeCutter, nodeSelection);
}
else if (this.BLOCK_TAGS.indexOf(childNodes[index2].nodeName.toLocaleLowerCase()) > -1 &&
childNodes[index2].nodeName.toLocaleLowerCase() !== 'p') {
var blockNodes = this.removeParent([childNodes[index2]]);
this.unWrap(docElement, blockNodes, nodeCutter, nodeSelection);
}
else if (this.BLOCK_TAGS.indexOf(childNodes[index2].nodeName.toLocaleLowerCase()) > -1 &&
childNodes[index2].nodeName.toLocaleLowerCase() === 'p') {
if (childNodes[index2].parentNode.nodeName.toLocaleLowerCase() === 'p') {
insert_methods_1.InsertMethods.unwrap(childNodes[index2].parentNode);
}
insert_methods_1.InsertMethods.Wrap(childNodes[index2], docElement.createElement(this.defaultTag));
insert_methods_1.InsertMethods.unwrap(childNodes[index2]);
}
else Iif (this.BLOCK_TAGS.indexOf(childNodes[index2].nodeName.toLocaleLowerCase()) > -1 &&
childNodes[index2].parentNode.nodeName.toLocaleLowerCase() ===
childNodes[index2].nodeName.toLocaleLowerCase()) {
insert_methods_1.InsertMethods.unwrap(childNodes[index2]);
}
}
}
else {
insert_methods_1.InsertMethods.Wrap(parentNodes[index1], docElement.createElement(this.defaultTag));
insert_methods_1.InsertMethods.unwrap(parentNodes[index1]);
}
}
};
ClearFormat.clearInlines = function (textNodes, nodes, range, nodeCutter, endNode) {
for (var index = 0; index < textNodes.length; index++) {
var currentInlineNode = textNodes[index];
var currentNode = void 0;
while (!this.domNode.isBlockNode(currentInlineNode) &&
(currentInlineNode.parentElement && !currentInlineNode.parentElement.classList.contains('e-img-inner'))) {
currentNode = currentInlineNode;
currentInlineNode = currentInlineNode.parentElement;
}
if (currentNode &&
isformatted_1.IsFormatted.inlineTags.indexOf(currentNode.nodeName.toLocaleLowerCase()) > -1) {
nodeCutter.GetSpliceNode(range, currentNode);
this.removeInlineParent(currentNode);
}
}
};
ClearFormat.removeInlineParent = function (textNodes) {
var nodes = insert_methods_1.InsertMethods.unwrap(textNodes);
for (var index = 0; index < nodes.length; index++) {
if (nodes[index].parentNode.childNodes.length === 1 && !nodes[index].parentNode.classList.contains('e-img-inner')
&& isformatted_1.IsFormatted.inlineTags.indexOf(nodes[index].parentNode.nodeName.toLocaleLowerCase()) > -1) {
this.removeInlineParent(nodes[index].parentNode);
}
else if (isformatted_1.IsFormatted.inlineTags.indexOf(nodes[index].nodeName.toLocaleLowerCase()) > -1) {
this.removeInlineParent(nodes[index]);
}
}
};
ClearFormat.BLOCK_TAGS = ['address', 'article', 'aside', 'blockquote',
'details', 'dd', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer',
'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'li', 'main', 'nav',
'noscript', 'ol', 'p', 'pre', 'section', 'ul'];
ClearFormat.NONVALID_PARENT_TAGS = ['thead', 'tbody', 'ul', 'ol', 'table', 'tfoot', 'tr'];
ClearFormat.IGNORE_PARENT_TAGS = ['ul', 'ol', 'table'];
ClearFormat.NONVALID_TAGS = ['thead', 'tbody', 'figcaption', 'td', 'tr', 'th', 'tfoot', 'figcaption', 'li'];
ClearFormat.defaultTag = 'p';
return ClearFormat;
}());
exports.ClearFormat = ClearFormat;
});
|