Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / editor / plugins / NormalizeStyle.js

History | View | Annotate | Download (8.65 KB)

1 9 andrej.cim
/*
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
5
*/
6
7
8
if(!dojo._hasResource["dojox.editor.plugins.NormalizeStyle"]){
9
dojo._hasResource["dojox.editor.plugins.NormalizeStyle"]=true;
10
dojo.provide("dojox.editor.plugins.NormalizeStyle");
11
dojo.require("dijit._editor._Plugin");
12
dojo.require("dijit._editor.html");
13
dojo.experimental("dojox.editor.plugins.NormalizeStyle");
14
dojo.declare("dojox.editor.plugins.NormalizeStyle",dijit._editor._Plugin,{mode:"semantic",condenseSpans:true,setEditor:function(_1){
15
this.editor=_1;
16
_1.customUndo=true;
17
if(this.mode==="semantic"){
18
this.editor.contentDomPostFilters.push(dojo.hitch(this,this._convertToSemantic));
19
}else{
20
if(this.mode==="css"){
21
this.editor.contentDomPostFilters.push(dojo.hitch(this,this._convertToCss));
22
}
23
}
24
if(dojo.isIE){
25
this.editor.contentDomPreFilters.push(dojo.hitch(this,this._convertToSemantic));
26
this._browserFilter=this._convertToSemantic;
27
}else{
28
if(dojo.isWebKit){
29
this.editor.contentDomPreFilters.push(dojo.hitch(this,this._convertToCss));
30
this._browserFilter=this._convertToCss;
31
}else{
32
if(dojo.isMoz){
33
this.editor.contentDomPreFilters.push(dojo.hitch(this,this._convertToSemantic));
34
this._browserFilter=this._convertToSemantic;
35
}else{
36
this.editor.contentDomPreFilters.push(dojo.hitch(this,this._convertToSemantic));
37
this._browserFilter=this._convertToSemantic;
38
}
39
}
40
}
41
if(this.editor._inserthtmlImpl){
42
this.editor._oldInsertHtmlImpl=this.editor._inserthtmlImpl;
43
}
44
this.editor._inserthtmlImpl=dojo.hitch(this,this._inserthtmlImpl);
45
},_convertToSemantic:function(_2){
46
if(_2){
47
var w=this.editor.window;
48
var _3=this;
49
var _4=function(_5){
50
if(_5.nodeType==1){
51
if(_5.id!=="dijitEditorBody"){
52
var _6=_5.style;
53
var _7=_5.tagName?_5.tagName.toLowerCase():"";
54
var _8;
55
if(_6&&_7!="table"&&_7!="ul"&&_7!="ol"){
56
var fw=_6.fontWeight?_6.fontWeight.toLowerCase():"";
57
var fs=_6.fontStyle?_6.fontStyle.toLowerCase():"";
58
var td=_6.textDecoration?_6.textDecoration.toLowerCase():"";
59
var s=_6.fontSize?_6.fontSize.toLowerCase():"";
60
var bc=_6.backgroundColor?_6.backgroundColor.toLowerCase():"";
61
var c=_6.color?_6.color.toLowerCase():"";
62
var _9=function(_a,_b){
63
if(_a){
64
while(_b.firstChild){
65
_a.appendChild(_b.firstChild);
66
}
67
if(_7=="span"&&!_b.style.cssText){
68
dojo.place(_a,_b,"before");
69
_b.parentNode.removeChild(_b);
70
_b=_a;
71
}else{
72
_b.appendChild(_a);
73
}
74
}
75
return _b;
76
};
77
switch(fw){
78
case "bold":
79
case "bolder":
80
case "700":
81
case "800":
82
case "900":
83
_8=dojo.withGlobal(w,"create",dojo,["b",{}]);
84
_5.style.fontWeight="";
85
break;
86
}
87
_5=_9(_8,_5);
88
_8=null;
89
if(fs=="italic"){
90
_8=dojo.withGlobal(w,"create",dojo,["i",{}]);
91
_5.style.fontStyle="";
92
}
93
_5=_9(_8,_5);
94
_8=null;
95
if(td){
96
var da=td.split(" ");
97
var _c=0;
98
dojo.forEach(da,function(s){
99
switch(s){
100
case "underline":
101
_8=dojo.withGlobal(w,"create",dojo,["u",{}]);
102
break;
103
case "line-through":
104
_8=dojo.withGlobal(w,"create",dojo,["strike",{}]);
105
break;
106
}
107
_c++;
108
if(_c==da.length){
109
_5.style.textDecoration="";
110
}
111
_5=_9(_8,_5);
112
_8=null;
113
});
114
}
115
if(s){
116
var _d={"xx-small":1,"x-small":2,"small":3,"medium":4,"large":5,"x-large":6,"xx-large":7,"-webkit-xxx-large":7};
117
if(s.indexOf("pt")>0){
118
s=s.substring(0,s.indexOf("pt"));
119
s=parseInt(s);
120
if(s<5){
121
s="xx-small";
122
}else{
123
if(s<10){
124
s="x-small";
125
}else{
126
if(s<15){
127
s="small";
128
}else{
129
if(s<20){
130
s="medium";
131
}else{
132
if(s<25){
133
s="large";
134
}else{
135
if(s<30){
136
s="x-large";
137
}else{
138
if(s>30){
139
s="xx-large";
140
}
141
}
142
}
143
}
144
}
145
}
146
}
147
}else{
148
if(s.indexOf("px")>0){
149
s=s.substring(0,s.indexOf("px"));
150
s=parseInt(s);
151
if(s<5){
152
s="xx-small";
153
}else{
154
if(s<10){
155
s="x-small";
156
}else{
157
if(s<15){
158
s="small";
159
}else{
160
if(s<20){
161
s="medium";
162
}else{
163
if(s<25){
164
s="large";
165
}else{
166
if(s<30){
167
s="x-large";
168
}else{
169
if(s>30){
170
s="xx-large";
171
}
172
}
173
}
174
}
175
}
176
}
177
}
178
}
179
}
180
var _e=_d[s];
181
if(!_e){
182
_e=3;
183
}
184
_8=dojo.withGlobal(w,"create",dojo,["font",{size:_e}]);
185
_5.style.fontSize="";
186
}
187
_5=_9(_8,_5);
188
_8=null;
189
if(bc&&_7!=="font"&&_3._isInline(_7)){
190
bc=new dojo.Color(bc).toHex();
191
_8=dojo.withGlobal(w,"create",dojo,["font",{style:{backgroundColor:bc}}]);
192
_5.style.backgroundColor="";
193
}
194
if(c&&_7!=="font"){
195
c=new dojo.Color(c).toHex();
196
_8=dojo.withGlobal(w,"create",dojo,["font",{color:c}]);
197
_5.style.color="";
198
}
199
_5=_9(_8,_5);
200
_8=null;
201
}
202
}
203
if(_5.childNodes){
204
var _f=[];
205
dojo.forEach(_5.childNodes,function(n){
206
_f.push(n);
207
});
208
dojo.forEach(_f,_4);
209
}
210
}
211
return _5;
212
};
213
return this._normalizeTags(_4(_2));
214
}
215
return _2;
216
},_normalizeTags:function(_10){
217
var w=this.editor.window;
218
var _11=dojo.withGlobal(w,function(){
219
return dojo.query("em,s,strong",_10);
220
});
221
if(_11&&_11.length){
222
dojo.forEach(_11,function(n){
223
if(n){
224
var tag=n.tagName?n.tagName.toLowerCase():"";
225
var _12;
226
switch(tag){
227
case "s":
228
_12="strike";
229
break;
230
case "em":
231
_12="i";
232
break;
233
case "strong":
234
_12="b";
235
break;
236
}
237
if(_12){
238
var _13=dojo.withGlobal(w,"create",dojo,[_12,null,n,"before"]);
239
while(n.firstChild){
240
_13.appendChild(n.firstChild);
241
}
242
n.parentNode.removeChild(n);
243
}
244
}
245
});
246
}
247
return _10;
248
},_convertToCss:function(_14){
249
if(_14){
250
var w=this.editor.window;
251
var _15=function(_16){
252
if(_16.nodeType==1){
253
if(_16.id!=="dijitEditorBody"){
254
var tag=_16.tagName?_16.tagName.toLowerCase():"";
255
if(tag){
256
var _17;
257
switch(tag){
258
case "b":
259
case "strong":
260
_17=dojo.withGlobal(w,"create",dojo,["span",{style:{"fontWeight":"bold"}}]);
261
break;
262
case "i":
263
case "em":
264
_17=dojo.withGlobal(w,"create",dojo,["span",{style:{"fontStyle":"italic"}}]);
265
break;
266
case "u":
267
_17=dojo.withGlobal(w,"create",dojo,["span",{style:{"textDecoration":"underline"}}]);
268
break;
269
case "strike":
270
case "s":
271
_17=dojo.withGlobal(w,"create",dojo,["span",{style:{"textDecoration":"line-through"}}]);
272
break;
273
case "font":
274
var _18={};
275
if(dojo.attr(_16,"color")){
276
_18.color=dojo.attr(_16,"color");
277
}
278
if(dojo.attr(_16,"face")){
279
_18.fontFace=dojo.attr(_16,"face");
280
}
281
if(_16.style&&_16.style.backgroundColor){
282
_18.backgroundColor=_16.style.backgroundColor;
283
}
284
if(_16.style&&_16.style.color){
285
_18.color=_16.style.color;
286
}
287
var _19={1:"xx-small",2:"x-small",3:"small",4:"medium",5:"large",6:"x-large",7:"xx-large"};
288
if(dojo.attr(_16,"size")){
289
_18.fontSize=_19[dojo.attr(_16,"size")];
290
}
291
_17=dojo.withGlobal(w,"create",dojo,["span",{style:_18}]);
292
break;
293
}
294
if(_17){
295
while(_16.firstChild){
296
_17.appendChild(_16.firstChild);
297
}
298
dojo.place(_17,_16,"before");
299
_16.parentNode.removeChild(_16);
300
_16=_17;
301
}
302
}
303
}
304
if(_16.childNodes){
305
var _1a=[];
306
dojo.forEach(_16.childNodes,function(n){
307
_1a.push(n);
308
});
309
dojo.forEach(_1a,_15);
310
}
311
}
312
return _16;
313
};
314
_14=_15(_14);
315
if(this.condenseSpans){
316
this._condenseSpans(_14);
317
}
318
}
319
return _14;
320
},_condenseSpans:function(_1b){
321
var _1c=function(_1d){
322
var _1e=function(_1f){
323
var m;
324
if(_1f){
325
m={};
326
var _20=_1f.toLowerCase().split(";");
327
dojo.forEach(_20,function(s){
328
if(s){
329
var ss=s.split(":");
330
var key=ss[0]?dojo.trim(ss[0]):"";
331
var val=ss[1]?dojo.trim(ss[1]):"";
332
if(key&&val){
333
var i;
334
var _21="";
335
for(i=0;i<key.length;i++){
336
var ch=key.charAt(i);
337
if(ch=="-"){
338
i++;
339
ch=key.charAt(i);
340
_21+=ch.toUpperCase();
341
}else{
342
_21+=ch;
343
}
344
}
345
m[_21]=val;
346
}
347
}
348
});
349
}
350
return m;
351
};
352
if(_1d&&_1d.nodeType==1){
353
var tag=_1d.tagName?_1d.tagName.toLowerCase():"";
354
if(tag==="span"&&_1d.childNodes&&_1d.childNodes.length===1){
355
var c=_1d.firstChild;
356
while(c&&c.nodeType==1&&c.tagName&&c.tagName.toLowerCase()=="span"){
357
if(!dojo.attr(c,"class")&&!dojo.attr(c,"id")&&c.style){
358
var s1=_1e(_1d.style.cssText);
359
var s2=_1e(c.style.cssText);
360
if(s1&&s2){
361
var _22={};
362
var i;
363
for(i in s1){
364
if(!s1[i]||!s2[i]||s1[i]==s2[i]){
365
_22[i]=s1[i];
366
delete s2[i];
367
}else{
368
if(s1[i]!=s2[i]){
369
if(i=="textDecoration"){
370
_22[i]=s1[i]+" "+s2[i];
371
delete s2[i];
372
}else{
373
_22=null;
374
}
375
break;
376
}else{
377
_22=null;
378
break;
379
}
380
}
381
}
382
if(_22){
383
for(i in s2){
384
_22[i]=s2[i];
385
}
386
dojo.style(_1d,_22);
387
while(c.firstChild){
388
_1d.appendChild(c.firstChild);
389
}
390
var t=c.nextSibling;
391
c.parentNode.removeChild(c);
392
c=t;
393
}else{
394
c=c.nextSibling;
395
}
396
}else{
397
c=c.nextSibling;
398
}
399
}else{
400
c=c.nextSibling;
401
}
402
}
403
}
404
}
405
if(_1d.childNodes&&_1d.childNodes.length){
406
dojo.forEach(_1d.childNodes,_1c);
407
}
408
};
409
_1c(_1b);
410
},_isInline:function(tag){
411
switch(tag){
412
case "a":
413
case "b":
414
case "strong":
415
case "s":
416
case "strike":
417
case "i":
418
case "u":
419
case "em":
420
case "sup":
421
case "sub":
422
case "span":
423
case "font":
424
case "big":
425
case "cite":
426
case "q":
427
case "img":
428
case "small":
429
return true;
430
default:
431
return false;
432
}
433
},_inserthtmlImpl:function(_23){
434
if(_23){
435
var div=this.editor.document.createElement("div");
436
div.innerHTML=_23;
437
div=this._browserFilter(div);
438
_23=dijit._editor.getChildrenHtml(div);
439
div.innerHTML="";
440
if(this.editor._oldInsertHtmlImpl){
441
return this.editor._oldInsertHtmlImpl(_23);
442
}else{
443
return this.editor.execCommand("inserthtml",_23);
444
}
445
}
446
return false;
447
}});
448
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
449
if(o.plugin){
450
return;
451
}
452
var _24=o.args.name.toLowerCase();
453
if(_24==="normalizestyle"){
454
o.plugin=new dojox.editor.plugins.NormalizeStyle({mode:("mode" in o.args)?o.args.mode:"semantic",condenseSpans:("condenseSpans" in o.args)?o.args.condenseSpans:true});
455
}
456
});
457
}