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, childOf) {
24 if(node.nodeType === Node.ELEMENT_NODE) {
25 return node.getData('canvasElement');
27 if(node.nodeType === Node.TEXT_NODE) {
28 return findCanvasElementInParent(node, childOf || 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, toret;
40 if(wlxmlParentNode === null) {
41 toret = wlxmlChildNode.getData('canvasElement');
43 throw new Error('This should never happen: root canvas element doesn\'t render root document node!');
46 parentElement = findCanvasElement(wlxmlParentNode);
47 parentElement.children().forEach(function(child) {
48 if(child.data('wlxmlNode').sameNode(wlxmlChildNode)) { // czemu tu, przy drugim undo child nie mial data?
57 nearestInDocumentOrder: nearestInDocumentOrder,
58 findCanvasElement: findCanvasElement,
59 findCanvasElementInParent: findCanvasElementInParent,