(function(){var a={table:1,ul:1,ol:1,blockquote:1,div:1},b={},c={};CKEDITOR.tools.extend(b,a,{tr:1,p:1,div:1,li:1});CKEDITOR.tools.extend(c,b,{td:1});function d(p){e(p);f(p);};function e(p){var q=p.editor,r=p.data.path;if(q.readOnly)return;var s=q.config.useComputedState,t;s=s===undefined||s;if(!s)t=g(r.lastElement);t=t||r.block||r.blockLimit;t.is('body')&&(t=q.getSelection().getRanges()[0].getEnclosedNode());if(!t)return;var u=s?t.getComputedStyle('direction'):t.getStyle('direction')||t.getAttribute('dir');q.getCommand('bidirtl').setState(u=='rtl'?CKEDITOR.TRISTATE_ON:CKEDITOR.TRISTATE_OFF);q.getCommand('bidiltr').setState(u=='ltr'?CKEDITOR.TRISTATE_ON:CKEDITOR.TRISTATE_OFF);};function f(p){var q=p.editor,r=p.data.path.block||p.data.path.blockLimit;q.fire('contentDirChanged',r?r.getComputedStyle('direction'):q.lang.dir);};function g(p){while(p&&!(p.getName() in c||p.is('body'))){var q=p.getParent();if(!q)break;p=q;}return p;};function h(p,q,r,s){if(p.isReadOnly())return;CKEDITOR.dom.element.setMarker(s,p,'bidi_processed',1);var t=p;while((t=t.getParent())&&!t.is('body')){if(t.getCustomData('bidi_processed')){p.removeStyle('direction');p.removeAttribute('dir');return;}}var u='useComputedState' in r.config?r.config.useComputedState:1,v=u?p.getComputedStyle('direction'):p.getStyle('direction')||p.hasAttribute('dir');if(v==q)return;p.removeStyle('direction');if(u){p.removeAttribute('dir');if(q!=p.getComputedStyle('direction'))p.setAttribute('dir',q);}else p.setAttribute('dir',q);r.forceNextSelectionCheck();};function i(p,q,r){var s=p.getCommonAncestor(false,true);p=p.clone();p.enlarge(r==CKEDITOR.ENTER_BR?CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS:CKEDITOR.ENLARGE_BLOCK_CONTENTS);if(p.checkBoundaryOfElement(s,CKEDITOR.START)&&p.checkBoundaryOfElement(s,CKEDITOR.END)){var t;while(s&&s.type==CKEDITOR.NODE_ELEMENT&&(t=s.getParent())&&t.getChildCount()==1&&!(s.getName() in q))s=t;return s.type==CKEDITOR.NODE_ELEMENT&&s.getName() in q&&s;}};function j(p){return function(q){var r=q.getSelection(),s=q.config.enterMode,t=r.getRanges();if(t&&t.length){var u={},v=r.createBookmarks(),w=t.createIterator(),x,y=0;while(x=w.getNextRange(1)){var z=x.getEnclosedNode();if(!z||z&&!(z.type==CKEDITOR.NODE_ELEMENT&&z.getName() in b))z=i(x,a,s);z&&h(z,p,q,u);var A,B,C=new CKEDITOR.dom.walker(x),D=v[y].startNode,E=v[y++].endNode;C.evaluator=function(F){return!!(F.type==CKEDITOR.NODE_ELEMENT&&F.getName() in a&&!(F.getName()==(s==CKEDITOR.ENTER_P?'p':'div')&&F.getParent().type==CKEDITOR.NODE_ELEMENT&&F.getParent().getName()=='blockquote')&&F.getPosition(D)&CKEDITOR.POSITION_FOLLOWING&&(F.getPosition(E)&CKEDITOR.POSITION_PRECEDING+CKEDITOR.POSITION_CONTAINS)==CKEDITOR.POSITION_PRECEDING);
};while(B=C.next())h(B,p,q,u);A=x.createIterator();A.enlargeBr=s!=CKEDITOR.ENTER_BR;while(B=A.getNextParagraph(s==CKEDITOR.ENTER_P?'p':'div'))h(B,p,q,u);}CKEDITOR.dom.element.clearAllMarkers(u);q.forceNextSelectionCheck();r.selectBookmarks(v);q.focus();}};};CKEDITOR.plugins.add('bidi',{requires:['styles','button'],init:function(p){var q=function(s,t,u,v){p.addCommand(u,new CKEDITOR.command(p,{exec:v}));p.ui.addButton(s,{label:t,command:u});},r=p.lang.bidi;q('BidiLtr',r.ltr,'bidiltr',j('ltr'));q('BidiRtl',r.rtl,'bidirtl',j('rtl'));p.on('selectionChange',d);p.on('contentDom',function(){p.document.on('dirChanged',function(s){p.fire('dirChanged',{node:s.data,dir:s.data.getDirection(1)});});});}});function k(p){var q=p.getDocument().getBody().getParent();while(p){if(p.equals(q))return false;p=p.getParent();}return true;};function l(p){var q=p==m.setAttribute,r=p==m.removeAttribute,s=/\bdirection\s*:\s*(.*?)\s*(:?$|;)/;return function(t,u){var x=this;if(!x.getDocument().equals(CKEDITOR.document)){var v;if((t==(q||r?'dir':'direction')||t=='style'&&(r||s.test(u)))&&!k(x)){v=x.getDirection(1);var w=p.apply(x,arguments);if(v!=x.getDirection(1)){x.getDocument().fire('dirChanged',x);return w;}}}return p.apply(x,arguments);};};var m=CKEDITOR.dom.element.prototype,n=['setStyle','removeStyle','setAttribute','removeAttribute'];for(var o=0;o<n.length;o++)m[n[o]]=CKEDITOR.tools.override(m[n[o]],l);})();
