/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/plugin/listUtils/cmd",function(r,p,n,e){function u(f){this.type=f}function v(f,d){var b,e,h=d.blockLimit,c=d.elements;if(!h)return!1;if(c)for(var a=0;a<c.length&&(b=c[a])&&b[0]!==h[0];a++)if(t[e=b.nodeName()]&&e==f)return!0;return!1}var t={ol:"insertOrderedList",ul:"insertUnorderedList"},w=p.RANGE,y=p.ElementPath,x=p.Walker,z=r.UA,m=r.Node,s=r.DOM,A=/^h[1-6]$/;u.prototype={changeListType:function(f,d,b,j){for(var h=n.listToArray(d.root,b,e,e,e),c=[],f=0;f<d.contents.length;f++){var a=
d.contents[f];if((a=a.closest("li",e))&&a[0]&&!a.data("list_item_processed"))c.push(a),a._4e_setMarker(b,"list_item_processed",!0,e)}a=new m(d.root[0].ownerDocument.createElement(this.type));for(f=0;f<c.length;f++){var i=c[f].data("listarray_index");h[i].parent=a}for(var b=n.arrayToList(h,b,null,"p"),k,h=b.listNode.childNodes.length,f=0;f<h&&(k=new m(b.listNode.childNodes[f]));f++)k.nodeName()==this.type&&j.push(k);d.root.before(b.listNode);d.root.remove()},createList:function(f,d,b){var j=d.contents,
f=d.root[0].ownerDocument,h=[];if(1==j.length&&j[0][0]===d.root[0]){var c=new m(f.createElement("div"));j[0][0].nodeType!=s.TEXT_NODE&&j[0]._4e_moveChildren(c,e,e);j[0][0].appendChild(c[0]);j[0]=c}d=d.contents[0].parent();for(c=0;c<j.length;c++)d=d._4e_commonAncestor(j[c].parent(),e);for(c=0;c<j.length;c++)for(var a=j[c],i;i=a.parent();){if(i[0]===d[0]){h.push(a);break}a=i}if(!(1>h.length)){j=new m(h[h.length-1][0].nextSibling);c=new m(f.createElement(this.type));for(b.push(c);h.length;)b=h.shift(),
a=new m(f.createElement("li")),A.test(b.nodeName())?a[0].appendChild(b[0]):(b._4e_copyAttributes(a,e,e),b._4e_moveChildren(a,e,e),b.remove()),c[0].appendChild(a[0]),z.ie||a._4e_appendBogus(e);j[0]?c.insertBefore(j,e):d.append(c)}},removeList:function(f,d,b){function j(a){if((g=new m(k[a?"firstChild":"lastChild"]))&&!(g[0].nodeType==s.ELEMENT_NODE&&g._4e_isBlockBoundary(e,e))&&(l=d.root[a?"prev":"next"](x.whitespaces(!0),1))&&!(g[0].nodeType==s.ELEMENT_NODE&&l._4e_isBlockBoundary({br:1},e)))g[a?"before":
"after"](f.get("document")[0].createElement("br"))}for(var h=n.listToArray(d.root,b,e,e,e),c=[],a=0;a<d.contents.length;a++){var i=d.contents[a];if((i=i.closest("li",e))&&!i.data("list_item_processed"))c.push(i),i._4e_setMarker(b,"list_item_processed",!0,e)}i=null;for(a=0;a<c.length;a++)i=c[a].data("listarray_index"),h[i].indent=-1;for(a=i+1;a<h.length;a++)if(h[a].indent>Math.max(h[a-1].indent,0)){c=h[a-1].indent+1-h[a].indent;for(i=h[a].indent;h[a]&&h[a].indent>=i;)h[a].indent+=c,a++;a--}var k=n.arrayToList(h,
b,null,"p").listNode,g,l;j(!0);j(e);d.root.before(k);d.root.remove()},exec:function(f){var d=f.getSelection(),b=d&&d.getRanges();if(b&&!(1>b.length)){for(var j=d.getStartElement(),j=new p.ElementPath(j),h=v(this.type,j),j=d.createBookmarks(!0),c=[],a={};0<b.length;){var i=b.shift(),k=i.getBoundaryNodes(),g=k.startNode,l=k.endNode;g[0].nodeType==s.ELEMENT_NODE&&"td"==g.nodeName()&&i.setStartAt(k.startNode,w.POSITION_AFTER_START);l[0].nodeType==s.ELEMENT_NODE&&"td"==l.nodeName()&&i.setEndAt(k.endNode,
w.POSITION_BEFORE_END);i=i.createIterator();for(i.forceBrBreak=!1;k=i.getNextParagraph();)if(!k.data("list_block")){k._4e_setMarker(a,"list_block",1,e);for(var l=new y(k),m=l.elements,o=null,n=!1,o=l.blockLimit,q,g=m.length-1;0<=g&&(q=m[g]);g--)if(t[q.nodeName()]&&o.contains(q)){o.removeData("list_group_object");(g=q.data("list_group_object"))?g.contents.push(k):(g={root:q,contents:[k]},c.push(g),q._4e_setMarker(a,"list_group_object",g,e));n=!0;break}n||(l=o||l.block,l.data("list_group_object")?l.data("list_group_object").contents.push(k):
(g={root:l,contents:[k]},l._4e_setMarker(a,"list_group_object",g,e),c.push(g)))}}for(b=[];0<c.length;)g=c.shift(),h?t[g.root.nodeName()]&&this.removeList(f,g,a):t[g.root.nodeName()]?this.changeListType(f,g,a,b):(p.Utils.clearAllMarkers(a),this.createList(f,g,b));for(var r=this,g=0;g<b.length;g++)o=b[g],f=function(a,c){var b=c[a?"prev":"next"](x.whitespaces(!0),1);b&&b[0]&&b.nodeName()==r.type&&(b.remove(),b._4e_moveChildren(c,a?!0:!1,e))},f(e,o),f(!0,o);p.Utils.clearAllMarkers(a);d.selectBookmarks(j)}}};
return{ListCommand:u,queryActive:v}},{requires:["editor","../listUtils/"]});
