first commit
This commit is contained in:
263
js/tinymce/classes/caret/CaretBookmark.js
Normal file
263
js/tinymce/classes/caret/CaretBookmark.js
Normal file
@@ -0,0 +1,263 @@
|
||||
/**
|
||||
* CaretBookmark.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 creates or resolves xpath like string representation of a CaretPositions.
|
||||
*
|
||||
* The format is a / separated list of chunks with:
|
||||
* <element|text()>[index|after|before]
|
||||
*
|
||||
* For example:
|
||||
* p[0]/b[0]/text()[0],1 = <p><b>a|c</b></p>
|
||||
* p[0]/img[0],before = <p>|<img></p>
|
||||
* p[0]/img[0],after = <p><img>|</p>
|
||||
*
|
||||
* @private
|
||||
* @static
|
||||
* @class tinymce.caret.CaretBookmark
|
||||
* @example
|
||||
* var bookmark = CaretBookmark.create(rootElm, CaretPosition.before(rootElm.firstChild));
|
||||
* var caretPosition = CaretBookmark.resolve(bookmark);
|
||||
*/
|
||||
define('tinymce/caret/CaretBookmark', [
|
||||
'tinymce/dom/NodeType',
|
||||
'tinymce/dom/DOMUtils',
|
||||
'tinymce/util/Fun',
|
||||
'tinymce/util/Arr',
|
||||
'tinymce/caret/CaretPosition'
|
||||
], function(NodeType, DomUtils, Fun, Arr, CaretPosition) {
|
||||
var isText = NodeType.isText,
|
||||
isBogus = NodeType.isBogus,
|
||||
nodeIndex = DomUtils.nodeIndex;
|
||||
|
||||
function normalizedParent(node) {
|
||||
var parentNode = node.parentNode;
|
||||
|
||||
if (isBogus(parentNode)) {
|
||||
return normalizedParent(parentNode);
|
||||
}
|
||||
|
||||
return parentNode;
|
||||
}
|
||||
|
||||
function getChildNodes(node) {
|
||||
if (!node) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Arr.reduce(node.childNodes, function(result, node) {
|
||||
if (isBogus(node) && node.nodeName != 'BR') {
|
||||
result = result.concat(getChildNodes(node));
|
||||
} else {
|
||||
result.push(node);
|
||||
}
|
||||
|
||||
return result;
|
||||
}, []);
|
||||
}
|
||||
|
||||
function normalizedTextOffset(textNode, offset) {
|
||||
while ((textNode = textNode.previousSibling)) {
|
||||
if (!isText(textNode)) {
|
||||
break;
|
||||
}
|
||||
|
||||
offset += textNode.data.length;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
function equal(targetValue) {
|
||||
return function(value) {
|
||||
return targetValue === value;
|
||||
};
|
||||
}
|
||||
|
||||
function normalizedNodeIndex(node) {
|
||||
var nodes, index, numTextFragments;
|
||||
|
||||
nodes = getChildNodes(normalizedParent(node));
|
||||
index = Arr.findIndex(nodes, equal(node), node);
|
||||
nodes = nodes.slice(0, index + 1);
|
||||
numTextFragments = Arr.reduce(nodes, function(result, node, i) {
|
||||
if (isText(node) && isText(nodes[i - 1])) {
|
||||
result++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}, 0);
|
||||
|
||||
nodes = Arr.filter(nodes, NodeType.matchNodeNames(node.nodeName));
|
||||
index = Arr.findIndex(nodes, equal(node), node);
|
||||
|
||||
return index - numTextFragments;
|
||||
}
|
||||
|
||||
function createPathItem(node) {
|
||||
var name;
|
||||
|
||||
if (isText(node)) {
|
||||
name = 'text()';
|
||||
} else {
|
||||
name = node.nodeName.toLowerCase();
|
||||
}
|
||||
|
||||
return name + '[' + normalizedNodeIndex(node) + ']';
|
||||
}
|
||||
|
||||
function parentsUntil(rootNode, node, predicate) {
|
||||
var parents = [];
|
||||
|
||||
for (node = node.parentNode; node != rootNode; node = node.parentNode) {
|
||||
if (predicate && predicate(node)) {
|
||||
break;
|
||||
}
|
||||
|
||||
parents.push(node);
|
||||
}
|
||||
|
||||
return parents;
|
||||
}
|
||||
|
||||
function create(rootNode, caretPosition) {
|
||||
var container, offset, path = [],
|
||||
outputOffset, childNodes, parents;
|
||||
|
||||
container = caretPosition.container();
|
||||
offset = caretPosition.offset();
|
||||
|
||||
if (isText(container)) {
|
||||
outputOffset = normalizedTextOffset(container, offset);
|
||||
} else {
|
||||
childNodes = container.childNodes;
|
||||
if (offset >= childNodes.length) {
|
||||
outputOffset = 'after';
|
||||
offset = childNodes.length - 1;
|
||||
} else {
|
||||
outputOffset = 'before';
|
||||
}
|
||||
|
||||
container = childNodes[offset];
|
||||
}
|
||||
|
||||
path.push(createPathItem(container));
|
||||
parents = parentsUntil(rootNode, container);
|
||||
parents = Arr.filter(parents, Fun.negate(NodeType.isBogus));
|
||||
path = path.concat(Arr.map(parents, function(node) {
|
||||
return createPathItem(node);
|
||||
}));
|
||||
|
||||
return path.reverse().join('/') + ',' + outputOffset;
|
||||
}
|
||||
|
||||
function resolvePathItem(node, name, index) {
|
||||
var nodes = getChildNodes(node);
|
||||
|
||||
nodes = Arr.filter(nodes, function(node, index) {
|
||||
return !isText(node) || !isText(nodes[index - 1]);
|
||||
});
|
||||
|
||||
nodes = Arr.filter(nodes, NodeType.matchNodeNames(name));
|
||||
return nodes[index];
|
||||
}
|
||||
|
||||
function findTextPosition(container, offset) {
|
||||
var node = container, targetOffset = 0, dataLen;
|
||||
|
||||
while (isText(node)) {
|
||||
dataLen = node.data.length;
|
||||
|
||||
if (offset >= targetOffset && offset <= targetOffset + dataLen) {
|
||||
container = node;
|
||||
offset = offset - targetOffset;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!isText(node.nextSibling)) {
|
||||
container = node;
|
||||
offset = dataLen;
|
||||
break;
|
||||
}
|
||||
|
||||
targetOffset += dataLen;
|
||||
node = node.nextSibling;
|
||||
}
|
||||
|
||||
if (offset > container.data.length) {
|
||||
offset = container.data.length;
|
||||
}
|
||||
|
||||
return new CaretPosition(container, offset);
|
||||
}
|
||||
|
||||
function resolve(rootNode, path) {
|
||||
var parts, container, offset;
|
||||
|
||||
if (!path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
parts = path.split(',');
|
||||
path = parts[0].split('/');
|
||||
offset = parts.length > 1 ? parts[1] : 'before';
|
||||
|
||||
container = Arr.reduce(path, function(result, value) {
|
||||
value = /([\w\-\(\)]+)\[([0-9]+)\]/.exec(value);
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value[1] === 'text()') {
|
||||
value[1] = '#text';
|
||||
}
|
||||
|
||||
return resolvePathItem(result, value[1], parseInt(value[2], 10));
|
||||
}, rootNode);
|
||||
|
||||
if (!container) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isText(container)) {
|
||||
if (offset === 'after') {
|
||||
offset = nodeIndex(container) + 1;
|
||||
} else {
|
||||
offset = nodeIndex(container);
|
||||
}
|
||||
|
||||
return new CaretPosition(container.parentNode, offset);
|
||||
}
|
||||
|
||||
return findTextPosition(container, parseInt(offset, 10));
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Create a xpath bookmark location for the specified caret position.
|
||||
*
|
||||
* @method create
|
||||
* @param {Node} rootNode Root node to create bookmark within.
|
||||
* @param {tinymce.caret.CaretPosition} caretPosition Caret position within the root node.
|
||||
* @return {String} String xpath like location of caret position.
|
||||
*/
|
||||
create: create,
|
||||
|
||||
/**
|
||||
* Resolves a xpath like bookmark location to the a caret position.
|
||||
*
|
||||
* @method resolve
|
||||
* @param {Node} rootNode Root node to resolve xpath bookmark within.
|
||||
* @param {String} bookmark Bookmark string to resolve.
|
||||
* @return {tinymce.caret.CaretPosition} Caret position resolved from xpath like bookmark.
|
||||
*/
|
||||
resolve: resolve
|
||||
};
|
||||
});
|
||||
86
js/tinymce/classes/caret/CaretCandidate.js
Normal file
86
js/tinymce/classes/caret/CaretCandidate.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* CaretCandidate.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 contains logic for handling caret candidates. A caret candidate is
|
||||
* for example text nodes, images, input elements, cE=false elements etc.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.CaretCandidate
|
||||
*/
|
||||
define("tinymce/caret/CaretCandidate", [
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/util/Arr",
|
||||
"tinymce/caret/CaretContainer"
|
||||
], function(NodeType, Arr, CaretContainer) {
|
||||
var isContentEditableTrue = NodeType.isContentEditableTrue,
|
||||
isContentEditableFalse = NodeType.isContentEditableFalse,
|
||||
isBr = NodeType.isBr,
|
||||
isText = NodeType.isText,
|
||||
isInvalidTextElement = NodeType.matchNodeNames('script style textarea'),
|
||||
isAtomicInline = NodeType.matchNodeNames('img input textarea hr iframe video audio object'),
|
||||
isTable = NodeType.matchNodeNames('table'),
|
||||
isCaretContainer = CaretContainer.isCaretContainer;
|
||||
|
||||
function isCaretCandidate(node) {
|
||||
if (isCaretContainer(node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isText(node)) {
|
||||
if (isInvalidTextElement(node.parentNode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return isAtomicInline(node) || isBr(node) || isTable(node) || isContentEditableFalse(node);
|
||||
}
|
||||
|
||||
function isInEditable(node, rootNode) {
|
||||
for (node = node.parentNode; node && node != rootNode; node = node.parentNode) {
|
||||
if (isContentEditableFalse(node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isContentEditableTrue(node)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function isAtomicContentEditableFalse(node) {
|
||||
if (!isContentEditableFalse(node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Arr.reduce(node.getElementsByTagName('*'), function(result, elm) {
|
||||
return result || isContentEditableTrue(elm);
|
||||
}, false) !== true;
|
||||
}
|
||||
|
||||
function isAtomic(node) {
|
||||
return isAtomicInline(node) || isAtomicContentEditableFalse(node);
|
||||
}
|
||||
|
||||
function isEditableCaretCandidate(node, rootNode) {
|
||||
return isCaretCandidate(node) && isInEditable(node, rootNode);
|
||||
}
|
||||
|
||||
return {
|
||||
isCaretCandidate: isCaretCandidate,
|
||||
isInEditable: isInEditable,
|
||||
isAtomic: isAtomic,
|
||||
isEditableCaretCandidate: isEditableCaretCandidate
|
||||
};
|
||||
});
|
||||
151
js/tinymce/classes/caret/CaretContainer.js
Normal file
151
js/tinymce/classes/caret/CaretContainer.js
Normal file
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* 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 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) {
|
||||
var text;
|
||||
|
||||
if (isElement(caretContainerNode) && isCaretContainer(caretContainerNode)) {
|
||||
if (caretContainerNode.innerHTML != ' ') {
|
||||
caretContainerNode.removeAttribute('data-mce-caret');
|
||||
} else {
|
||||
if (caretContainerNode.parentNode) {
|
||||
caretContainerNode.parentNode.removeChild(caretContainerNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isText(caretContainerNode)) {
|
||||
text = Zwsp.trim(caretContainerNode.data);
|
||||
|
||||
if (text.length === 0) {
|
||||
if (caretContainerNode.parentNode) {
|
||||
caretContainerNode.parentNode.removeChild(caretContainerNode);
|
||||
}
|
||||
}
|
||||
|
||||
caretContainerNode.nodeValue = 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
|
||||
};
|
||||
});
|
||||
371
js/tinymce/classes/caret/CaretPosition.js
Normal file
371
js/tinymce/classes/caret/CaretPosition.js
Normal file
@@ -0,0 +1,371 @@
|
||||
/**
|
||||
* CaretPosition.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 contains logic for creating caret positions within a document a caretposition
|
||||
* is similar to a DOMRange object but it doesn't have two endpoints and is also more lightweight
|
||||
* since it's now updated live when the DOM changes.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.CaretPosition
|
||||
* @example
|
||||
* var caretPos1 = new CaretPosition(container, offset);
|
||||
* var caretPos2 = CaretPosition.fromRangeStart(someRange);
|
||||
*/
|
||||
define("tinymce/caret/CaretPosition", [
|
||||
"tinymce/util/Fun",
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/dom/DOMUtils",
|
||||
"tinymce/dom/RangeUtils",
|
||||
"tinymce/caret/CaretCandidate",
|
||||
"tinymce/geom/ClientRect",
|
||||
"tinymce/text/ExtendingChar"
|
||||
], function(Fun, NodeType, DOMUtils, RangeUtils, CaretCandidate, ClientRect, ExtendingChar) {
|
||||
var isElement = NodeType.isElement,
|
||||
isCaretCandidate = CaretCandidate.isCaretCandidate,
|
||||
isBlock = NodeType.matchStyleValues('display', 'block table'),
|
||||
isFloated = NodeType.matchStyleValues('float', 'left right'),
|
||||
isValidElementCaretCandidate = Fun.and(isElement, isCaretCandidate, Fun.negate(isFloated)),
|
||||
isNotPre = Fun.negate(NodeType.matchStyleValues('white-space', 'pre pre-line pre-wrap')),
|
||||
isText = NodeType.isText,
|
||||
isBr = NodeType.isBr,
|
||||
nodeIndex = DOMUtils.nodeIndex,
|
||||
resolveIndex = RangeUtils.getNode;
|
||||
|
||||
function isWhiteSpace(chr) {
|
||||
return chr && /[\r\n\t ]/.test(chr);
|
||||
}
|
||||
|
||||
function isHiddenWhiteSpaceRange(range) {
|
||||
var container = range.startContainer,
|
||||
offset = range.startOffset,
|
||||
text;
|
||||
|
||||
if (isWhiteSpace(range.toString()) && isNotPre(container.parentNode)) {
|
||||
text = container.data;
|
||||
|
||||
if (isWhiteSpace(text[offset - 1]) || isWhiteSpace(text[offset + 1])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getCaretPositionClientRects(caretPosition) {
|
||||
var clientRects = [], beforeNode, node;
|
||||
|
||||
// Hack for older WebKit versions that doesn't
|
||||
// support getBoundingClientRect on BR elements
|
||||
function getBrClientRect(brNode) {
|
||||
var doc = brNode.ownerDocument,
|
||||
rng = doc.createRange(),
|
||||
nbsp = doc.createTextNode('\u00a0'),
|
||||
parentNode = brNode.parentNode,
|
||||
clientRect;
|
||||
|
||||
parentNode.insertBefore(nbsp, brNode);
|
||||
rng.setStart(nbsp, 0);
|
||||
rng.setEnd(nbsp, 1);
|
||||
clientRect = ClientRect.clone(rng.getBoundingClientRect());
|
||||
parentNode.removeChild(nbsp);
|
||||
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
function getBoundingClientRect(item) {
|
||||
var clientRect, clientRects;
|
||||
|
||||
clientRects = item.getClientRects();
|
||||
if (clientRects.length > 0) {
|
||||
clientRect = ClientRect.clone(clientRects[0]);
|
||||
} else {
|
||||
clientRect = ClientRect.clone(item.getBoundingClientRect());
|
||||
}
|
||||
|
||||
if (isBr(item) && clientRect.left === 0) {
|
||||
return getBrClientRect(item);
|
||||
}
|
||||
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
function collapseAndInflateWidth(clientRect, toStart) {
|
||||
clientRect = ClientRect.collapse(clientRect, toStart);
|
||||
clientRect.width = 1;
|
||||
clientRect.right = clientRect.left + 1;
|
||||
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
function addUniqueAndValidRect(clientRect) {
|
||||
if (clientRect.height === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (clientRects.length > 0) {
|
||||
if (ClientRect.isEqual(clientRect, clientRects[clientRects.length - 1])) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
clientRects.push(clientRect);
|
||||
}
|
||||
|
||||
function addCharacterOffset(container, offset) {
|
||||
var range = container.ownerDocument.createRange();
|
||||
|
||||
if (offset < container.data.length) {
|
||||
if (ExtendingChar.isExtendingChar(container.data[offset])) {
|
||||
return clientRects;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
range.setStart(container, offset - 1);
|
||||
range.setEnd(container, offset);
|
||||
|
||||
if (!isHiddenWhiteSpaceRange(range)) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(range), false));
|
||||
}
|
||||
}
|
||||
|
||||
if (offset < container.data.length) {
|
||||
range.setStart(container, offset);
|
||||
range.setEnd(container, offset + 1);
|
||||
|
||||
if (!isHiddenWhiteSpaceRange(range)) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(range), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isText(caretPosition.container())) {
|
||||
addCharacterOffset(caretPosition.container(), caretPosition.offset());
|
||||
return clientRects;
|
||||
}
|
||||
|
||||
if (isElement(caretPosition.container())) {
|
||||
if (caretPosition.isAtEnd()) {
|
||||
node = resolveIndex(caretPosition.container(), caretPosition.offset());
|
||||
if (isText(node)) {
|
||||
addCharacterOffset(node, node.data.length);
|
||||
}
|
||||
|
||||
if (isValidElementCaretCandidate(node) && !isBr(node)) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), false));
|
||||
}
|
||||
} else {
|
||||
node = resolveIndex(caretPosition.container(), caretPosition.offset());
|
||||
if (isText(node)) {
|
||||
addCharacterOffset(node, 0);
|
||||
}
|
||||
|
||||
if (isValidElementCaretCandidate(node) && caretPosition.isAtEnd()) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), false));
|
||||
return clientRects;
|
||||
}
|
||||
|
||||
beforeNode = resolveIndex(caretPosition.container(), caretPosition.offset() - 1);
|
||||
if (isValidElementCaretCandidate(beforeNode) && !isBr(beforeNode)) {
|
||||
if (isBlock(beforeNode) || isBlock(node) || !isValidElementCaretCandidate(node)) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(beforeNode), false));
|
||||
}
|
||||
}
|
||||
|
||||
if (isValidElementCaretCandidate(node)) {
|
||||
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return clientRects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a location within the document by a container and an offset.
|
||||
*
|
||||
* @constructor
|
||||
* @param {Node} container Container node.
|
||||
* @param {Number} offset Offset within that container node.
|
||||
* @param {Array} clientRects Optional client rects array for the position.
|
||||
*/
|
||||
function CaretPosition(container, offset, clientRects) {
|
||||
function isAtStart() {
|
||||
if (isText(container)) {
|
||||
return offset === 0;
|
||||
}
|
||||
|
||||
return offset === 0;
|
||||
}
|
||||
|
||||
function isAtEnd() {
|
||||
if (isText(container)) {
|
||||
return offset >= container.data.length;
|
||||
}
|
||||
|
||||
return offset >= container.childNodes.length;
|
||||
}
|
||||
|
||||
function toRange() {
|
||||
var range;
|
||||
|
||||
range = container.ownerDocument.createRange();
|
||||
range.setStart(container, offset);
|
||||
range.setEnd(container, offset);
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
function getClientRects() {
|
||||
if (!clientRects) {
|
||||
clientRects = getCaretPositionClientRects(new CaretPosition(container, offset));
|
||||
}
|
||||
|
||||
return clientRects;
|
||||
}
|
||||
|
||||
function isVisible() {
|
||||
return getClientRects().length > 0;
|
||||
}
|
||||
|
||||
function isEqual(caretPosition) {
|
||||
return caretPosition && container === caretPosition.container() && offset === caretPosition.offset();
|
||||
}
|
||||
|
||||
function getNode(before) {
|
||||
return resolveIndex(container, before ? offset - 1 : offset);
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Returns the container node.
|
||||
*
|
||||
* @method container
|
||||
* @return {Node} Container node.
|
||||
*/
|
||||
container: Fun.constant(container),
|
||||
|
||||
/**
|
||||
* Returns the offset within the container node.
|
||||
*
|
||||
* @method offset
|
||||
* @return {Number} Offset within the container node.
|
||||
*/
|
||||
offset: Fun.constant(offset),
|
||||
|
||||
/**
|
||||
* Returns a range out of a the caret position.
|
||||
*
|
||||
* @method toRange
|
||||
* @return {DOMRange} range for the caret position.
|
||||
*/
|
||||
toRange: toRange,
|
||||
|
||||
/**
|
||||
* Returns the client rects for the caret position. Might be multiple rects between
|
||||
* block elements.
|
||||
*
|
||||
* @method getClientRects
|
||||
* @return {Array} Array of client rects.
|
||||
*/
|
||||
getClientRects: getClientRects,
|
||||
|
||||
/**
|
||||
* Returns true if the caret location is visible/displayed on screen.
|
||||
*
|
||||
* @method isVisible
|
||||
* @return {Boolean} true/false if the position is visible or not.
|
||||
*/
|
||||
isVisible: isVisible,
|
||||
|
||||
/**
|
||||
* Returns true if the caret location is at the beginning of text node or container.
|
||||
*
|
||||
* @method isVisible
|
||||
* @return {Boolean} true/false if the position is at the beginning.
|
||||
*/
|
||||
isAtStart: isAtStart,
|
||||
|
||||
/**
|
||||
* Returns true if the caret location is at the end of text node or container.
|
||||
*
|
||||
* @method isVisible
|
||||
* @return {Boolean} true/false if the position is at the end.
|
||||
*/
|
||||
isAtEnd: isAtEnd,
|
||||
|
||||
/**
|
||||
* Compares the caret position to another caret position. This will only compare the
|
||||
* container and offset not it's visual position.
|
||||
*
|
||||
* @method isEqual
|
||||
* @param {tinymce.caret.CaretPosition} caretPosition Caret position to compare with.
|
||||
* @return {Boolean} true if the caret positions are equal.
|
||||
*/
|
||||
isEqual: isEqual,
|
||||
|
||||
/**
|
||||
* Returns the closest resolved node from a node index. That means if you have an offset after the
|
||||
* last node in a container it will return that last node.
|
||||
*
|
||||
* @method getNode
|
||||
* @return {Node} Node that is closest to the index.
|
||||
*/
|
||||
getNode: getNode
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a caret position from the start of a range.
|
||||
*
|
||||
* @method fromRangeStart
|
||||
* @param {DOMRange} range DOM Range to create caret position from.
|
||||
* @return {tinymce.caret.CaretPosition} Caret position from the start of DOM range.
|
||||
*/
|
||||
CaretPosition.fromRangeStart = function(range) {
|
||||
return new CaretPosition(range.startContainer, range.startOffset);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a caret position from the end of a range.
|
||||
*
|
||||
* @method fromRangeEnd
|
||||
* @param {DOMRange} range DOM Range to create caret position from.
|
||||
* @return {tinymce.caret.CaretPosition} Caret position from the end of DOM range.
|
||||
*/
|
||||
CaretPosition.fromRangeEnd = function(range) {
|
||||
return new CaretPosition(range.endContainer, range.endOffset);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a caret position from a node and places the offset after it.
|
||||
*
|
||||
* @method after
|
||||
* @param {Node} node Node to get caret position from.
|
||||
* @return {tinymce.caret.CaretPosition} Caret position from the node.
|
||||
*/
|
||||
CaretPosition.after = function(node) {
|
||||
return new CaretPosition(node.parentNode, nodeIndex(node) + 1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a caret position from a node and places the offset before it.
|
||||
*
|
||||
* @method before
|
||||
* @param {Node} node Node to get caret position from.
|
||||
* @return {tinymce.caret.CaretPosition} Caret position from the node.
|
||||
*/
|
||||
CaretPosition.before = function(node) {
|
||||
return new CaretPosition(node.parentNode, nodeIndex(node));
|
||||
};
|
||||
|
||||
return CaretPosition;
|
||||
});
|
||||
302
js/tinymce/classes/caret/CaretUtils.js
Normal file
302
js/tinymce/classes/caret/CaretUtils.js
Normal file
@@ -0,0 +1,302 @@
|
||||
/**
|
||||
* CaretUtils.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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utility functions shared by the caret logic.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.CaretUtils
|
||||
*/
|
||||
define("tinymce/caret/CaretUtils", [
|
||||
"tinymce/util/Fun",
|
||||
"tinymce/dom/TreeWalker",
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/caret/CaretPosition",
|
||||
"tinymce/caret/CaretContainer",
|
||||
"tinymce/caret/CaretCandidate"
|
||||
], function(Fun, TreeWalker, NodeType, CaretPosition, CaretContainer, CaretCandidate) {
|
||||
var isContentEditableTrue = NodeType.isContentEditableTrue,
|
||||
isContentEditableFalse = NodeType.isContentEditableFalse,
|
||||
isBlockLike = NodeType.matchStyleValues('display', 'block table table-cell table-caption'),
|
||||
isCaretContainer = CaretContainer.isCaretContainer,
|
||||
curry = Fun.curry,
|
||||
isElement = NodeType.isElement,
|
||||
isCaretCandidate = CaretCandidate.isCaretCandidate;
|
||||
|
||||
function isForwards(direction) {
|
||||
return direction > 0;
|
||||
}
|
||||
|
||||
function isBackwards(direction) {
|
||||
return direction < 0;
|
||||
}
|
||||
|
||||
function findNode(node, direction, predicateFn, rootNode, shallow) {
|
||||
var walker = new TreeWalker(node, rootNode);
|
||||
|
||||
if (isBackwards(direction)) {
|
||||
if (isContentEditableFalse(node)) {
|
||||
node = walker.prev(true);
|
||||
if (predicateFn(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
while ((node = walker.prev(shallow))) {
|
||||
if (predicateFn(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isForwards(direction)) {
|
||||
if (isContentEditableFalse(node)) {
|
||||
node = walker.next(true);
|
||||
if (predicateFn(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
while ((node = walker.next(shallow))) {
|
||||
if (predicateFn(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getEditingHost(node, rootNode) {
|
||||
for (node = node.parentNode; node && node != rootNode; node = node.parentNode) {
|
||||
if (isContentEditableTrue(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
function getParentBlock(node, rootNode) {
|
||||
while (node && node != rootNode) {
|
||||
if (isBlockLike(node)) {
|
||||
return node;
|
||||
}
|
||||
|
||||
node = node.parentNode;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isInSameBlock(caretPosition1, caretPosition2, rootNode) {
|
||||
return getParentBlock(caretPosition1.container(), rootNode) == getParentBlock(caretPosition2.container(), rootNode);
|
||||
}
|
||||
|
||||
function isInSameEditingHost(caretPosition1, caretPosition2, rootNode) {
|
||||
return getEditingHost(caretPosition1.container(), rootNode) == getEditingHost(caretPosition2.container(), rootNode);
|
||||
}
|
||||
|
||||
function getChildNodeAtRelativeOffset(relativeOffset, caretPosition) {
|
||||
var container, offset;
|
||||
|
||||
if (!caretPosition) {
|
||||
return null;
|
||||
}
|
||||
|
||||
container = caretPosition.container();
|
||||
offset = caretPosition.offset();
|
||||
|
||||
if (!isElement(container)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return container.childNodes[offset + relativeOffset];
|
||||
}
|
||||
|
||||
function beforeAfter(before, node) {
|
||||
var range = node.ownerDocument.createRange();
|
||||
|
||||
if (before) {
|
||||
range.setStartBefore(node);
|
||||
range.setEndBefore(node);
|
||||
} else {
|
||||
range.setStartAfter(node);
|
||||
range.setEndAfter(node);
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
function isNodesInSameBlock(rootNode, node1, node2) {
|
||||
return getParentBlock(node1, rootNode) == getParentBlock(node2, rootNode);
|
||||
}
|
||||
|
||||
function lean(left, rootNode, node) {
|
||||
var sibling, siblingName;
|
||||
|
||||
if (left) {
|
||||
siblingName = 'previousSibling';
|
||||
} else {
|
||||
siblingName = 'nextSibling';
|
||||
}
|
||||
|
||||
while (node && node != rootNode) {
|
||||
sibling = node[siblingName];
|
||||
|
||||
if (isCaretContainer(sibling)) {
|
||||
sibling = sibling[siblingName];
|
||||
}
|
||||
|
||||
if (isContentEditableFalse(sibling)) {
|
||||
if (isNodesInSameBlock(rootNode, sibling, node)) {
|
||||
return sibling;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (isCaretCandidate(sibling)) {
|
||||
break;
|
||||
}
|
||||
|
||||
node = node.parentNode;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
var before = curry(beforeAfter, true);
|
||||
var after = curry(beforeAfter, false);
|
||||
|
||||
function normalizeRange(direction, rootNode, range) {
|
||||
var node, container, offset, location;
|
||||
var leanLeft = curry(lean, true, rootNode);
|
||||
var leanRight = curry(lean, false, rootNode);
|
||||
|
||||
container = range.startContainer;
|
||||
offset = range.startOffset;
|
||||
|
||||
if (CaretContainer.isCaretContainerBlock(container)) {
|
||||
if (!isElement(container)) {
|
||||
container = container.parentNode;
|
||||
}
|
||||
|
||||
location = container.getAttribute('data-mce-caret');
|
||||
|
||||
if (location == 'before') {
|
||||
node = container.nextSibling;
|
||||
if (isContentEditableFalse(node)) {
|
||||
return before(node);
|
||||
}
|
||||
}
|
||||
|
||||
if (location == 'after') {
|
||||
node = container.previousSibling;
|
||||
if (isContentEditableFalse(node)) {
|
||||
return after(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!range.collapsed) {
|
||||
return range;
|
||||
}
|
||||
|
||||
if (NodeType.isText(container)) {
|
||||
if (isCaretContainer(container)) {
|
||||
if (direction === 1) {
|
||||
node = leanRight(container);
|
||||
if (node) {
|
||||
return before(node);
|
||||
}
|
||||
|
||||
node = leanLeft(container);
|
||||
if (node) {
|
||||
return after(node);
|
||||
}
|
||||
}
|
||||
|
||||
if (direction === -1) {
|
||||
node = leanLeft(container);
|
||||
if (node) {
|
||||
return after(node);
|
||||
}
|
||||
|
||||
node = leanRight(container);
|
||||
if (node) {
|
||||
return before(node);
|
||||
}
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
if (CaretContainer.endsWithCaretContainer(container) && offset >= container.data.length - 1) {
|
||||
if (direction === 1) {
|
||||
node = leanRight(container);
|
||||
if (node) {
|
||||
return before(node);
|
||||
}
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
if (CaretContainer.startsWithCaretContainer(container) && offset <= 1) {
|
||||
if (direction === -1) {
|
||||
node = leanLeft(container);
|
||||
if (node) {
|
||||
return after(node);
|
||||
}
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
if (offset === container.data.length) {
|
||||
node = leanRight(container);
|
||||
if (node) {
|
||||
return before(node);
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
if (offset === 0) {
|
||||
node = leanLeft(container);
|
||||
if (node) {
|
||||
return after(node);
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
function isNextToContentEditableFalse(relativeOffset, caretPosition) {
|
||||
return isContentEditableFalse(getChildNodeAtRelativeOffset(relativeOffset, caretPosition));
|
||||
}
|
||||
|
||||
return {
|
||||
isForwards: isForwards,
|
||||
isBackwards: isBackwards,
|
||||
findNode: findNode,
|
||||
getEditingHost: getEditingHost,
|
||||
getParentBlock: getParentBlock,
|
||||
isInSameBlock: isInSameBlock,
|
||||
isInSameEditingHost: isInSameEditingHost,
|
||||
isBeforeContentEditableFalse: curry(isNextToContentEditableFalse, 0),
|
||||
isAfterContentEditableFalse: curry(isNextToContentEditableFalse, -1),
|
||||
normalizeRange: normalizeRange
|
||||
};
|
||||
});
|
||||
215
js/tinymce/classes/caret/CaretWalker.js
Normal file
215
js/tinymce/classes/caret/CaretWalker.js
Normal file
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* CaretWalker.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 contains logic for moving around a virtual caret in logical order within a DOM element.
|
||||
*
|
||||
* It ignores the most obvious invalid caret locations such as within a script element or within a
|
||||
* contentEditable=false element but it will return locations that isn't possible to render visually.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.CaretWalker
|
||||
* @example
|
||||
* var caretWalker = new CaretWalker(rootElm);
|
||||
*
|
||||
* var prevLogicalCaretPosition = caretWalker.prev(CaretPosition.fromRangeStart(range));
|
||||
* var nextLogicalCaretPosition = caretWalker.next(CaretPosition.fromRangeEnd(range));
|
||||
*/
|
||||
define("tinymce/caret/CaretWalker", [
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/caret/CaretCandidate",
|
||||
"tinymce/caret/CaretPosition",
|
||||
"tinymce/caret/CaretUtils",
|
||||
"tinymce/util/Arr",
|
||||
"tinymce/util/Fun"
|
||||
], function(NodeType, CaretCandidate, CaretPosition, CaretUtils, Arr, Fun) {
|
||||
var isContentEditableFalse = NodeType.isContentEditableFalse,
|
||||
isText = NodeType.isText,
|
||||
isElement = NodeType.isElement,
|
||||
isForwards = CaretUtils.isForwards,
|
||||
isBackwards = CaretUtils.isBackwards,
|
||||
isCaretCandidate = CaretCandidate.isCaretCandidate,
|
||||
isAtomic = CaretCandidate.isAtomic,
|
||||
isEditableCaretCandidate = CaretCandidate.isEditableCaretCandidate;
|
||||
|
||||
function getParents(node, rootNode) {
|
||||
var parents = [];
|
||||
|
||||
while (node && node != rootNode) {
|
||||
parents.push(node);
|
||||
node = node.parentNode;
|
||||
}
|
||||
|
||||
return parents;
|
||||
}
|
||||
|
||||
function nodeAtIndex(container, offset) {
|
||||
if (container.hasChildNodes() && offset < container.childNodes.length) {
|
||||
return container.childNodes[offset];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCaretCandidatePosition(direction, node) {
|
||||
if (isForwards(direction)) {
|
||||
if (isCaretCandidate(node.previousSibling) && !isText(node.previousSibling)) {
|
||||
return CaretPosition.before(node);
|
||||
}
|
||||
|
||||
if (isText(node)) {
|
||||
return CaretPosition(node, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (isBackwards(direction)) {
|
||||
if (isCaretCandidate(node.nextSibling) && !isText(node.nextSibling)) {
|
||||
return CaretPosition.after(node);
|
||||
}
|
||||
|
||||
if (isText(node)) {
|
||||
return CaretPosition(node, node.data.length);
|
||||
}
|
||||
}
|
||||
|
||||
if (isBackwards(direction)) {
|
||||
return CaretPosition.after(node);
|
||||
}
|
||||
|
||||
return CaretPosition.before(node);
|
||||
}
|
||||
|
||||
function findCaretPosition(direction, startCaretPosition, rootNode) {
|
||||
var container, offset, node, nextNode, innerNode,
|
||||
rootContentEditableFalseElm, caretPosition;
|
||||
|
||||
if (!isElement(rootNode) || !startCaretPosition) {
|
||||
return null;
|
||||
}
|
||||
|
||||
caretPosition = startCaretPosition;
|
||||
container = caretPosition.container();
|
||||
offset = caretPosition.offset();
|
||||
|
||||
if (isText(container)) {
|
||||
if (isBackwards(direction) && offset > 0) {
|
||||
return CaretPosition(container, --offset);
|
||||
}
|
||||
|
||||
if (isForwards(direction) && offset < container.length) {
|
||||
return CaretPosition(container, ++offset);
|
||||
}
|
||||
|
||||
node = container;
|
||||
} else {
|
||||
if (isBackwards(direction) && offset > 0) {
|
||||
nextNode = nodeAtIndex(container, offset - 1);
|
||||
if (isCaretCandidate(nextNode)) {
|
||||
if (!isAtomic(nextNode)) {
|
||||
innerNode = CaretUtils.findNode(nextNode, direction, isEditableCaretCandidate, nextNode);
|
||||
if (innerNode) {
|
||||
if (isText(innerNode)) {
|
||||
return CaretPosition(innerNode, innerNode.data.length);
|
||||
}
|
||||
|
||||
return CaretPosition.after(innerNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (isText(nextNode)) {
|
||||
return CaretPosition(nextNode, nextNode.data.length);
|
||||
}
|
||||
|
||||
return CaretPosition.before(nextNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (isForwards(direction) && offset < container.childNodes.length) {
|
||||
nextNode = nodeAtIndex(container, offset);
|
||||
if (isCaretCandidate(nextNode)) {
|
||||
if (!isAtomic(nextNode)) {
|
||||
innerNode = CaretUtils.findNode(nextNode, direction, isEditableCaretCandidate, nextNode);
|
||||
if (innerNode) {
|
||||
if (isText(innerNode)) {
|
||||
return CaretPosition(innerNode, 0);
|
||||
}
|
||||
|
||||
return CaretPosition.before(innerNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (isText(nextNode)) {
|
||||
return CaretPosition(nextNode, 0);
|
||||
}
|
||||
|
||||
return CaretPosition.after(nextNode);
|
||||
}
|
||||
}
|
||||
|
||||
node = caretPosition.getNode();
|
||||
}
|
||||
|
||||
if ((isForwards(direction) && caretPosition.isAtEnd()) || (isBackwards(direction) && caretPosition.isAtStart())) {
|
||||
node = CaretUtils.findNode(node, direction, Fun.constant(true), rootNode, true);
|
||||
if (isEditableCaretCandidate(node)) {
|
||||
return getCaretCandidatePosition(direction, node);
|
||||
}
|
||||
}
|
||||
|
||||
nextNode = CaretUtils.findNode(node, direction, isEditableCaretCandidate, rootNode);
|
||||
|
||||
rootContentEditableFalseElm = Arr.last(Arr.filter(getParents(container, rootNode), isContentEditableFalse));
|
||||
if (rootContentEditableFalseElm && (!nextNode || !rootContentEditableFalseElm.contains(nextNode))) {
|
||||
if (isForwards(direction)) {
|
||||
caretPosition = CaretPosition.after(rootContentEditableFalseElm);
|
||||
} else {
|
||||
caretPosition = CaretPosition.before(rootContentEditableFalseElm);
|
||||
}
|
||||
|
||||
return caretPosition;
|
||||
}
|
||||
|
||||
if (nextNode) {
|
||||
return getCaretCandidatePosition(direction, nextNode);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return function(rootNode) {
|
||||
return {
|
||||
/**
|
||||
* Returns the next logical caret position from the specificed input
|
||||
* caretPoisiton or null if there isn't any more positions left for example
|
||||
* at the end specified root element.
|
||||
*
|
||||
* @method next
|
||||
* @param {tinymce.caret.CaretPosition} caretPosition Caret position to start from.
|
||||
* @return {tinymce.caret.CaretPosition} CaretPosition or null if no position was found.
|
||||
*/
|
||||
next: function(caretPosition) {
|
||||
return findCaretPosition(1, caretPosition, rootNode);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the previous logical caret position from the specificed input
|
||||
* caretPoisiton or null if there isn't any more positions left for example
|
||||
* at the end specified root element.
|
||||
*
|
||||
* @method prev
|
||||
* @param {tinymce.caret.CaretPosition} caretPosition Caret position to start from.
|
||||
* @return {tinymce.caret.CaretPosition} CaretPosition or null if no position was found.
|
||||
*/
|
||||
prev: function(caretPosition) {
|
||||
return findCaretPosition(-1, caretPosition, rootNode);
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
192
js/tinymce/classes/caret/FakeCaret.js
Normal file
192
js/tinymce/classes/caret/FakeCaret.js
Normal file
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* FakeCaret.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 contains logic for rendering a fake visual caret.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.FakeCaret
|
||||
*/
|
||||
define("tinymce/caret/FakeCaret", [
|
||||
"tinymce/caret/CaretContainer",
|
||||
"tinymce/caret/CaretPosition",
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/dom/RangeUtils",
|
||||
"tinymce/dom/DomQuery",
|
||||
"tinymce/geom/ClientRect",
|
||||
"tinymce/util/Delay"
|
||||
], function(CaretContainer, CaretPosition, NodeType, RangeUtils, $, ClientRect, Delay) {
|
||||
var isContentEditableFalse = NodeType.isContentEditableFalse;
|
||||
|
||||
return function(rootNode, isBlock) {
|
||||
var cursorInterval, $lastVisualCaret, caretContainerNode;
|
||||
|
||||
function getAbsoluteClientRect(node, before) {
|
||||
var clientRect = ClientRect.collapse(node.getBoundingClientRect(), before),
|
||||
docElm, scrollX, scrollY, margin, rootRect;
|
||||
|
||||
if (rootNode.tagName == 'BODY') {
|
||||
docElm = rootNode.ownerDocument.documentElement;
|
||||
scrollX = rootNode.scrollLeft || docElm.scrollLeft;
|
||||
scrollY = rootNode.scrollTop || docElm.scrollTop;
|
||||
} else {
|
||||
rootRect = rootNode.getBoundingClientRect();
|
||||
scrollX = rootNode.scrollLeft - rootRect.left;
|
||||
scrollY = rootNode.scrollTop - rootRect.top;
|
||||
}
|
||||
|
||||
clientRect.left += scrollX;
|
||||
clientRect.right += scrollX;
|
||||
clientRect.top += scrollY;
|
||||
clientRect.bottom += scrollY;
|
||||
clientRect.width = 1;
|
||||
|
||||
margin = node.offsetWidth - node.clientWidth;
|
||||
|
||||
if (margin > 0) {
|
||||
if (before) {
|
||||
margin *= -1;
|
||||
}
|
||||
|
||||
clientRect.left += margin;
|
||||
clientRect.right += margin;
|
||||
}
|
||||
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
function trimInlineCaretContainers() {
|
||||
var contentEditableFalseNodes, node, sibling, i, data;
|
||||
|
||||
contentEditableFalseNodes = $('*[contentEditable=false]', rootNode);
|
||||
for (i = 0; i < contentEditableFalseNodes.length; i++) {
|
||||
node = contentEditableFalseNodes[i];
|
||||
|
||||
sibling = node.previousSibling;
|
||||
if (CaretContainer.endsWithCaretContainer(sibling)) {
|
||||
data = sibling.data;
|
||||
|
||||
if (data.length == 1) {
|
||||
sibling.parentNode.removeChild(sibling);
|
||||
} else {
|
||||
sibling.deleteData(data.length - 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
sibling = node.nextSibling;
|
||||
if (CaretContainer.startsWithCaretContainer(sibling)) {
|
||||
data = sibling.data;
|
||||
|
||||
if (data.length == 1) {
|
||||
sibling.parentNode.removeChild(sibling);
|
||||
} else {
|
||||
sibling.deleteData(0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function show(before, node) {
|
||||
var clientRect, rng, container;
|
||||
|
||||
hide();
|
||||
|
||||
if (isBlock(node)) {
|
||||
caretContainerNode = CaretContainer.insertBlock('p', node, before);
|
||||
clientRect = getAbsoluteClientRect(node, before);
|
||||
$(caretContainerNode).css('top', clientRect.top);
|
||||
|
||||
$lastVisualCaret = $('<div class="mce-visual-caret" data-mce-bogus="all"></div>').css(clientRect).appendTo(rootNode);
|
||||
|
||||
if (before) {
|
||||
$lastVisualCaret.addClass('mce-visual-caret-before');
|
||||
}
|
||||
|
||||
startBlink();
|
||||
|
||||
rng = node.ownerDocument.createRange();
|
||||
container = caretContainerNode.firstChild;
|
||||
rng.setStart(container, 0);
|
||||
rng.setEnd(container, 1);
|
||||
} else {
|
||||
caretContainerNode = CaretContainer.insertInline(node, before);
|
||||
rng = node.ownerDocument.createRange();
|
||||
|
||||
if (isContentEditableFalse(caretContainerNode.nextSibling)) {
|
||||
rng.setStart(caretContainerNode, 0);
|
||||
rng.setEnd(caretContainerNode, 0);
|
||||
} else {
|
||||
rng.setStart(caretContainerNode, 1);
|
||||
rng.setEnd(caretContainerNode, 1);
|
||||
}
|
||||
|
||||
return rng;
|
||||
}
|
||||
|
||||
return rng;
|
||||
}
|
||||
|
||||
function hide() {
|
||||
trimInlineCaretContainers();
|
||||
|
||||
if (caretContainerNode) {
|
||||
CaretContainer.remove(caretContainerNode);
|
||||
caretContainerNode = null;
|
||||
}
|
||||
|
||||
if ($lastVisualCaret) {
|
||||
$lastVisualCaret.remove();
|
||||
$lastVisualCaret = null;
|
||||
}
|
||||
|
||||
clearInterval(cursorInterval);
|
||||
}
|
||||
|
||||
function startBlink() {
|
||||
cursorInterval = Delay.setInterval(function() {
|
||||
$('div.mce-visual-caret', rootNode).toggleClass('mce-visual-caret-hidden');
|
||||
}, 500);
|
||||
}
|
||||
|
||||
function destroy() {
|
||||
Delay.clearInterval(cursorInterval);
|
||||
}
|
||||
|
||||
function getCss() {
|
||||
return (
|
||||
'.mce-visual-caret {' +
|
||||
'position: absolute;' +
|
||||
'background-color: black;' +
|
||||
'background-color: currentcolor;' +
|
||||
'}' +
|
||||
'.mce-visual-caret-hidden {' +
|
||||
'display: none;' +
|
||||
'}' +
|
||||
'*[data-mce-caret] {' +
|
||||
'position: absolute;' +
|
||||
'left: -1000px;' +
|
||||
'right: auto;' +
|
||||
'top: 0;' +
|
||||
'margin: 0;' +
|
||||
'padding: 0;' +
|
||||
'}'
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
show: show,
|
||||
hide: hide,
|
||||
getCss: getCss,
|
||||
destroy: destroy
|
||||
};
|
||||
};
|
||||
});
|
||||
135
js/tinymce/classes/caret/LineUtils.js
Normal file
135
js/tinymce/classes/caret/LineUtils.js
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* LineUtils.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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utility functions for working with lines.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.LineUtils
|
||||
*/
|
||||
define("tinymce/caret/LineUtils", [
|
||||
"tinymce/util/Fun",
|
||||
"tinymce/util/Arr",
|
||||
"tinymce/dom/NodeType",
|
||||
"tinymce/dom/Dimensions",
|
||||
"tinymce/geom/ClientRect",
|
||||
"tinymce/caret/CaretUtils",
|
||||
"tinymce/caret/CaretCandidate"
|
||||
], function(Fun, Arr, NodeType, Dimensions, ClientRect, CaretUtils, CaretCandidate) {
|
||||
var isContentEditableFalse = NodeType.isContentEditableFalse,
|
||||
findNode = CaretUtils.findNode,
|
||||
curry = Fun.curry;
|
||||
|
||||
function distanceToRectLeft(clientRect, clientX) {
|
||||
return Math.abs(clientRect.left - clientX);
|
||||
}
|
||||
|
||||
function distanceToRectRight(clientRect, clientX) {
|
||||
return Math.abs(clientRect.right - clientX);
|
||||
}
|
||||
|
||||
function findClosestClientRect(clientRects, clientX) {
|
||||
function isInside(clientX, clientRect) {
|
||||
return clientX >= clientRect.left && clientX <= clientRect.right;
|
||||
}
|
||||
|
||||
return Arr.reduce(clientRects, function(oldClientRect, clientRect) {
|
||||
var oldDistance, newDistance;
|
||||
|
||||
oldDistance = Math.min(distanceToRectLeft(oldClientRect, clientX), distanceToRectRight(oldClientRect, clientX));
|
||||
newDistance = Math.min(distanceToRectLeft(clientRect, clientX), distanceToRectRight(clientRect, clientX));
|
||||
|
||||
if (isInside(clientX, clientRect)) {
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
if (isInside(clientX, oldClientRect)) {
|
||||
return oldClientRect;
|
||||
}
|
||||
|
||||
// cE=false has higher priority
|
||||
if (newDistance == oldDistance && isContentEditableFalse(clientRect.node)) {
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
if (newDistance < oldDistance) {
|
||||
return clientRect;
|
||||
}
|
||||
|
||||
return oldClientRect;
|
||||
});
|
||||
}
|
||||
|
||||
function walkUntil(direction, rootNode, predicateFn, node) {
|
||||
while ((node = findNode(node, direction, CaretCandidate.isEditableCaretCandidate, rootNode))) {
|
||||
if (predicateFn(node)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findLineNodeRects(rootNode, targetNodeRect) {
|
||||
var clientRects = [];
|
||||
|
||||
function collect(checkPosFn, node) {
|
||||
var lineRects;
|
||||
|
||||
lineRects = Arr.filter(Dimensions.getClientRects(node), function(clientRect) {
|
||||
return !checkPosFn(clientRect, targetNodeRect);
|
||||
});
|
||||
|
||||
clientRects = clientRects.concat(lineRects);
|
||||
|
||||
return lineRects.length === 0;
|
||||
}
|
||||
|
||||
clientRects.push(targetNodeRect);
|
||||
walkUntil(-1, rootNode, curry(collect, ClientRect.isAbove), targetNodeRect.node);
|
||||
walkUntil(1, rootNode, curry(collect, ClientRect.isBelow), targetNodeRect.node);
|
||||
|
||||
return clientRects;
|
||||
}
|
||||
|
||||
function getContentEditableFalseChildren(rootNode) {
|
||||
return Arr.filter(Arr.toArray(rootNode.getElementsByTagName('*')), isContentEditableFalse);
|
||||
}
|
||||
|
||||
function caretInfo(clientRect, clientX) {
|
||||
return {
|
||||
node: clientRect.node,
|
||||
before: distanceToRectLeft(clientRect, clientX) < distanceToRectRight(clientRect, clientX)
|
||||
};
|
||||
}
|
||||
|
||||
function closestCaret(rootNode, clientX, clientY) {
|
||||
var contentEditableFalseNodeRects, closestNodeRect;
|
||||
|
||||
contentEditableFalseNodeRects = Dimensions.getClientRects(getContentEditableFalseChildren(rootNode));
|
||||
contentEditableFalseNodeRects = Arr.filter(contentEditableFalseNodeRects, function(clientRect) {
|
||||
return clientY >= clientRect.top && clientY <= clientRect.bottom;
|
||||
});
|
||||
|
||||
closestNodeRect = findClosestClientRect(contentEditableFalseNodeRects, clientX);
|
||||
if (closestNodeRect) {
|
||||
closestNodeRect = findClosestClientRect(findLineNodeRects(rootNode, closestNodeRect), clientX);
|
||||
if (closestNodeRect && isContentEditableFalse(closestNodeRect.node)) {
|
||||
return caretInfo(closestNodeRect, clientX);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
findClosestClientRect: findClosestClientRect,
|
||||
findLineNodeRects: findLineNodeRects,
|
||||
closestCaret: closestCaret
|
||||
};
|
||||
});
|
||||
166
js/tinymce/classes/caret/LineWalker.js
Normal file
166
js/tinymce/classes/caret/LineWalker.js
Normal file
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* LineWalker.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 lets you walk the document line by line
|
||||
* returing nodes and client rects for each line.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.caret.LineWalker
|
||||
*/
|
||||
define("tinymce/caret/LineWalker", [
|
||||
"tinymce/util/Fun",
|
||||
"tinymce/util/Arr",
|
||||
"tinymce/dom/Dimensions",
|
||||
"tinymce/caret/CaretCandidate",
|
||||
"tinymce/caret/CaretUtils",
|
||||
"tinymce/caret/CaretWalker",
|
||||
"tinymce/caret/CaretPosition",
|
||||
"tinymce/geom/ClientRect"
|
||||
], function(Fun, Arr, Dimensions, CaretCandidate, CaretUtils, CaretWalker, CaretPosition, ClientRect) {
|
||||
var curry = Fun.curry;
|
||||
|
||||
function findUntil(direction, rootNode, predicateFn, node) {
|
||||
while ((node = CaretUtils.findNode(node, direction, CaretCandidate.isEditableCaretCandidate, rootNode))) {
|
||||
if (predicateFn(node)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function walkUntil(direction, isAboveFn, isBeflowFn, rootNode, predicateFn, caretPosition) {
|
||||
var line = 0, node, result = [], targetClientRect;
|
||||
|
||||
function add(node) {
|
||||
var i, clientRect, clientRects;
|
||||
|
||||
clientRects = Dimensions.getClientRects(node);
|
||||
if (direction == -1) {
|
||||
clientRects = clientRects.reverse();
|
||||
}
|
||||
|
||||
for (i = 0; i < clientRects.length; i++) {
|
||||
clientRect = clientRects[i];
|
||||
if (isBeflowFn(clientRect, targetClientRect)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (result.length > 0 && isAboveFn(clientRect, Arr.last(result))) {
|
||||
line++;
|
||||
}
|
||||
|
||||
clientRect.line = line;
|
||||
|
||||
if (predicateFn(clientRect)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
result.push(clientRect);
|
||||
}
|
||||
}
|
||||
|
||||
targetClientRect = Arr.last(caretPosition.getClientRects());
|
||||
if (!targetClientRect) {
|
||||
return result;
|
||||
}
|
||||
|
||||
node = caretPosition.getNode();
|
||||
add(node);
|
||||
findUntil(direction, rootNode, add, node);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function aboveLineNumber(lineNumber, clientRect) {
|
||||
return clientRect.line > lineNumber;
|
||||
}
|
||||
|
||||
function isLine(lineNumber, clientRect) {
|
||||
return clientRect.line === lineNumber;
|
||||
}
|
||||
|
||||
var upUntil = curry(walkUntil, -1, ClientRect.isAbove, ClientRect.isBelow);
|
||||
var downUntil = curry(walkUntil, 1, ClientRect.isBelow, ClientRect.isAbove);
|
||||
|
||||
function positionsUntil(direction, rootNode, predicateFn, node) {
|
||||
var caretWalker = new CaretWalker(rootNode), walkFn, isBelowFn, isAboveFn,
|
||||
caretPosition, result = [], line = 0, clientRect, targetClientRect;
|
||||
|
||||
function getClientRect(caretPosition) {
|
||||
if (direction == 1) {
|
||||
return Arr.last(caretPosition.getClientRects());
|
||||
}
|
||||
|
||||
return Arr.last(caretPosition.getClientRects());
|
||||
}
|
||||
|
||||
if (direction == 1) {
|
||||
walkFn = caretWalker.next;
|
||||
isBelowFn = ClientRect.isBelow;
|
||||
isAboveFn = ClientRect.isAbove;
|
||||
caretPosition = CaretPosition.after(node);
|
||||
} else {
|
||||
walkFn = caretWalker.prev;
|
||||
isBelowFn = ClientRect.isAbove;
|
||||
isAboveFn = ClientRect.isBelow;
|
||||
caretPosition = CaretPosition.before(node);
|
||||
}
|
||||
|
||||
targetClientRect = getClientRect(caretPosition);
|
||||
|
||||
do {
|
||||
if (!caretPosition.isVisible()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
clientRect = getClientRect(caretPosition);
|
||||
|
||||
if (isAboveFn(clientRect, targetClientRect)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (result.length > 0 && isBelowFn(clientRect, Arr.last(result))) {
|
||||
line++;
|
||||
}
|
||||
|
||||
clientRect = ClientRect.clone(clientRect);
|
||||
clientRect.position = caretPosition;
|
||||
clientRect.line = line;
|
||||
|
||||
if (predicateFn(clientRect)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result.push(clientRect);
|
||||
} while ((caretPosition = walkFn(caretPosition)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
upUntil: upUntil,
|
||||
downUntil: downUntil,
|
||||
|
||||
/**
|
||||
* Find client rects with line and caret position until the predicate returns true.
|
||||
*
|
||||
* @method positionsUntil
|
||||
* @param {Number} direction Direction forward/backward 1/-1.
|
||||
* @param {DOMNode} rootNode Root node to walk within.
|
||||
* @param {function} predicateFn Gets the client rect as it's input.
|
||||
* @param {DOMNode} node Node to start walking from.
|
||||
* @return {Array} Array of client rects with line and position properties.
|
||||
*/
|
||||
positionsUntil: positionsUntil,
|
||||
|
||||
isAboveLine: curry(aboveLineNumber),
|
||||
isLine: curry(isLine)
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user