8 var nearestInDocumentOrder = function(selector, direction, element) {
9 var parents = $(element).parents(),
10 parent = parents.length ? $(parents[parents.length-1]) : element;
12 var adj = parent.find(selector).filter(function() {
13 /*jshint bitwise: false*/
14 return this.compareDocumentPosition(element) & (direction === 'above' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING);
18 return adj[direction === 'above' ? adj.length-1 : 0];
23 var getElementForElementRootNode = function(node) {
24 return node.getData('canvasElement');
27 var getElementForNode = function(node) {
28 while(!node.getData('canvasElement')) {
31 return node.getData('canvasElement');
34 var getElementForDetachedNode = function(node, originalParent) {
35 var ptr = originalParent;
37 return node.getData('canvasElement');
39 while(!ptr.getData('canvasElement')) {
42 return ptr.getData('canvasElement');
45 var caretPositionFromPoint = function(x, y) {
46 /* globals document */
47 var range, textNode, offset;
48 if(document.caretPositionFromPoint) {
49 range = document.caretPositionFromPoint(x, y);
50 textNode = range.offsetNode;
51 offset = range.offset;
52 } else if(document.caretRangeFromPoint) {
53 range = document.caretRangeFromPoint(x, y);
54 textNode = range.startContainer;
55 offset = range.startOffset;
65 nearestInDocumentOrder: nearestInDocumentOrder,
69 getElementForNode: getElementForNode,
70 getElementForDetachedNode: getElementForDetachedNode,
71 getElementForElementRootNode: getElementForElementRootNode,
72 caretPositionFromPoint: caretPositionFromPoint