2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo._hasResource["dijit._editor.plugins.ViewSource"]){
9 dojo._hasResource["dijit._editor.plugins.ViewSource"]=true;
10 dojo.provide("dijit._editor.plugins.ViewSource");
11 dojo.require("dojo.window");
12 dojo.require("dojo.i18n");
13 dojo.require("dijit._editor._Plugin");
14 dojo.require("dijit.form.Button");
15 dojo.requireLocalization("dijit._editor","commands",null,"ROOT,ar,ca,cs,da,de,el,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
16 dojo.declare("dijit._editor.plugins.ViewSource",dijit._editor._Plugin,{stripScripts:true,stripComments:true,stripIFrames:true,readOnly:false,_fsPlugin:null,toggle:function(){
20 this.button.set("checked",!this.button.get("checked"));
21 },_initButton:function(){
22 var _1=dojo.i18n.getLocalization("dijit._editor","commands"),_2=this.editor;
23 this.button=new dijit.form.ToggleButton({label:_1["viewSource"],dir:_2.dir,lang:_2.lang,showLabel:false,iconClass:this.iconClassPrefix+" "+this.iconClassPrefix+"ViewSource",tabIndex:"-1",onChange:dojo.hitch(this,"_showSource")});
25 this._ieFixNode=dojo.create("div",{style:{opacity:"0",zIndex:"-1000",position:"absolute",top:"-1000px"}},dojo.body());
27 this.button.set("readOnly",false);
28 },setEditor:function(_3){
31 this.editor.addKeyHandler(dojo.keys.F12,true,true,dojo.hitch(this,function(e){
35 setTimeout(dojo.hitch(this,function(){
39 },_showSource:function(_4){
47 this._createSourceView();
50 ed._sourceQueryCommandEnabled=ed.queryCommandEnabled;
51 ed.queryCommandEnabled=function(_8){
52 var _9=_8.toLowerCase();
53 if(_9==="viewsource"){
59 this.editor.onDisplayChanged();
64 this._disabledPlugins=dojo.filter(_5,function(p){
65 if(p&&p.button&&!p.button.get("disabled")&&!(p instanceof dijit._editor.plugins.ViewSource)){
66 p._vs_updateState=p.updateState;
67 p.updateState=function(){
70 p.button.set("disabled",true);
79 p.button.set("checked",false);
89 this._fsPlugin._getAltViewNode=function(){
93 this.sourceArea.value=_6;
94 var is=dojo.marginBox(ed.iframe.parentNode);
95 dojo.marginBox(this.sourceArea,{w:is.w,h:is.h});
96 dojo.style(ed.iframe,"display","none");
97 dojo.style(this.sourceArea,{display:"block"});
99 var vp=dojo.window.getBox();
100 if("_prevW" in this&&"_prevH" in this){
101 if(vp.w===this._prevW&&vp.h===this._prevH){
112 clearTimeout(this._resizer);
113 delete this._resizer;
115 this._resizer=setTimeout(dojo.hitch(this,function(){
116 delete this._resizer;
120 this._resizeHandle=dojo.connect(window,"onresize",this,_a);
121 setTimeout(dojo.hitch(this,this._resize),100);
122 this.editor.onNormalizedDisplayChanged();
124 if(!ed._sourceQueryCommandEnabled){
127 dojo.disconnect(this._resizeHandle);
128 delete this._resizeHandle;
129 ed.queryCommandEnabled=ed._sourceQueryCommandEnabled;
131 _6=this.sourceArea.value;
137 dojo.forEach(this._disabledPlugins,function(p){
138 p.button.set("disabled",false);
139 if(p._vs_updateState){
140 p.updateState=p._vs_updateState;
143 this._disabledPlugins=null;
144 dojo.style(this.sourceArea,"display","none");
145 dojo.style(ed.iframe,"display","block");
146 delete ed._sourceQueryCommandEnabled;
147 this.editor.onDisplayChanged();
149 setTimeout(dojo.hitch(this,function(){
150 var _b=ed.domNode.parentNode;
152 var _c=dijit.getEnclosingWidget(_b);
162 },_resize:function(){
164 var _d=ed.getHeaderHeight();
165 var fH=ed.getFooterHeight();
166 var eb=dojo.position(ed.domNode);
167 var _e=dojo._getPadBorderExtents(ed.iframe.parentNode);
168 var _f=dojo._getMarginExtents(ed.iframe.parentNode);
169 var _10=dojo._getPadBorderExtents(ed.domNode);
170 var _11=dojo._getMarginExtents(ed.domNode);
171 var edb={w:eb.w-(_10.w+_11.w),h:eb.h-(_d+_10.h+_11.h+fH)};
172 if(this._fsPlugin&&this._fsPlugin.isFullscreen){
173 var vp=dojo.window.getBox();
175 edb.h=(vp.h-(_d+_10.h+fH));
181 var _12=-this._ieFixNode.offsetTop/1000;
182 edb.w=Math.floor((edb.w+0.9)/_12);
183 edb.h=Math.floor((edb.h+0.9)/_12);
185 dojo.marginBox(this.sourceArea,{w:edb.w-(_e.w+_f.w),h:edb.h-(_e.h+_f.h)});
186 dojo.marginBox(ed.iframe.parentNode,{h:edb.h});
187 },_createSourceView:function(){
190 this.sourceArea=dojo.create("textarea");
192 dojo.attr(this.sourceArea,"readOnly",true);
195 dojo.style(this.sourceArea,{padding:"0px",margin:"0px",borderWidth:"0px",borderStyle:"none"});
196 dojo.place(this.sourceArea,ed.iframe,"before");
197 if(dojo.isIE&&ed.iframe.parentNode.lastChild!==ed.iframe){
198 dojo.style(ed.iframe.parentNode.lastChild,{width:"0px",height:"0px",padding:"0px",margin:"0px",borderWidth:"0px",borderStyle:"none"});
200 ed._viewsource_oldFocus=ed.focus;
203 if(_14._sourceShown){
204 _14.setSourceAreaCaret();
208 delete this._vsFocused;
209 dijit.focus(ed.editNode);
211 ed._viewsource_oldFocus();
219 for(i=0;i<_13.length;i++){
221 if(p&&(p.declaredClass==="dijit._editor.plugins.FullScreen"||p.declaredClass===(dijit._scopeName+"._editor.plugins.FullScreen"))){
227 this._fsPlugin._viewsource_getAltViewNode=this._fsPlugin._getAltViewNode;
228 this._fsPlugin._getAltViewNode=function(){
229 return _14._sourceShown?_14.sourceArea:this._viewsource_getAltViewNode();
232 this.connect(this.sourceArea,"onkeydown",dojo.hitch(this,function(e){
233 if(this._sourceShown&&e.keyCode==dojo.keys.F12&&e.ctrlKey&&e.shiftKey){
235 this.button.set("checked",false);
236 setTimeout(dojo.hitch(this,function(){
242 },_stripScripts:function(_15){
244 _15=_15.replace(/<\s*script[^>]*>((.|\s)*?)<\\?\/\s*script\s*>/ig,"");
245 _15=_15.replace(/<\s*script\b([^<>]|\s)*>?/ig,"");
246 _15=_15.replace(/<[^>]*=(\s|)*[("|')]javascript:[^$1][(\s|.)]*[$1][^>]*>/ig,"");
249 },_stripComments:function(_16){
251 _16=_16.replace(/<!--(.|\s){1,}?-->/g,"");
254 },_stripIFrames:function(_17){
256 _17=_17.replace(/<\s*iframe[^>]*>((.|\s)*?)<\\?\/\s*iframe\s*>/ig,"");
259 },_filter:function(_18){
261 if(this.stripScripts){
262 _18=this._stripScripts(_18);
264 if(this.stripComments){
265 _18=this._stripComments(_18);
267 if(this.stripIFrames){
268 _18=this._stripIFrames(_18);
272 },setSourceAreaCaret:function(){
274 var _19=this.sourceArea;
276 if(this._sourceShown&&!this.readOnly){
278 if(this.sourceArea.createTextRange){
279 var _1a=_19.createTextRange();
281 _1a.moveStart("character",-99999);
282 _1a.moveStart("character",0);
283 _1a.moveEnd("character",0);
287 if(win.getSelection){
288 if(_19.setSelectionRange){
289 _19.setSelectionRange(0,0);
294 },destroy:function(){
296 dojo.body().removeChild(this._ieFixNode);
299 clearTimeout(this._resizer);
300 delete this._resizer;
302 if(this._resizeHandle){
303 dojo.disconnect(this._resizeHandle);
304 delete this._resizeHandle;
306 this.inherited(arguments);
308 dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
312 var _1b=o.args.name.toLowerCase();
313 if(_1b==="viewsource"){
314 o.plugin=new dijit._editor.plugins.ViewSource({readOnly:("readOnly" in o.args)?o.args.readOnly:false,stripComments:("stripComments" in o.args)?o.args.stripComments:true,stripScripts:("stripScripts" in o.args)?o.args.stripScripts:true,stripIFrames:("stripIFrames" in o.args)?o.args.stripIFrames:true});