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 | 1×
1×
1×
1×
211×
211×
211×
211×
1×
31×
31×
31×
31×
31×
31×
4×
1×
1×
1×
3×
3×
3×
4×
4×
27×
27×
27×
27×
66×
2×
64×
27×
27×
41×
32×
32×
9×
41×
27×
27×
27×
1×
59×
59×
48×
13×
13×
5×
8×
8×
8×
2×
6×
35×
1×
47×
47×
47×
47×
127×
41×
86×
47×
47×
86×
86×
47×
1×
365×
1×
4×
4×
4×
4×
12×
12×
4×
1×
4×
4×
4×
1×
29×
29×
63×
29×
29×
29×
1×
80×
80×
52×
28×
28×
44×
16×
28×
1×
1×
| define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DOMMethods = (function () {
function DOMMethods(editElement) {
this.directRangeElems = ['IMG', 'TABLE', 'AUDIO', 'VIDEO', 'HR'];
this.BLOCK_TAGS = ['address', 'article', 'aside', 'audio', 'blockquote',
'canvas', 'details', 'dd', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer',
'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'main', 'nav',
'noscript', 'output', 'p', 'pre', 'section', 'td', 'tfoot', 'th',
'video', 'body'];
this.editableElement = editElement;
this.currentDocument = editElement.ownerDocument;
}
DOMMethods.prototype.getBlockNode = function () {
var _this = this;
var blockCollection = [];
var selection = this.currentDocument.getSelection();
var range = selection.getRangeAt(0);
var directRange = range.startContainer === this.editableElement && range.startContainer === range.endContainer &&
range.startContainer.nodeName !== '#text';
if (directRange) {
if (range.startOffset === range.endOffset) {
var isDirectRangeElems = this.editableElement.childNodes[range.startOffset] &&
this.directRangeElems.indexOf(this.editableElement.childNodes[range.startOffset].nodeName) > -1;
Eif (isDirectRangeElems) {
blockCollection.push(this.editableElement.childNodes[range.startOffset]);
}
}
else {
var isElementRange = range.endOffset === range.startOffset + 1;
Eif (isElementRange) {
blockCollection.push(this.editableElement.childNodes[range.startOffset]);
}
}
Eif (blockCollection.length > 0) {
return blockCollection;
}
}
else {
var start = range.startContainer.nodeType === Node.TEXT_NODE ?
range.startContainer.parentElement : range.startContainer;
var end = range.endContainer.nodeType === Node.TEXT_NODE ?
range.endContainer.parentElement : range.endContainer;
var endBlockNode = this.isBlockNode(end) ? end : this.getParentBlockNode(end);
var blockNodeWalker = this.currentDocument.createTreeWalker(this.editableElement, NodeFilter.SHOW_ELEMENT, {
acceptNode: function (node) {
if (!range.intersectsNode(node)) {
return NodeFilter.FILTER_REJECT;
}
return _this.isBlockNode(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
blockNodeWalker.currentNode = start;
while (blockNodeWalker.currentNode) {
if (this.isBlockNode(blockNodeWalker.currentNode)) {
this.addToBlockCollection(blockCollection, blockNodeWalker, range);
blockNodeWalker.nextNode();
}
else {
blockNodeWalker.previousNode();
}
if (blockNodeWalker.currentNode === end || blockNodeWalker.currentNode === endBlockNode) {
this.addToBlockCollection(blockCollection, blockNodeWalker, range);
break;
}
}
}
return blockCollection;
};
DOMMethods.prototype.addToBlockCollection = function (blockCollection, blockNodeWalker, range) {
var currentNode = blockNodeWalker.currentNode;
if (blockNodeWalker.currentNode && blockCollection.indexOf(blockNodeWalker.currentNode) === -1) {
if (currentNode.nodeName === 'LI') {
var isDirectChild = !currentNode.parentNode.closest('li');
if (isDirectChild) {
blockCollection.push(blockNodeWalker.currentNode);
}
else {
var commonAncestor = range.commonAncestorContainer;
var onlyNestedLISelection = ((commonAncestor.nodeName === 'OL' ||
commonAncestor.nodeName === 'UL') && commonAncestor.closest('li')) ? true : false;
if (onlyNestedLISelection) {
blockCollection.push(blockNodeWalker.currentNode);
}
else {
return;
}
}
}
else {
blockCollection.push(blockNodeWalker.currentNode);
}
}
};
DOMMethods.prototype.getTextNodes = function (blockElem) {
var nodeCollection = [];
var selection = this.currentDocument.getSelection();
var range = selection.getRangeAt(0);
var textNodeWalker = this.currentDocument.createTreeWalker(blockElem, NodeFilter.SHOW_TEXT, {
acceptNode: function (node) {
if (!range.intersectsNode(node)) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
}
});
var textNode = textNodeWalker.nextNode();
while (textNode) {
nodeCollection.push(textNode);
textNode = textNodeWalker.nextNode();
}
return nodeCollection;
};
DOMMethods.prototype.isBlockNode = function (element) {
return (!!element && (element.nodeType === Node.ELEMENT_NODE && this.BLOCK_TAGS.indexOf(element.tagName.toLowerCase()) >= 0));
};
DOMMethods.prototype.getLastTextNode = function (node) {
var treeWalker = this.currentDocument.createTreeWalker(node, NodeFilter.SHOW_TEXT, null);
var lastTextNode = null;
var currentNode = treeWalker.nextNode();
while (currentNode) {
lastTextNode = currentNode;
currentNode = treeWalker.nextNode();
}
return lastTextNode;
};
DOMMethods.prototype.getFirstTextNode = function (node) {
var treeWalker = this.currentDocument.createTreeWalker(node, NodeFilter.SHOW_TEXT, null);
var firstTextNode = treeWalker.nextNode();
return firstTextNode;
};
DOMMethods.prototype.getParentBlockNode = function (node) {
var _this = this;
var treeWalker = this.currentDocument.createTreeWalker(this.editableElement, NodeFilter.SHOW_ELEMENT, {
acceptNode: function (currentNode) {
return _this.isBlockNode(currentNode) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
treeWalker.currentNode = node;
var blockParent = treeWalker.parentNode();
return blockParent;
};
DOMMethods.prototype.getTopMostNode = function (text) {
var domMehtods = new DOMMethods(this.editableElement);
if (domMehtods.isBlockNode(text.parentNode)) {
return text;
}
var parent = text.parentNode;
while (parent) {
if (!domMehtods.isBlockNode(parent.parentNode) && text.textContent === parent.textContent) {
parent = parent.parentNode;
}
else {
return parent;
}
}
return parent;
};
return DOMMethods;
}());
exports.DOMMethods = DOMMethods;
});
|