/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/core/selection",function(o){function r(b){this.document=b;this._={cache:{}};if(j)try{var a=this.getNative().createRange();if(!a||a.item&&a.item(0).ownerDocument!=b||a.parentElement&&a.parentElement().ownerDocument!=b)this.isInvalid=l}catch(d){this.isInvalid=l}}function t(b){b=new r(b);return!b||b.isInvalid?u:b}var m=o.Editor;m.SELECTION={SELECTION_NONE:1,SELECTION_TEXT:2,SELECTION_ELEMENT:3};var l=!0,u=null,n=o.UA,g=o.DOM,i=o.Node,k=m.SELECTION,s=m.RANGE,j=n.ie,x=m.Walker,p=m.Range,
v={img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1};o.augment(r,{getNative:!j?function(){var b=this._.cache;return b.nativeSel||(b.nativeSel=g._getWin(this.document).getSelection())}:function(){var b=this._.cache;return b.nativeSel||(b.nativeSel=this.document.selection)},getType:!j?function(){var b=this._.cache;if(b.type)return b.type;var a=k.SELECTION_TEXT,d=this.getNative();if(d){if(1==d.rangeCount){var d=
d.getRangeAt(0),c=d.startContainer;c==d.endContainer&&c.nodeType==g.ELEMENT_NODE&&1==Number(d.endOffset-d.startOffset)&&v[c.childNodes[d.startOffset].nodeName.toLowerCase()]&&(a=k.SELECTION_ELEMENT)}}else a=k.SELECTION_NONE;return b.type=a}:function(){var b=this._.cache;if(b.type)return b.type;var a=k.SELECTION_NONE;try{var d=this.getNative(),c=d.type;"Text"==c&&(a=k.SELECTION_TEXT);"Control"==c&&(a=k.SELECTION_ELEMENT);d.createRange().parentElement&&(a=k.SELECTION_TEXT)}catch(e){}return b.type=a},
getRanges:j?function(){var b=function(a,b){a=a.duplicate();a.collapse(b);for(var c=a.parentElement(),e=c.childNodes,f,h=0;h<e.length;h++){var q=e[h];if(q.nodeType==g.ELEMENT_NODE){f=a.duplicate();f.moveToElementText(q);var q=f.compareEndPoints("StartToStart",a),i=f.compareEndPoints("EndToStart",a);f.collapse();if(0<q)break;else{if(!q||1==i&&-1==q)return{container:c,offset:h};if(!i)return{container:c,offset:h+1}}f=u}}f||(f=a.duplicate(),f.moveToElementText(c),f.collapse(!1));f.setEndPoint("StartToStart",
a);f=(""+f.text).replace(/\r\n|\r/g,"\n").length;try{for(;0<f;)f-=e[--h].nodeValue.length}catch(k){f=0}return 0===f?{container:c,offset:h}:{container:e[h],offset:-f}};return function(a){var d=this._.cache;if(d.ranges&&!a)return d.ranges;var c=this.getNative(),a=c&&c.createRange(),e=this.getType();if(!c)return[];if(e==k.SELECTION_TEXT)return c=new p(this.document),e=b(a,l),c.setStart(new i(e.container),e.offset),e=b(a),c.setEnd(new i(e.container),e.offset),d.ranges=[c];if(e==k.SELECTION_ELEMENT){d=
d.ranges=[];for(e=0;e<a.length;e++){for(var f=a.item(e),h=f.parentNode,g=0,c=new p(this.document);g<h.childNodes.length&&h.childNodes[g]!=f;g++);c.setStart(new i(h),g);c.setEnd(new i(h),g+1);d.push(c)}return d}return d.ranges=[]}}():function(b){var a=this._.cache;if(a.ranges&&!b)return a.ranges;var b=[],d=this.getNative();if(!d)return[];for(var c=0;c<d.rangeCount;c++){var e=d.getRangeAt(c),f=new p(this.document);f.setStart(new i(e.startContainer),e.startOffset);f.setEnd(new i(e.endContainer),e.endOffset);
b.push(f)}return a.ranges=b},getStartElement:function(){var b=this._.cache;if(void 0!==b.startElement)return b.startElement;var a,d=this.getNative();switch(this.getType()){case k.SELECTION_ELEMENT:return this.getSelectedElement();case k.SELECTION_TEXT:var c=this.getRanges()[0];if(c&&!c.collapsed){for(c.optimize();l;)if(a=c.startContainer,c.startOffset==(a[0].nodeType===g.ELEMENT_NODE?a[0].childNodes.length:a[0].nodeValue.length)&&!a._4e_isBlockBoundary())c.setStartAfter(a);else break;a=c.startContainer;
if(a[0].nodeType!=g.ELEMENT_NODE)return a.parent();a=new i(a[0].childNodes[c.startOffset]);if(!a[0]||a[0].nodeType!=g.ELEMENT_NODE)return c.startContainer;for(c=a[0].firstChild;c&&c.nodeType==g.ELEMENT_NODE;)a=new i(c),c=c.firstChild;return a}if(j)c=d.createRange(),c.collapse(l),a=new i(c.parentElement());else{if((a=d.anchorNode)&&a.nodeType!=g.ELEMENT_NODE)a=a.parentNode;a&&(a=new i(a))}}return b.startElement=a},getSelectedElement:function(){var b,a=this._.cache;if(void 0!==a.selectedElement)return a.selectedElement;
j&&(b=this.getNative().createRange(),b=b.item&&b.item(0));var d;if(b)d=new i(b);else{b=this.getRanges()[0];for(var c,e=2;e&&(!(d=b.getEnclosedNode())||!(d[0].nodeType==g.ELEMENT_NODE&&v[d.nodeName()]&&(c=d)));e--)b.shrink(s.SHRINK_ELEMENT);d=c}return a.selectedElement=d},reset:function(){this._.cache={}},selectElement:function(b){var a,d=this.document;if(j)try{a=d.body.createControlRange(),a.addElement(b[0]),a.select()}catch(c){a=d.body.createTextRange(),a.moveToElementText(b[0]),a.select()}finally{}else a=
d.createRange(),a.selectNode(b[0]),b=this.getNative(),b.removeAllRanges(),b.addRange(a);this.reset()},selectRanges:function(b){if(j){if(1<b.length){var a=b[b.length-1];b[0].setEnd(a.endContainer,a.endOffset);b.length=1}b[0]&&b[0].select()}else{a=this.getNative();if(!a)return;a.removeAllRanges();for(var d=0;d<b.length;d++){var c=b[d],e=this.document.createRange(),f=c.startContainer;if(c.collapsed&&(n.gecko&&1.09>n.gecko||n.opera||n.webkit)&&f[0].nodeType==g.ELEMENT_NODE&&!f[0].childNodes.length)f[0].appendChild(this.document.createTextNode(n.webkit?
"​":"")),c.startOffset++,c.endOffset++;e.setStart(f[0],c.startOffset);e.setEnd(c.endContainer[0],c.endOffset);a.addRange(e)}}this.reset()},createBookmarks2:function(b){for(var a=[],d=this.getRanges(),c=0;c<d.length;c++)a.push(d[c].createBookmark2(b));return a},createBookmarks:function(b,a){for(var d=[],c=this.document,e,a=a||this.getRanges(),f=a.length,h=0;h<f;h++){d.push(e=a[h].createBookmark(b,l));var i=(b=e.serializable)?o.one("#"+e.startNode,c):e.startNode;e=b?o.one("#"+e.endNode,c):e.endNode;
for(var k=h+1;k<f;k++){var j=a[k],m=j.startContainer,n=j.endContainer;g.equals(m,i.parent())&&j.startOffset++;g.equals(m,e.parent())&&j.startOffset++;g.equals(n,i.parent())&&j.endOffset++;g.equals(n,e.parent())&&j.endOffset++}}return d},selectBookmarks:function(b){for(var a=[],d=0;d<b.length;d++){var c=new p(this.document);c.moveToBookmark(b[d]);a.push(c)}this.selectRanges(a);return this},getCommonAncestor:function(){var b=this.getRanges();return b[0].startContainer._4e_commonAncestor(b[b.length-
1].endContainer)},scrollIntoView:function(){var b=this.getStartElement();b&&b.scrollIntoView(void 0,!1)},removeAllRanges:function(){var b=this.getNative();j?b&&b.clear():b&&b.removeAllRanges()}});var w={table:1,tbody:1,tr:1},y=x.whitespaces(l),z=/\ufeff|\u00a0/;p.prototype.select=p.prototype.select=!j?function(){var b=this.startContainer;this.collapsed&&b[0].nodeType==g.ELEMENT_NODE&&!b[0].childNodes.length&&b[0].appendChild(this.document.createTextNode(""));var a=this.document.createRange();a.setStart(b[0],
this.startOffset);try{a.setEnd(this.endContainer[0],this.endOffset)}catch(d){if(0<=d.toString().indexOf("NS_ERROR_ILLEGAL_VALUE"))this.collapse(l),a.setEnd(this.endContainer[0],this.endOffset);else throw d;}b=t(this.document).getNative();b.removeAllRanges();b.addRange(a)}:function(b){var a=this.collapsed,d,c;if(this.startContainer[0]===this.endContainer[0]&&1==this.endOffset-this.startOffset){var e=this.startContainer[0].childNodes[this.startOffset];if(e.nodeType==g.ELEMENT_NODE){(new r(this.document)).selectElement(new i(e));
return}}(this.startContainer[0].nodeType==g.ELEMENT_NODE&&this.startContainer.nodeName()in w||this.endContainer[0].nodeType==g.ELEMENT_NODE&&this.endContainer.nodeName()in w)&&this.shrink(s.SHRINK_ELEMENT,l);var f=this.createBookmark(),e=f.startNode,h;a||(h=f.endNode);f=this.document.body.createTextRange();f.moveToElementText(e[0]);f.moveStart("character",1);if(h)b=this.document.body.createTextRange(),b.moveToElementText(h[0]),f.setEndPoint("EndToEnd",b),f.moveEnd("character",-1);else{for(d=e[0].nextSibling;d&&
!y(d);)d=d.nextSibling;d=!(d&&d.nodeValue&&d.nodeValue.match(z))&&(b||!e[0].previousSibling||e[0].previousSibling&&"br"==g.nodeName(e[0].previousSibling));c=new i(this.document.createElement("span"));c.html("&#65279;");c.insertBefore(e);d&&g.insertBefore(this.document.createTextNode(""),e[0]||e)}this.setStartBefore(e);e._4e_remove();if(a){if(d?(f.moveStart("character",-1),f.select(),this.document.selection.clear()):f.select(),c)this.moveToPosition(c,s.POSITION_BEFORE_START),c._4e_remove()}else this.setEndBefore(h),
h._4e_remove(),f.select()};r.getSelection=t;return m.Selection=r},{requires:["./base","./walker","./range","./dom"]});
