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.wlxmlNode.sameNode(wlxmlChildNode)) { // czemu tu, przy drugim undo child nie mial data?
56 var getElementForNode = function(node) {
58 var ptr = node.nodeType === Node.TEXT_NODE ? node.parent() : node;
59 while(!ptr.getData('canvasElement')) {
62 return ptr.getData('canvasElement');
65 var getElementForDetachedNode = function(node, originalParent) {
66 var ptr = originalParent;
68 return node.getData('canvasElement');
70 while(!ptr.getData('canvasElement')) {
73 return ptr.getData('canvasElement');
77 nearestInDocumentOrder: nearestInDocumentOrder,
78 findCanvasElement: findCanvasElement,
79 findCanvasElementInParent: findCanvasElementInParent,
83 getElementForNode: getElementForNode,
84 getElementForDetachedNode: getElementForDetachedNode