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];
24 var getElementForNode = function(node) {
26 var ptr = node.nodeType === Node.TEXT_NODE ? node.parent() : node;
27 while(!ptr.getData('canvasElement')) {
30 return ptr.getData('canvasElement');
33 var getElementForDetachedNode = function(node, originalParent) {
34 var ptr = originalParent;
36 return node.getData('canvasElement');
38 while(!ptr.getData('canvasElement')) {
41 return ptr.getData('canvasElement');
44 var getElementForTextNode = function(textNode) {
45 var parentElement = getElementForNode(textNode.parent()),
47 parentElement.children().some(function(child) {
48 if(child.wlxmlNode.sameNode(textNode)) {
57 nearestInDocumentOrder: nearestInDocumentOrder,
61 getElementForNode: getElementForNode,
62 getElementForDetachedNode: getElementForDetachedNode,
63 getElementForTextNode: getElementForTextNode