CaretContainer.js
3.76 KB
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
/**
* CaretContainer.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This module handles caret containers. A caret container is a node that
* holds the caret for positional purposes.
*
* @private
* @class tinymce.caret.CaretContainer
*/
define("tinymce/caret/CaretContainer", [
"tinymce/dom/NodeType",
"tinymce/text/Zwsp"
], function(NodeType, Zwsp) {
var isElement = NodeType.isElement,
isText = NodeType.isText;
function isCaretContainerBlock(node) {
if (isText(node)) {
node = node.parentNode;
}
return isElement(node) && node.hasAttribute('data-mce-caret');
}
function isCaretContainerInline(node) {
return isText(node) && Zwsp.isZwsp(node.data);
}
function isCaretContainer(node) {
return isCaretContainerBlock(node) || isCaretContainerInline(node);
}
function removeNode(node) {
var parentNode = node.parentNode;
if (parentNode) {
parentNode.removeChild(node);
}
}
function getNodeValue(node) {
try {
return node.nodeValue;
} catch (ex) {
// IE sometimes produces "Invalid argument" on nodes
return "";
}
}
function setNodeValue(node, text) {
if (text.length === 0) {
removeNode(node);
} else {
node.nodeValue = text;
}
}
function insertInline(node, before) {
var doc, sibling, textNode, parentNode;
doc = node.ownerDocument;
textNode = doc.createTextNode(Zwsp.ZWSP);
parentNode = node.parentNode;
if (!before) {
sibling = node.nextSibling;
if (isText(sibling)) {
if (isCaretContainer(sibling)) {
return sibling;
}
if (startsWithCaretContainer(sibling)) {
sibling.splitText(1);
return sibling;
}
}
if (node.nextSibling) {
parentNode.insertBefore(textNode, node.nextSibling);
} else {
parentNode.appendChild(textNode);
}
} else {
sibling = node.previousSibling;
if (isText(sibling)) {
if (isCaretContainer(sibling)) {
return sibling;
}
if (endsWithCaretContainer(sibling)) {
return sibling.splitText(sibling.data.length - 1);
}
}
parentNode.insertBefore(textNode, node);
}
return textNode;
}
function insertBlock(blockName, node, before) {
var doc, blockNode, parentNode;
doc = node.ownerDocument;
blockNode = doc.createElement(blockName);
blockNode.setAttribute('data-mce-caret', before ? 'before' : 'after');
blockNode.setAttribute('data-mce-bogus', 'all');
blockNode.appendChild(doc.createTextNode('\u00a0'));
parentNode = node.parentNode;
if (!before) {
if (node.nextSibling) {
parentNode.insertBefore(blockNode, node.nextSibling);
} else {
parentNode.appendChild(blockNode);
}
} else {
parentNode.insertBefore(blockNode, node);
}
return blockNode;
}
function remove(caretContainerNode) {
if (isElement(caretContainerNode) && isCaretContainer(caretContainerNode)) {
if (caretContainerNode.innerHTML != ' ') {
caretContainerNode.removeAttribute('data-mce-caret');
} else {
removeNode(caretContainerNode);
}
}
if (isText(caretContainerNode)) {
var text = Zwsp.trim(getNodeValue(caretContainerNode));
setNodeValue(caretContainerNode, text);
}
}
function startsWithCaretContainer(node) {
return isText(node) && node.data[0] == Zwsp.ZWSP;
}
function endsWithCaretContainer(node) {
return isText(node) && node.data[node.data.length - 1] == Zwsp.ZWSP;
}
return {
isCaretContainer: isCaretContainer,
isCaretContainerBlock: isCaretContainerBlock,
isCaretContainerInline: isCaretContainerInline,
insertInline: insertInline,
insertBlock: insertBlock,
remove: remove,
startsWithCaretContainer: startsWithCaretContainer,
endsWithCaretContainer: endsWithCaretContainer
};
});