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 return this.compareDocumentPosition(element) & (direction === 'above' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING);
17 return adj[direction === 'above' ? adj.length-1 : 0];
22 var findCanvasElement = function(node) {
23 if(node.nodeType === Node.ELEMENT_NODE) {
24 return node.getData('canvasElement');
26 if(node.nodeType === Node.TEXT_NODE) {
27 return findCanvasElementInParent(node, node.parent())
32 * We take child and its parent as arguments separatly to
33 * handle situation where child was removed from WLXMLDocument
34 * and it lost reference to its parent (but we may still have it on canvas).
36 var findCanvasElementInParent = function(wlxmlChildNode, wlxmlParentNode) {
37 var parentElement = findCanvasElement(wlxmlParentNode),
39 parentElement.children().forEach(function(child) {
40 if(child.data('wlxmlNode').sameNode(wlxmlChildNode))
47 nearestInDocumentOrder: nearestInDocumentOrder,
48 findCanvasElement: findCanvasElement,
49 findCanvasElementInParent: findCanvasElementInParent,