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, withParent) {
25 if(node.nodeType === Node.TEXT_NODE) {
26 return _getElementForTextNode(node, withParent);
28 while(!node.getData('canvasElement')) {
31 return node.getData('canvasElement');
34 var _getElementForTextNode = function(textNode, withParent) {
35 var parentElement = getElementForNode(withParent || textNode.parent()),
37 parentElement.children().some(function(child) {
38 if(child.wlxmlNode.sameNode(textNode)) {
46 var getElementForDetachedNode = function(node, originalParent) {
47 var ptr = originalParent;
49 return node.getData('canvasElement');
51 while(!ptr.getData('canvasElement')) {
54 return ptr.getData('canvasElement');
60 nearestInDocumentOrder: nearestInDocumentOrder,
64 getElementForNode: getElementForNode,
65 getElementForDetachedNode: getElementForDetachedNode