/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/core/range",function(o,u,I,m,t){function J(a){var b=a.nodeType!=g.TEXT_NODE&&g.nodeName(a)in y.$removeEmpty,c=a.nodeType==g.TEXT_NODE&&!o.trim(a.nodeValue),a=!!a.parentNode.getAttribute("_ke_bookmark");return b||c||a}function C(a){return!D(a)&&!z(a)}function E(a){var b=p;return function(c){if(z(c))return h;if(c.nodeType==g.TEXT_NODE){if(o.trim(c.nodeValue).length)return p}else if(c.nodeType==g.ELEMENT_NODE&&(c=g.nodeName(c),!K[c]))if(!a&&!A.ie&&"br"==c&&!b)b=h;else return p;return h}}
function B(a,b){var c=a.startContainer,d=a.endContainer,f=a.startOffset,i=a.endOffset,e,G=p,l=p,q=void 0,n=a.document,o;0<b&&(q=n.createDocumentFragment());if(a.collapsed)return q;a.optimizeBookmark();d[0].nodeType==g.TEXT_NODE?(l=h,d=d._4e_splitText(i)):0<d[0].childNodes.length&&(i>=d[0].childNodes.length?(d=new v(d[0].appendChild(n.createTextNode(""))),o=h):d=new v(d[0].childNodes[i]));c[0].nodeType==g.TEXT_NODE?(G=h,c._4e_splitText(f)):f?f>=c[0].childNodes.length?(c=new v(c[0].appendChild(n.createTextNode(""))),
e=h):c=new v(c[0].childNodes[f].previousSibling):(e=new v(n.createTextNode("")),c.prepend(e),c=e,e=h);var m=c._4e_parents(),j=d._4e_parents();m.each(function(a,b){m[b]=a});j.each(function(a,b){j[b]=a});for(var s,r,n=0;n<m.length&&!(s=m[n],r=j[n],!s.equals(r));n++);for(var f=q,k,t,u=n;u<m.length;u++){k=m[u];i=0<b&&!k.equals(c)?f.appendChild(k.clone()[0]):null;k=k[0].nextSibling;t=j[u];for(var w=d[0],x=t&&t[0];k&&!(x==k||w==k);)t=k.nextSibling,2==b?f.appendChild(k.cloneNode(h)):(g._4e_remove(k),1==
b&&f.appendChild(k)),k=t;i&&(f=i)}for(f=q;n<j.length;n++){k=j[n];i=0<b&&!k.equals(d)?f.appendChild(k.clone()[0]):null;if(!m[n]||!k._4e_sameLevel(m[n]))for(k=k[0].previousSibling;k;)t=k.previousSibling,2==b?f.insertBefore(k.cloneNode(h),f.firstChild):(g._4e_remove(k),1==b&&f.insertBefore(k,f.firstChild)),k=t;i&&(f=i)}if(2==b){if(G&&(s=c[0],s.nodeType==g.TEXT_NODE&&s.nextSibling&&s.nextSibling.nodeType==g.TEXT_NODE&&(s.data+=s.nextSibling.data,s.parentNode.removeChild(s.nextSibling))),l)l=d[0],l.nodeType==
g.TEXT_NODE&&l.previousSibling&&l.previousSibling.nodeType==g.TEXT_NODE&&(l.previousSibling.data+=l.data,l.parentNode.removeChild(l))}else{if(s&&r&&(!c._4e_sameLevel(s)||!d._4e_sameLevel(r)))l=s._4e_index(),e&&s._4e_sameLevel(c)&&l--,a.setStart(s.parent(),l+1);a.collapse(h)}e&&c.remove();o&&d.remove();return q}function w(a){a.collapsed=a.startContainer&&a.endContainer&&a.startContainer[0]==a.endContainer[0]&&a.startOffset==a.endOffset}function x(a){this.endOffset=this.endContainer=this.startOffset=
this.startContainer=j;this.collapsed=h;this.document=a}u.RANGE={POSITION_AFTER_START:1,POSITION_BEFORE_END:2,POSITION_BEFORE_START:3,POSITION_AFTER_END:4,ENLARGE_ELEMENT:1,ENLARGE_BLOCK_CONTENTS:2,ENLARGE_LIST_ITEM_CONTENTS:3,START:1,END:2,SHRINK_ELEMENT:1,SHRINK_TEXT:2};var h=!0,p=!1,j=null,e=u.RANGE,L=u.POSITION,g=o.DOM,A=o.UA,y=u.XHTML_DTD,v=o.Node,r=v.all,H={area:1,base:1,br:1,col:1,hr:1,img:1,input:1,link:1,meta:1,param:1},D=new m.whitespaces,z=new m.bookmark,M=m.whitespaces(h),N=m.bookmark(!1,
!0),K={abbr:1,acronym:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,"var":1};o.augment(x,{toString:function(){var a=[],b=this.startContainer[0],c=this.endContainer[0];a.push((b.id||b.nodeName)+":"+this.startOffset);a.push((c.id||c.nodeName)+":"+this.endOffset);return a.join("<br/>")},optimize:function(){var a=this.startContainer,b=this.startOffset;a[0].nodeType!=g.ELEMENT_NODE&&(b?b>=a[0].nodeValue.length&&
this.setStartAfter(a):this.setStartBefore(a));a=this.endContainer;b=this.endOffset;a[0].nodeType!=g.ELEMENT_NODE&&(b?b>=a[0].nodeValue.length&&this.setEndAfter(a):this.setEndBefore(a))},setStartAfter:function(a){this.setStart(a.parent(),a._4e_index()+1)},setStartBefore:function(a){this.setStart(a.parent(),a._4e_index())},setEndAfter:function(a){this.setEnd(a.parent(),a._4e_index()+1)},setEndBefore:function(a){this.setEnd(a.parent(),a._4e_index())},optimizeBookmark:function(){var a=this.startContainer,
b=this.endContainer;a&&"span"==a.nodeName()&&a.attr("_ke_bookmark")&&this.setStartBefore(a);b&&"span"==b.nodeName()&&b.attr("_ke_bookmark")&&this.setEndAfter(b)},setStart:function(a,b){a[0].nodeType==g.ELEMENT_NODE&&H[a.nodeName()]&&(a=a.parent(),b=a._4e_index());this.startContainer=a;this.startOffset=b;this.endContainer||(this.endContainer=a,this.endOffset=b);w(this)},setEnd:function(a,b){a[0].nodeType==g.ELEMENT_NODE&&H[a.nodeName()]&&(a=a.parent(),b=a._4e_index()+1);this.endContainer=a;this.endOffset=
b;this.startContainer||(this.startContainer=a,this.startOffset=b);w(this)},setStartAt:function(a,b){switch(b){case e.POSITION_AFTER_START:this.setStart(a,0);break;case e.POSITION_BEFORE_END:a[0].nodeType==g.TEXT_NODE?this.setStart(a,a[0].nodeValue.length):this.setStart(a,a[0].childNodes.length);break;case e.POSITION_BEFORE_START:this.setStartBefore(a);break;case e.POSITION_AFTER_END:this.setStartAfter(a)}w(this)},setEndAt:function(a,b){switch(b){case e.POSITION_AFTER_START:this.setEnd(a,0);break;
case e.POSITION_BEFORE_END:a[0].nodeType==g.TEXT_NODE?this.setEnd(a,a[0].nodeValue.length):this.setEnd(a,a[0].childNodes.length);break;case e.POSITION_BEFORE_START:this.setEndBefore(a);break;case e.POSITION_AFTER_END:this.setEndAfter(a)}w(this)},cloneContents:function(){return B(this,2)},deleteContents:function(){return B(this,0)},extractContents:function(){return B(this,1)},collapse:function(a){a?(this.endContainer=this.startContainer,this.endOffset=this.startOffset):(this.startContainer=this.endContainer,
this.startOffset=this.endOffset);this.collapsed=h},clone:function(){var a=new x(this.document);a.startContainer=this.startContainer;a.startOffset=this.startOffset;a.endContainer=this.endContainer;a.endOffset=this.endOffset;a.collapsed=this.collapsed;return a},getEnclosedNode:function(){var a=this.clone();a.optimize();if(a.startContainer[0].nodeType!=g.ELEMENT_NODE||a.endContainer[0].nodeType!=g.ELEMENT_NODE)return j;var b=new m(a);b.evaluator=function(a){return M(a)&&N(a)};a=b.next();b.reset();b=
b.previous();return a&&a.equals(b)?a:j},shrink:function(a,b){if(!this.collapsed){var a=a||e.SHRINK_TEXT,c=this.clone(),d=this.startContainer,f=this.endContainer,i=this.startOffset,F=this.endOffset,j=h,l,q,n=h;d&&d[0].nodeType==g.TEXT_NODE&&(i?i>=d[0].nodeValue.length?c.setStartAfter(d):(c.setStartBefore(d),j=p):c.setStartBefore(d));f&&f[0].nodeType==g.TEXT_NODE&&(F?F>=f[0].nodeValue.length?c.setEndAfter(f):(c.setEndAfter(f),n=p):c.setEndBefore(f));if(j||n)q=new m(c),q.evaluator=function(b){return b.nodeType==
(a==e.SHRINK_ELEMENT?g.ELEMENT_NODE:g.TEXT_NODE)},q.guard=function(b,c){if(a==e.SHRINK_ELEMENT&&b.nodeType==g.TEXT_NODE||c&&b==l)return p;!c&&b.nodeType==g.ELEMENT_NODE&&(l=b);return h};j&&(c=q[a==e.SHRINK_ELEMENT?"lastForward":"next"]())&&this.setStartAt(c,b?e.POSITION_AFTER_START:e.POSITION_BEFORE_START);n&&(q.reset(),(q=q[a==e.SHRINK_ELEMENT?"lastBackward":"previous"]())&&this.setEndAt(q,b?e.POSITION_BEFORE_END:e.POSITION_AFTER_END));return j||n}},createBookmark2:function(a){var b=this.startContainer,
c=this.endContainer,d=this.startOffset,f=this.endOffset,i,e;if(!b||!c)return{start:0,end:0};if(a){if(b[0].nodeType==g.ELEMENT_NODE&&(i=new v(b[0].childNodes[d]))&&i[0]&&i[0].nodeType==g.TEXT_NODE&&0<d&&i[0].previousSibling.nodeType==g.TEXT_NODE)b=i,d=0;for(;b[0].nodeType==g.TEXT_NODE&&(e=b.prev(void 0,1))&&e[0].nodeType==g.TEXT_NODE;)b=e,d+=e[0].nodeValue.length;if(!this.collapsed){if(c[0].nodeType==g.ELEMENT_NODE&&(i=new v(c[0].childNodes[f]))&&i[0]&&i[0].nodeType==g.TEXT_NODE&&0<f&&i[0].previousSibling.nodeType==
g.TEXT_NODE)c=i,f=0;for(;c[0].nodeType==g.TEXT_NODE&&(e=c.prev(void 0,1))&&e[0].nodeType==g.TEXT_NODE;)c=e,f+=e[0].nodeValue.length}}return{start:b._4e_address(a),end:this.collapsed?j:c._4e_address(a),startOffset:d,endOffset:f,normalized:a,is2:h}},createBookmark:function(a){var b,c,d,f,i=this.collapsed;b=new v("<span>",j,this.document);b.attr("_ke_bookmark",1);b.css("display","none");b.html("&nbsp;");a&&(d=o.guid("ke_bm_"),b.attr("id",d+"S"));i||(c=b.clone(),c.html("&nbsp;"),a&&c.attr("id",d+"E"),
f=this.clone(),f.collapse(),f.insertNode(c));f=this.clone();f.collapse(h);f.insertNode(b);c?(this.setStartAfter(b),this.setEndBefore(c)):this.moveToPosition(b,e.POSITION_AFTER_END);return{startNode:a?d+"S":b,endNode:a?d+"E":c,serializable:a,collapsed:i}},moveToPosition:function(a,b){this.setStartAt(a,b);this.collapse(h)},trim:function(a,b){var c=this.startContainer,d=this.startOffset,f=this.collapsed;if((!a||f)&&c[0]&&c[0].nodeType==g.TEXT_NODE){if(d)if(d>=c[0].nodeValue.length)d=c._4e_index()+1,
c=c.parent();else{var e=c._4e_splitText(d),d=c._4e_index()+1,c=c.parent();g.equals(this.startContainer,this.endContainer)?this.setEnd(e,this.endOffset-this.startOffset):g.equals(c,this.endContainer)&&(this.endOffset+=1)}else d=c._4e_index(),c=c.parent();this.setStart(c,d);if(f){this.collapse(h);return}}c=this.endContainer;d=this.endOffset;!b&&!f&&c[0]&&c[0].nodeType==g.TEXT_NODE&&(d?(d>=c[0].nodeValue.length||c._4e_splitText(d),d=c._4e_index()+1):d=c._4e_index(),c=c.parent(),this.setEnd(c,d))},insertNode:function(a){this.optimizeBookmark();
this.trim(p,h);var b=this.startContainer;b[0].insertBefore(a[0],b[0].childNodes[this.startOffset]||null);b[0]==this.endContainer[0]&&this.endOffset++;this.setStartBefore(a)},moveToBookmark:function(a){var b=r(this.document);if(a.is2){var c=b._4e_getByAddress(a.start,a.normalized),d=a.startOffset,b=a.end&&b._4e_getByAddress(a.end,a.normalized),a=a.endOffset;this.setStart(c,d);b?this.setEnd(b,a):this.collapse(h)}else c=(d=a.serializable)?o.one("#"+a.startNode,b):a.startNode,a=d?o.one("#"+a.endNode,
b):a.endNode,this.setStartBefore(c),c._4e_remove(),a&&a[0]?(this.setEndBefore(a),a._4e_remove()):this.collapse(h)},getCommonAncestor:function(a,b){var c=this.startContainer,d=this.endContainer,c=c[0]==d[0]?a&&c[0].nodeType==g.ELEMENT_NODE&&this.startOffset==this.endOffset-1?new v(c[0].childNodes[this.startOffset]):c:c._4e_commonAncestor(d);return b&&c[0].nodeType==g.TEXT_NODE?c.parent():c},enlarge:function(){function a(a,c,d,f){var e=a[c?"startContainer":"endContainer"],h,j=c?0:1,l=0,q=c?"previousSibling":
"nextSibling";h=a[c?"startOffset":"endOffset"];if(e[0].nodeType==g.TEXT_NODE){if(c){if(h)return}else if(h<e[0].nodeValue.length)return;h=e[0][q];e=e[0].parentNode}else h=e[0].childNodes[h+(c?-1:1)]||null,e=e[0];for(;e;){for(;h;)if(D(h)||z(h))h=h[q];else break;if(h){if(!l)a[c?"setStartAfter":"setEndBefore"](r(h));break}e=r(e);if("body"==e.nodeName())break;if(l||e.equals(f))d[j]=e,l=1;else a[c?"setStartBefore":"setEndAfter"](e);h=e[0][q];e=e[0].parentNode}}return function(b){switch(b){case e.ENLARGE_ELEMENT:if(this.collapsed)break;
var b=this.getCommonAncestor(),c=[];a(this,1,c,b);a(this,0,c,b);c[0]&&c[1]&&(b=c[0].contains(c[1])?c[1]:c[0],this.setStartBefore(b),this.setEndAfter(b));break;case e.ENLARGE_BLOCK_CONTENTS:case e.ENLARGE_LIST_ITEM_CONTENTS:var d=new x(this.document),c=new v(this.document.body);d.setStartAt(c,e.POSITION_AFTER_START);d.setEnd(this.startContainer,this.startOffset);var d=new m(d),f,h,p=m.blockBoundary(b==e.ENLARGE_LIST_ITEM_CONTENTS?{br:1}:j),o=function(a){var b=p(a);b||(f=r(a));return b},l=function(a){var b=
o(a);!b&&"br"==g.nodeName(a)&&(h=r(a));return b};d.guard=o;d=d.lastBackward();f=f||c;this.setStartAt(f,"br"!=f.nodeName()&&(!d&&this.checkStartOfBlock()||d&&f.contains(d))?e.POSITION_AFTER_START:e.POSITION_AFTER_END);d=this.clone();d.collapse();d.setEndAt(c,e.POSITION_BEFORE_END);d=new m(d);d.guard=b==e.ENLARGE_LIST_ITEM_CONTENTS?l:o;f=j;d=d.lastForward();f=f||c;this.setEndAt(f,!d&&this.checkEndOfBlock()||d&&f.contains(d)?e.POSITION_BEFORE_END:e.POSITION_BEFORE_START);h&&this.setEndAfter(h)}}}(),
checkStartOfBlock:function(){var a=this.startContainer,b=this.startOffset;if(b&&a[0].nodeType==g.TEXT_NODE&&o.trim(a[0].nodeValue.substring(0,b)).length)return p;this.trim();a=new t(this.startContainer);b=this.clone();b.collapse(h);b.setStartAt(a.block||a.blockLimit,e.POSITION_AFTER_START);a=new m(b);a.evaluator=E(h);return a.checkBackward()},checkEndOfBlock:function(){var a=this.endContainer,b=this.endOffset;if(a[0].nodeType==g.TEXT_NODE&&o.trim(a[0].nodeValue.substring(b)).length)return p;this.trim();
a=new t(this.endContainer);b=this.clone();b.collapse(p);b.setEndAt(a.block||a.blockLimit,e.POSITION_BEFORE_END);a=new m(b);a.evaluator=E(p);return a.checkForward()},checkBoundaryOfElement:function(a,b){var c=this.clone();c[b==e.START?"setStartAt":"setEndAt"](a,b==e.START?e.POSITION_AFTER_START:e.POSITION_BEFORE_END);c=new m(c);c.evaluator=J;return c[b==e.START?"checkBackward":"checkForward"]()},getBoundaryNodes:function(){var a=this.startContainer,b=this.endContainer,c=this.startOffset,d=this.endOffset,
e;if(a[0].nodeType==g.ELEMENT_NODE)if(e=a[0].childNodes.length,e>c)a=r(a[0].childNodes[c]);else if(0==e)a=a._4e_previousSourceNode();else{for(a=a[0];a.lastChild;)a=a.lastChild;a=r(a);a=a._4e_nextSourceNode()||a}if(b[0].nodeType==g.ELEMENT_NODE)if(e=b[0].childNodes.length,e>d)b=r(b[0].childNodes[d])._4e_previousSourceNode(h);else if(0==e)b=b._4e_previousSourceNode();else{for(b=b[0];b.lastChild;)b=b.lastChild;b=r(b)}a._4e_position(b)&L.POSITION_FOLLOWING&&(a=b);return{startNode:a,endNode:b}},fixBlock:function(a,
b){var c=this.createBookmark(),d=r(this.document.createElement(b));this.collapse(a);this.enlarge(e.ENLARGE_BLOCK_CONTENTS);d[0].appendChild(this.extractContents());d._4e_trim();A.ie||d._4e_appendBogus();this.insertNode(d);this.moveToBookmark(c);return d},splitBlock:function(a){var b=new t(this.startContainer),c=new t(this.endContainer),d=b.block,f=c.block,g=j;if(!b.blockLimit.equals(c.blockLimit))return j;"br"!=a&&(d||(d=this.fixBlock(h,a),f=(new t(this.endContainer)).block),f||(f=this.fixBlock(p,
a)));a=d&&this.checkStartOfBlock();b=f&&this.checkEndOfBlock();this.deleteContents();d&&d[0]==f[0]&&(b?(g=new t(this.startContainer),this.moveToPosition(f,e.POSITION_AFTER_END),f=j):a?(g=new t(this.startContainer),this.moveToPosition(d,e.POSITION_BEFORE_START),d=j):(f=this.splitElement(d),!A.ie&&!o.inArray(d.nodeName(),["ul","ol"])&&d._4e_appendBogus()));return{previousBlock:d,nextBlock:f,wasStartOfBlock:a,wasEndOfBlock:b,elementPath:g}},splitElement:function(a){if(!this.collapsed)return j;this.setEndAt(a,
e.POSITION_BEFORE_END);var b=this.extractContents(),c=a.clone(p);c[0].appendChild(b);c.insertAfter(a);this.moveToPosition(a,e.POSITION_AFTER_END);return c},moveToElementEditablePosition:function(a,b){for(var c=0;a;){if(a[0].nodeType==g.TEXT_NODE){this.moveToPosition(a,b?e.POSITION_AFTER_END:e.POSITION_BEFORE_START);c=1;break}a[0].nodeType==g.ELEMENT_NODE&&a._4e_isEditable()&&(this.moveToPosition(a,b?e.POSITION_BEFORE_END:e.POSITION_AFTER_START),c=1);var d=a,f=c,h=void 0;d[0].nodeType==g.ELEMENT_NODE&&
d._4e_isEditable()&&(h=d[b?"last":"first"](C,1));!f&&!h&&(h=d[b?"prev":"next"](C,1));a=h}return!!c},selectNodeContents:function(a){var b=a[0];this.setStart(a,0);this.setEnd(a,b.nodeType==g.TEXT_NODE?b.nodeValue.length:b.childNodes.length)},insertNodeByDtd:function(a){var b,c,d,e=a.nodeName();b=y.$block[e];this.deleteContents();if(b){for(b=this.getCommonAncestor(p,h);(c=y[b.nodeName()])&&(!c||!c[e]);){var g=b.parent();this.checkStartOfBlock()&&this.checkEndOfBlock()?(this.setStartBefore(b),this.collapse(h),
b.remove()):d=b;b=g}d&&this.splitElement(d)}this.insertNode(a)}});I.injectDom({_4e_breakParent:function(a,b){var b=r(b),a=r(a),c,d=new u.Range(a[0].ownerDocument);d.setStartAfter(a);d.setEndAfter(b);c=d.extractContents();d.insertNode(a.remove());a.after(c)}});return u.Range=x},{requires:["./base","./utils","./walker","./elementPath","./dom"]});
