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 findCanvasElement = function(node) {
24 if(node.nodeType === Node.ELEMENT_NODE) {
25 return node.getData('canvasElement');
27 if(node.nodeType === Node.TEXT_NODE) {
28 return findCanvasElementInParent(node, node.parent());
33 * We take child and its parent as arguments separatly to
34 * handle situation where child was removed from WLXMLDocument
35 * and it lost reference to its parent (but we may still have it on canvas).
37 var findCanvasElementInParent = function(wlxmlChildNode, wlxmlParentNode) {
38 var parentElement = findCanvasElement(wlxmlParentNode),
40 parentElement.children().forEach(function(child) {
41 if(child.data('wlxmlNode').sameNode(wlxmlChildNode)) {
49 nearestInDocumentOrder: nearestInDocumentOrder,
50 findCanvasElement: findCanvasElement,
51 findCanvasElementInParent: findCanvasElementInParent,