/** * Position module for Rangy. * Extensions to Range and Selection objects to provide access to pixel positions relative to the viewport or document. * * Part of Rangy, a cross-browser JavaScript range and selection library * http://code.google.com/p/rangy/ * * Depends on Rangy core. * * Copyright 2013, Tim Down * Licensed under the MIT license. * Version: 1.3alpha.780M * Build date: 17 May 2013 */ rangy.createModule("Position",["WrappedSelection"],function(a,b){function j(a){var b=0,d=0;if(typeof a.pageXOffset==c&&typeof a.pageYOffset==c)b=a.pageXOffset,d=a.pageYOffset;else{var e=a.document,f=e.documentElement,h=e.compatMode,i=typeof h=="string"&&h.indexOf("CSS")>=0&&f?f:g.getBody(e);if(i&&typeof i.scrollLeft==c&&typeof i.scrollTop==c)try{b=i.scrollLeft,d=i.scrollTop}catch(j){}}return{x:b,y:d}}function k(a,b){b=b.toLowerCase();while(a){if(a.nodeType==1&&a.tagName.toLowerCase()==b)return a;a=a.parentNode}return null}function l(a,b,c,d){this.top=a,this.right=b,this.bottom=c,this.left=d,this.width=b-d,this.height=c-a}function m(a,b,c){return new l(a.top+c,a.right+b,a.bottom+c,a.left+b)}function n(a,b){var d=0,e=0,f=b.documentElement,h=g.getBody(b),i=f.clientWidth===0&&typeof h.clientTop==c?h:f,j=i.clientLeft,k=i.clientTop;return j&&(d=-j),k&&(e=-k),m(a,d,e)}function o(a){var b=[],c=[],d=[],e=[];for(var f=0,g=a.length,h;f0?b[b.length-1]:null}function t(a,b,c){return console.log("pointIsInOrAboveRect",a,b,Math.floor(c.top),Math.floor(c.right),Math.floor(c.bottom),Math.floor(c.left)),b=c.left&&a<=c.right}function u(b,c,d,e){var f=b.elementFromPoint(c,d);console.log("elementFromPoint is ",f);var h=a.createRange(b);h.selectNodeContents(f),h.collapse(!0);var j=f.firstChild,k,l,m;if(!j)j=f.parentNode,k=g.getNodeIndex(f),e||++k;else{a:while(j){console.log(j);if(j.nodeType==3)for(k=0,m=j.length;k<=m;++k){h.setEnd(j,k),l=s(h);if(l&&t(c,d,l)){l.right-c>c-l.left&&--k;break a}}else{h.setEndAfter(j),l=s(h);if(l&&t(c,d,l)){k=g.getNodeIndex(j),j=f.parentNode,e||++k;break}}j=j.nextSibling}j||(j=f,k=f.childNodes.length)}return new i(j,k)}function v(c){if(a.features.implementsTextRange)return p;if(typeof c.caretPositionFromPoint!=d)return q;if(typeof c.caretRangeFromPoint!=d)return r;if(typeof c.elementFromPoint!=d&&A)return u;throw b.createError("createCaretPositionFromPointGetter(): Browser does not provide a recognised method to create a selection from pixel coordinates")}function w(c,d,e,f,h){h=g.getContentDocument(h,b,"createRangeFromPoints");var i=v(h),j=i(h,c,d,!1),k=i(h,e,f,!0);console.log(j.node,j.offset,k.node,k.offset);var l=a.createRange(h);return l.setStartAndEnd(j.node,j.offset,k.node,k.offset),l}function x(a,b,c,d,e){var f,g,h,i,j=d1&&d.sort(K),a?d[0]["getStart"+c+"Pos"]():d[d.length-1]["getEnd"+c+"Pos"]()}}var c="number",d="undefined",e=a.WrappedRange,f=a.WrappedTextRange,g=a.dom,h=a.util,i=g.DomPosition,y=document.createElement("span"),z=h.isHostMethod(y,"getBoundingClientRect");y=null;var A=!1,B=!1;if(a.features.implementsDomRange){var C=a.createNativeRange();A=h.isHostMethod(C,"getClientRects"),B=h.isHostMethod(C,"getBoundingClientRect"),C.detach()}h.extend(a.features,{rangeSupportsGetBoundingClientRect:B,rangeSupportsGetClientRects:A,elementSupportsGetBoundingClientRect:z});var D=function(a){return function(){var b=this.cloneRange();b.collapse(a);var c=b.getBoundingClientRect();return{x:c[a?"left":"right"],y:c[a?"top":"bottom"]}}},E=a.rangePrototype;if(a.features.implementsTextRange&&z)E.getBoundingClientRect=function(){var a=f.rangeToTextRange(this),b=this.getNodes([1],function(a){return/^t[dh]$/i.test(a.tagName)}),c,d=[];if(b.length>0){var e=k(this.startContainer,"table");for(var h=0,i,j,l,m,p;i=b[h];++h){l=k(i,"table");if(!e||l!=e)m=this.cloneRange(),e&&m.setStartAfter(e),m.setEndBefore(l),d.push(f.rangeToTextRange(m).getBoundingClientRect());this.containsNode(i)?d.push(i.getBoundingClientRect()):(j=a.duplicate(),j.moveToElementText(i),j.compareEndPoints("StartToStart",a)==-1?j.setEndPoint("StartToStart",a):j.compareEndPoints("EndToEnd",a)==1&&j.setEndPoint("EndToEnd",a),d.push(j.getBoundingClientRect())),e=l}var q=k(this.endContainer,"table");!q&&e&&(m=this.cloneRange(),m.setStartAfter(e),d.push(f.rangeToTextRange(m).getBoundingClientRect())),c=o(d)}else c=a.getBoundingClientRect();return n(c,g.getDocument(this.startContainer))};else if(a.features.implementsDomRange){var F=function(a){return a instanceof e?a:new e(a)};if(B){E.getBoundingClientRect=function(){var a=F(this).nativeRange,b=a.getBoundingClientRect()||a.getClientRects()[0];return n(b,g.getDocument(this.startContainer))};if(A){var G=function(a,b){var c=a.childNodes};D=function(a){return function(){var c,d=F(this).nativeRange,e=d.getClientRects();if(e.length==0&&z){!a,console.log(d,d.getClientRects(),d.getBoundingClientRect());if(this.collapsed&&this.startContainer.nodeType==1&&this.startOffset0)return a?(c=e[0],{x:c.left,y:c.top}):(c=e[e.length-1],{x:c.right,y:c.bottom});throw b.createError("Cannot get position for range "+this.inspect())}}}}else{var H=z?function(a){return n(a.getBoundingClientRect(),g.getDocument(a))}:function(a){var b=0,c=0,d=a,e=a.offsetWidth,f=a.offsetHeight;while(d)b+=d.offsetLeft,c+=d.offsetTop,d=d.offsetParent;return n(new l(c,b+e,c+f,b),g.getDocument(a))},I=function(a){var b;a.splitBoundaries();var c=document.createElement("span");if(a.collapsed)a.insertNode(c),b=H(c),c.parentNode.removeChild(c);else{var d=a.cloneRange();d.collapse(!0),d.insertNode(c);var e=H(c);c.parentNode.removeChild(c),d.collapseToPoint(a.endContainer,a.endOffset),d.insertNode(c);var f=H(c);c.parentNode.removeChild(c);var g=[e,f],h=a.getNodes([1],function(b){return a.containsNode(b)});for(var i=0,j=h.length;i