Quirks.js
11.9 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* Quirks.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 class includes fixes for various browser quirks.
*
* @class tinymce.tableplugin.Quirks
* @private
*/
define("tinymce/tableplugin/Quirks", [
"tinymce/util/VK",
"tinymce/util/Delay",
"tinymce/Env",
"tinymce/util/Tools",
"tinymce/tableplugin/Utils"
], function(VK, Delay, Env, Tools, Utils) {
var each = Tools.each, getSpanVal = Utils.getSpanVal;
return function(editor) {
/**
* Fixed caret movement around tables on WebKit.
*/
function moveWebKitSelection() {
function eventHandler(e) {
var key = e.keyCode;
function handle(upBool, sourceNode) {
var siblingDirection = upBool ? 'previousSibling' : 'nextSibling';
var currentRow = editor.dom.getParent(sourceNode, 'tr');
var siblingRow = currentRow[siblingDirection];
if (siblingRow) {
moveCursorToRow(editor, sourceNode, siblingRow, upBool);
e.preventDefault();
return true;
}
var tableNode = editor.dom.getParent(currentRow, 'table');
var middleNode = currentRow.parentNode;
var parentNodeName = middleNode.nodeName.toLowerCase();
if (parentNodeName === 'tbody' || parentNodeName === (upBool ? 'tfoot' : 'thead')) {
var targetParent = getTargetParent(upBool, tableNode, middleNode, 'tbody');
if (targetParent !== null) {
return moveToRowInTarget(upBool, targetParent, sourceNode);
}
}
return escapeTable(upBool, currentRow, siblingDirection, tableNode);
}
function getTargetParent(upBool, topNode, secondNode, nodeName) {
var tbodies = editor.dom.select('>' + nodeName, topNode);
var position = tbodies.indexOf(secondNode);
if (upBool && position === 0 || !upBool && position === tbodies.length - 1) {
return getFirstHeadOrFoot(upBool, topNode);
} else if (position === -1) {
var topOrBottom = secondNode.tagName.toLowerCase() === 'thead' ? 0 : tbodies.length - 1;
return tbodies[topOrBottom];
}
return tbodies[position + (upBool ? -1 : 1)];
}
function getFirstHeadOrFoot(upBool, parent) {
var tagName = upBool ? 'thead' : 'tfoot';
var headOrFoot = editor.dom.select('>' + tagName, parent);
return headOrFoot.length !== 0 ? headOrFoot[0] : null;
}
function moveToRowInTarget(upBool, targetParent, sourceNode) {
var targetRow = getChildForDirection(targetParent, upBool);
if (targetRow) {
moveCursorToRow(editor, sourceNode, targetRow, upBool);
}
e.preventDefault();
return true;
}
function escapeTable(upBool, currentRow, siblingDirection, table) {
var tableSibling = table[siblingDirection];
if (tableSibling) {
moveCursorToStartOfElement(tableSibling);
return true;
}
var parentCell = editor.dom.getParent(table, 'td,th');
if (parentCell) {
return handle(upBool, parentCell, e);
}
var backUpSibling = getChildForDirection(currentRow, !upBool);
moveCursorToStartOfElement(backUpSibling);
e.preventDefault();
return false;
}
function getChildForDirection(parent, up) {
var child = parent && parent[up ? 'lastChild' : 'firstChild'];
// BR is not a valid table child to return in this case we return the table cell
return child && child.nodeName === 'BR' ? editor.dom.getParent(child, 'td,th') : child;
}
function moveCursorToStartOfElement(n) {
editor.selection.setCursorLocation(n, 0);
}
function isVerticalMovement() {
return key == VK.UP || key == VK.DOWN;
}
function isInTable(editor) {
var node = editor.selection.getNode();
var currentRow = editor.dom.getParent(node, 'tr');
return currentRow !== null;
}
function columnIndex(column) {
var colIndex = 0;
var c = column;
while (c.previousSibling) {
c = c.previousSibling;
colIndex = colIndex + getSpanVal(c, "colspan");
}
return colIndex;
}
function findColumn(rowElement, columnIndex) {
var c = 0, r = 0;
each(rowElement.children, function(cell, i) {
c = c + getSpanVal(cell, "colspan");
r = i;
if (c > columnIndex) {
return false;
}
});
return r;
}
function moveCursorToRow(ed, node, row, upBool) {
var srcColumnIndex = columnIndex(editor.dom.getParent(node, 'td,th'));
var tgtColumnIndex = findColumn(row, srcColumnIndex);
var tgtNode = row.childNodes[tgtColumnIndex];
var rowCellTarget = getChildForDirection(tgtNode, upBool);
moveCursorToStartOfElement(rowCellTarget || tgtNode);
}
function shouldFixCaret(preBrowserNode) {
var newNode = editor.selection.getNode();
var newParent = editor.dom.getParent(newNode, 'td,th');
var oldParent = editor.dom.getParent(preBrowserNode, 'td,th');
return newParent && newParent !== oldParent && checkSameParentTable(newParent, oldParent);
}
function checkSameParentTable(nodeOne, NodeTwo) {
return editor.dom.getParent(nodeOne, 'TABLE') === editor.dom.getParent(NodeTwo, 'TABLE');
}
if (isVerticalMovement() && isInTable(editor)) {
var preBrowserNode = editor.selection.getNode();
Delay.setEditorTimeout(editor, function() {
if (shouldFixCaret(preBrowserNode)) {
handle(!e.shiftKey && key === VK.UP, preBrowserNode, e);
}
}, 0);
}
}
editor.on('KeyDown', function(e) {
eventHandler(e);
});
}
function fixBeforeTableCaretBug() {
// Checks if the selection/caret is at the start of the specified block element
function isAtStart(rng, par) {
var doc = par.ownerDocument, rng2 = doc.createRange(), elm;
rng2.setStartBefore(par);
rng2.setEnd(rng.endContainer, rng.endOffset);
elm = doc.createElement('body');
elm.appendChild(rng2.cloneContents());
// Check for text characters of other elements that should be treated as content
return elm.innerHTML.replace(/<(br|img|object|embed|input|textarea)[^>]*>/gi, '-').replace(/<[^>]+>/g, '').length === 0;
}
// Fixes an bug where it's impossible to place the caret before a table in Gecko
// this fix solves it by detecting when the caret is at the beginning of such a table
// and then manually moves the caret infront of the table
editor.on('KeyDown', function(e) {
var rng, table, dom = editor.dom;
// On gecko it's not possible to place the caret before a table
if (e.keyCode == 37 || e.keyCode == 38) {
rng = editor.selection.getRng();
table = dom.getParent(rng.startContainer, 'table');
if (table && editor.getBody().firstChild == table) {
if (isAtStart(rng, table)) {
rng = dom.createRng();
rng.setStartBefore(table);
rng.setEndBefore(table);
editor.selection.setRng(rng);
e.preventDefault();
}
}
}
});
}
// Fixes an issue on Gecko where it's impossible to place the caret behind a table
// This fix will force a paragraph element after the table but only when the forced_root_block setting is enabled
function fixTableCaretPos() {
editor.on('KeyDown SetContent VisualAid', function() {
var last;
// Skip empty text nodes from the end
for (last = editor.getBody().lastChild; last; last = last.previousSibling) {
if (last.nodeType == 3) {
if (last.nodeValue.length > 0) {
break;
}
} else if (last.nodeType == 1 && (last.tagName == 'BR' || !last.getAttribute('data-mce-bogus'))) {
break;
}
}
if (last && last.nodeName == 'TABLE') {
if (editor.settings.forced_root_block) {
editor.dom.add(
editor.getBody(),
editor.settings.forced_root_block,
editor.settings.forced_root_block_attrs,
Env.ie && Env.ie < 10 ? ' ' : '<br data-mce-bogus="1" />'
);
} else {
editor.dom.add(editor.getBody(), 'br', {'data-mce-bogus': '1'});
}
}
});
editor.on('PreProcess', function(o) {
var last = o.node.lastChild;
if (last && (last.nodeName == "BR" || (last.childNodes.length == 1 &&
(last.firstChild.nodeName == 'BR' || last.firstChild.nodeValue == '\u00a0'))) &&
last.previousSibling && last.previousSibling.nodeName == "TABLE") {
editor.dom.remove(last);
}
});
}
// this nasty hack is here to work around some WebKit selection bugs.
function fixTableCellSelection() {
function tableCellSelected(ed, rng, n, currentCell) {
// The decision of when a table cell is selected is somewhat involved. The fact that this code is
// required is actually a pointer to the root cause of this bug. A cell is selected when the start
// and end offsets are 0, the start container is a text, and the selection node is either a TR (most cases)
// or the parent of the table (in the case of the selection containing the last cell of a table).
var TEXT_NODE = 3, table = ed.dom.getParent(rng.startContainer, 'TABLE');
var tableParent, allOfCellSelected, tableCellSelection;
if (table) {
tableParent = table.parentNode;
}
allOfCellSelected = rng.startContainer.nodeType == TEXT_NODE &&
rng.startOffset === 0 &&
rng.endOffset === 0 &&
currentCell &&
(n.nodeName == "TR" || n == tableParent);
tableCellSelection = (n.nodeName == "TD" || n.nodeName == "TH") && !currentCell;
return allOfCellSelected || tableCellSelection;
}
function fixSelection() {
var rng = editor.selection.getRng();
var n = editor.selection.getNode();
var currentCell = editor.dom.getParent(rng.startContainer, 'TD,TH');
if (!tableCellSelected(editor, rng, n, currentCell)) {
return;
}
if (!currentCell) {
currentCell = n;
}
// Get the very last node inside the table cell
var end = currentCell.lastChild;
while (end.lastChild) {
end = end.lastChild;
}
// Select the entire table cell. Nothing outside of the table cell should be selected.
if (end.nodeType == 3) {
rng.setEnd(end, end.data.length);
editor.selection.setRng(rng);
}
}
editor.on('KeyDown', function() {
fixSelection();
});
editor.on('MouseDown', function(e) {
if (e.button != 2) {
fixSelection();
}
});
}
/**
* Delete table if all cells are selected.
*/
function deleteTable() {
function placeCaretInCell(cell) {
editor.selection.select(cell, true);
editor.selection.collapse(true);
}
function clearCell(cell) {
editor.$(cell).empty();
Utils.paddCell(cell);
}
editor.on('keydown', function(e) {
if ((e.keyCode == VK.DELETE || e.keyCode == VK.BACKSPACE) && !e.isDefaultPrevented()) {
var table, tableCells, selectedTableCells, cell;
table = editor.dom.getParent(editor.selection.getStart(), 'table');
if (table) {
tableCells = editor.dom.select('td,th', table);
selectedTableCells = Tools.grep(tableCells, function(cell) {
return !!editor.dom.getAttrib(cell, 'data-mce-selected');
});
if (selectedTableCells.length === 0) {
// If caret is within an empty table cell then empty it for real
cell = editor.dom.getParent(editor.selection.getStart(), 'td,th');
if (editor.selection.isCollapsed() && cell && editor.dom.isEmpty(cell)) {
e.preventDefault();
clearCell(cell);
placeCaretInCell(cell);
}
return;
}
e.preventDefault();
editor.undoManager.transact(function() {
if (tableCells.length == selectedTableCells.length) {
editor.execCommand('mceTableDelete');
} else {
Tools.each(selectedTableCells, clearCell);
placeCaretInCell(selectedTableCells[0]);
}
});
}
}
});
}
deleteTable();
if (Env.webkit) {
moveWebKitSelection();
fixTableCellSelection();
}
if (Env.gecko) {
fixBeforeTableCaretBug();
fixTableCaretPos();
}
if (Env.ie > 9) {
fixBeforeTableCaretBug();
fixTableCaretPos();
}
};
});