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, withParent) {
24 if(node.nodeType === Node.TEXT_NODE) {
25 return _getElementForRootTextNode(node, withParent);
27 return node.getData('canvasElement');
30 var _getElementForRootTextNode = function(textNode, withParent) {
31 var parentElement = getElementForNode(withParent || textNode.parent()),
33 parentElement.children().some(function(child) {
34 if(child.wlxmlNode.sameNode(textNode)) {
42 var getElementForNode = function(node, withParent) {
43 if(node.nodeType === Node.TEXT_NODE) {
44 return _getElementForTextNode(node, withParent);
46 while(!node.getData('canvasElement')) {
49 return node.getData('canvasElement');
52 var _getElementForTextNode = function(textNode, withParent) {
53 var parentElement = getElementForNode(withParent || textNode.parent()),
55 parentElement.children().some(function(child) {
56 if(child.wlxmlNode.sameNode(textNode)) {
64 var getElementForDetachedNode = function(node, originalParent) {
65 var ptr = originalParent;
67 return node.getData('canvasElement');
69 while(!ptr.getData('canvasElement')) {
72 return ptr.getData('canvasElement');
78 nearestInDocumentOrder: nearestInDocumentOrder,
82 getElementForNode: getElementForNode,
83 getElementForDetachedNode: getElementForDetachedNode,
84 getElementForElementRootNode: getElementForElementRootNode