Debian Codegraph @

dojox/editor/plugins/NormalizeStyle.js

raw symbols SCIP
1 define([
2 "dojo",
3 "dijit",
4 "dojox",
5 "dijit/_editor/_Plugin",
6 "dijit/_editor/html",
7 "dojo/_base/connect",
8 "dojo/_base/declare"
9 ], function(dojo, dijit, dojox, _Plugin, editorHtml) {
10
11 var NormalizeStyle = dojo.declare("dojox.editor.plugins.NormalizeStyle", _Plugin,{
12 // summary:
13 // This plugin provides NormalizeStyle capability to the editor. It is
14 // a headless plugin that tries to normalize how content is styled when
15 // it comes out of th editor ('b' or css). It also auto-converts
16 // incoming content to the proper one expected by the browser as well so
17 // that the native styling buttons work.
18
19 // mode: [public] String
20 // A String variable indicating if it should use semantic tags 'b', 'i', etc, or
21 // CSS styling. The default is semantic.
22 mode: "semantic",
23
24 // condenseSpans: [public] Boolean
25 // A boolean variable indicating if it should try to condense
26 // 'span''span''span' styles when in css mode
27 // The default is true, it will try to combine where it can.
28 condenseSpans: true,
29
30 setEditor: function(editor){
31 // summary:
32 // Over-ride for the setting of the editor.
33 // editor: Object
34 // The editor to configure for this plugin to use.
35 this.editor = editor;
36 editor.customUndo = true;
37
38 if(this.mode === "semantic"){
39 this.editor.contentDomPostFilters.push(dojo.hitch(this, this._convertToSemantic));
40 }else if(this.mode === "css"){
41 this.editor.contentDomPostFilters.push(dojo.hitch(this, this._convertToCss));
42 }
43
44 // Pre DOM filters are usually based on what browser, as they all use different ways to
45 // apply styles with actions and modify them.
46 if(dojo.isIE){
47 // IE still uses semantic tags most of the time, so convert to that.
48 this.editor.contentDomPreFilters.push(dojo.hitch(this, this._convertToSemantic));
49 this._browserFilter = this._convertToSemantic;
50 }else if(dojo.isWebKit){
51 this.editor.contentDomPreFilters.push(dojo.hitch(this, this._convertToCss));
52 this._browserFilter = this._convertToCss;
53 }else if(dojo.isMoz){
54 //Editor currently forces Moz into semantic mode, so we need to match. Ideally
55 //editor could get rid of that and just use CSS mode, which would work cleaner
56 //That's why this is split out, to make it easy to change later.
57 this.editor.contentDomPreFilters.push(dojo.hitch(this, this._convertToSemantic));
58 this._browserFilter = this._convertToSemantic;
59 }else{
60 this.editor.contentDomPreFilters.push(dojo.hitch(this, this._convertToSemantic));
61 this._browserFilter = this._convertToSemantic;
62 }
63
64 // Set up the inserthtml impl over-ride. This catches semi-paste events and
65 // tries to normalize them too.
66 if(this.editor._inserthtmlImpl){
67 this.editor._oldInsertHtmlImpl = this.editor._inserthtmlImpl;
68 }
69 this.editor._inserthtmlImpl = dojo.hitch(this, this._inserthtmlImpl);
70 },
71
72 _convertToSemantic: function(node){
73 // summary:
74 // A function to convert the HTML structure of 'node' into
75 // semantic tags where possible.
76 // node: DOMNode
77 // The node to process.
78 // tags:
79 // private
80 if(node){
81 var doc = this.editor.document;
82 var self = this;
83 var convertNode = function(cNode){
84 if(cNode.nodeType == 1){
85 if(cNode.id !== "dijitEditorBody"){
86 var style = cNode.style;
87 var tag = cNode.tagName?cNode.tagName.toLowerCase():"";
88 var sTag;
89 if(style && tag != "table" && tag != "ul" && tag != "ol"){
90 // Avoid wrapper blocks that have specific underlying structure, as injecting
91 // spans/etc there is invalid.
92 // Lets check and convert certain node/style types.
93 var fw = style.fontWeight? style.fontWeight.toLowerCase() : "";
94 var fs = style.fontStyle? style.fontStyle.toLowerCase() : "";
95 var td = style.textDecoration? style.textDecoration.toLowerCase() : "";
96 var s = style.fontSize?style.fontSize.toLowerCase() : "";
97 var bc = style.backgroundColor?style.backgroundColor.toLowerCase() : "";
98 var c = style.color?style.color.toLowerCase() : "";
99
100 var wrapNodes = function(wrap, pNode){
101 if(wrap){
102 while(pNode.firstChild){
103 wrap.appendChild(pNode.firstChild);
104 }
105 if(tag == "span" && !pNode.style.cssText){
106 // A styler tag with nothing extra in it, so lets remove it.
107 dojo.place(wrap, pNode, "before");
108 pNode.parentNode.removeChild(pNode);
109 pNode = wrap;
110 }else{
111 pNode.appendChild(wrap);
112 }
113 }
114 return pNode;
115 };
116 switch(fw){
117 case "bold":
118 case "bolder":
119 case "700":
120 case "800":
121 case "900":
122 sTag = doc.createElement("b");
123 cNode.style.fontWeight = "";
124 break;
125 }
126 cNode = wrapNodes(sTag, cNode);
127 sTag = null;
128 if(fs == "italic"){
129 sTag = doc.createElement("i");
130 cNode.style.fontStyle = "";
131 }
132 cNode = wrapNodes(sTag, cNode);
133 sTag = null;
134 if(td){
135 var da = td.split(" ");
136 var count = 0;
137 dojo.forEach(da, function(s){
138 switch(s){
139 case "underline":
140 sTag = doc.createElement("u");
141 break;
142 case "line-through":
143 sTag = doc.createElement("strike");
144 break;
145 }
146 count++;
147 if(count == da.length){
148 // Last one, clear the decor and see if we can span strip on wrap.
149 cNode.style.textDecoration = "";
150 }
151 cNode = wrapNodes(sTag, cNode);
152 sTag = null;
153 });
154
155 }
156 if(s){
157 var sizeMap = {
158 "xx-small": 1,
159 "x-small": 2,
160 "small": 3,
161 "medium": 4,
162 "large": 5,
163 "x-large": 6,
164 "xx-large": 7,
165 "-webkit-xxx-large": 7
166 };
167
168 // Convert point or px size to size
169 // to something roughly mappable.
170 if(s.indexOf("pt") > 0){
171 s = s.substring(0,s.indexOf("pt"));
172 s = parseInt(s);
173 if(s < 5){
174 s = "xx-small";
175 }else if(s < 10){
176 s = "x-small";
177 }else if(s < 15){
178 s = "small";
179 }else if(s < 20){
180 s = "medium";
181 }else if(s < 25){
182 s = "large";
183 }else if(s < 30){
184 s = "x-large";
185 }else if(s > 30){
186 s = "xx-large";
187 }
188 }else if(s.indexOf("px") > 0){
189 s = s.substring(0,s.indexOf("px"));
190 s = parseInt(s);
191 if(s < 5){
192 s = "xx-small";
193 }else if(s < 10){
194 s = "x-small";
195 }else if(s < 15){
196 s = "small";
197 }else if(s < 20){
198 s = "medium";
199 }else if(s < 25){
200 s = "large";
201 }else if(s < 30){
202 s = "x-large";
203 }else if(s > 30){
204 s = "xx-large";
205 }
206 }
207 var size = sizeMap[s];
208 if(!size){
209 size = 3;
210 }
211 sTag = doc.createElement("font");
212 sTag.setAttribute("size", size);
213 cNode.style.fontSize = "";
214 }
215 cNode = wrapNodes(sTag, cNode);
216 sTag = null;
217 if(bc && tag !== "font" && self._isInline(tag)){
218 // IE doesn't like non-font background color crud.
219 // Also, don't move it in if the background color is set on a block style node,
220 // as it won't color properly once put on inline font.
221 bc = new dojo.Color(bc).toHex();
222 sTag = doc.createElement("font");
223 sTag.style.backgroundColor = bc;
224 cNode.style.backgroundColor = "";
225 }
226 if(c && tag !== "font"){
227 // IE doesn't like non-font background color crud.
228 c = new dojo.Color(c).toHex();
229 sTag = doc.createElement("font");
230 sTag.setAttribute("color", c);
231 cNode.style.color = "";
232 }
233 cNode = wrapNodes(sTag, cNode);
234 sTag = null;
235 }
236 }
237 if(cNode.childNodes){
238 // Clone it, since we may alter its position
239 var nodes = [];
240 dojo.forEach(cNode.childNodes, function(n){ nodes.push(n);});
241 dojo.forEach(nodes, convertNode);
242 }
243 }
244 return cNode;
245 };
246 return this._normalizeTags(convertNode(node));
247 }
248 return node;
249 },
250
251 _normalizeTags: function(node){
252 // summary:
253 // A function to handle normalizing certain tag types contained under 'node'
254 // node:
255 // The node to search from.
256 // tags:
257 // Protected.
258 var w = this.editor.window;
259 var doc = this.editor.document;
260 dojo.query("em,s,strong", node).forEach(function(n){
261 var tag = n.tagName?n.tagName.toLowerCase():"";
262 var tTag;
263 switch(tag){
264 case "s":
265 tTag = "strike";
266 break;
267 case "em":
268 tTag = "i";
269 break;
270 case "strong":
271 tTag = "b";
272 break;
273 }
274 if(tTag){
275 var nNode = doc.createElement(tTag);
276 dojo.place("<"+tTag+">", n, "before");
277 while(n.firstChild){
278 nNode.appendChild(n.firstChild);
279 }
280 n.parentNode.removeChild(n);
281 }
282 });
283 return node;
284 },
285
286 _convertToCss: function(node){
287 // summary:
288 // A function to convert the HTML structure of 'node' into
289 // css span styles around text instead of semantic tags.
290 // Note: It does not do compression of spans together.
291 // node: DOMNode
292 // The node to process
293 // tags:
294 // private
295 if(node){
296 var doc = this.editor.document;
297 var convertNode = function(cNode) {
298 if(cNode.nodeType == 1){
299 if(cNode.id !== "dijitEditorBody"){
300 var tag = cNode.tagName?cNode.tagName.toLowerCase():"";
301 if(tag){
302 var span;
303 switch(tag){
304 case "b":
305 case "strong": // Mainly IE
306 span = doc.createElement("span");
307 span.style.fontWeight = "bold";
308 break;
309 case "i":
310 case "em": // Mainly IE
311 span = doc.createElement("span");
312 span.style.fontStyle = "italic";
313 break;
314 case "u":
315 span = doc.createElement("span");
316 span.style.textDecoration = "underline";
317 break;
318 case "strike":
319 case "s": // Mainly WebKit.
320 span = doc.createElement("span");
321 span.style.textDecoration = "line-through";
322 break;
323 case "font": // Try to deal with colors
324 var styles = {};
325 if(dojo.attr(cNode, "color")){
326 styles.color = dojo.attr(cNode, "color");
327 }
328 if(dojo.attr(cNode, "face")){
329 styles.fontFace = dojo.attr(cNode, "face");
330 }
331 if(cNode.style && cNode.style.backgroundColor){
332 styles.backgroundColor = cNode.style.backgroundColor;
333 }
334 if(cNode.style && cNode.style.color){
335 styles.color = cNode.style.color;
336 }
337 var sizeMap = {
338 1: "xx-small",
339 2: "x-small",
340 3: "small",
341 4: "medium",
342 5: "large",
343 6: "x-large",
344 7: "xx-large"
345 };
346 if(dojo.attr(cNode, "size")){
347 styles.fontSize = sizeMap[dojo.attr(cNode, "size")];
348 }
349 span = doc.createElement("span");
350 dojo.style(span, styles);
351 break;
352 }
353 if(span){
354 while(cNode.firstChild){
355 span.appendChild(cNode.firstChild);
356 }
357 dojo.place(span, cNode, "before");
358 cNode.parentNode.removeChild(cNode);
359 cNode = span;
360 }
361 }
362 }
363 if(cNode.childNodes){
364 // Clone it, since we may alter its position
365 var nodes = [];
366 dojo.forEach(cNode.childNodes, function(n){ nodes.push(n);});
367 dojo.forEach(nodes, convertNode);
368 }
369 }
370 return cNode;
371 };
372 node = convertNode(node);
373 if(this.condenseSpans){
374 this._condenseSpans(node);
375 }
376 }
377 return node;
378 },
379
380 _condenseSpans: function(node){
381 // summary:
382 // Method to condense spans if you end up with multi-wrapping from
383 // from converting b, i, u, to span nodes.
384 // node:
385 // The node (and its children), to process.
386 // tags:
387 // private
388 var compressSpans = function(node){
389 // Okay, span with no class or id and it has styles.
390 // So, merge the styles, then collapse. Merge requires determining
391 // all the common/different styles and anything that overlaps the style,
392 // but a different value can't be merged.
393 var genStyleMap = function(styleText){
394 var m;
395 if(styleText){
396 m = {};
397 var styles = styleText.toLowerCase().split(";");
398 dojo.forEach(styles, function(s){
399 if(s){
400 var ss = s.split(":");
401 var key = ss[0] ? dojo.trim(ss[0]): "";
402 var val = ss[1] ? dojo.trim(ss[1]): "";
403 if(key && val){
404 var i;
405 var nKey = "";
406 for(i = 0; i < key.length; i++){
407 var ch = key.charAt(i);
408 if(ch == "-"){
409 i++;
410 ch = key.charAt(i);
411 nKey += ch.toUpperCase();
412 }else{
413 nKey += ch;
414 }
415 }
416 m[nKey] = val;
417 }
418 }
419 });
420 }
421 return m;
422 };
423 if(node && node.nodeType == 1){
424 var tag = node.tagName? node.tagName.toLowerCase() : "";
425 if(tag === "span" && node.childNodes && node.childNodes.length === 1){
426 // Okay, a possibly compressible span
427 var c = node.firstChild;
428 while(c && c.nodeType == 1 && c.tagName && c.tagName.toLowerCase() == "span"){
429 if(!dojo.attr(c, "class") && !dojo.attr(c, "id") && c.style){
430 var s1 = genStyleMap(node.style.cssText);
431 var s2 = genStyleMap(c.style.cssText);
432 if(s1 && s2){
433 // Maps, so lets see if we can combine them.
434 var combinedMap = {};
435 var i;
436 for(i in s1){
437 if(!s1[i] || !s2[i] || s1[i] == s2[i]){
438 combinedMap[i] = s1[i];
439 delete s2[i];
440 }else if(s1[i] != s2[i]){
441 // Collision, cannot merge.
442 // IE does not handle combined underline strikethrough text
443 // decorations on a single span.
444 if(i == "textDecoration"){
445 combinedMap[i] = s1[i] + " " + s2[i];
446 delete s2[i];
447 }else{
448 combinedMap = null;
449 }
450 break;
451 }else{
452 combinedMap = null;
453 break;
454 }
455 }
456 if(combinedMap){
457 for(i in s2){
458 combinedMap[i] = s2[i];
459 }
460 dojo.style(node, combinedMap);
461 while(c.firstChild){
462 node.appendChild(c.firstChild);
463 }
464 var t = c.nextSibling;
465 c.parentNode.removeChild(c);
466 c = t;
467 }else{
468 c = c.nextSibling;
469 }
470 }else{
471 c = c.nextSibling;
472 }
473 }else{
474 c = c.nextSibling;
475 }
476 }
477 }
478 }
479 if(node.childNodes && node.childNodes.length){
480 dojo.forEach(node.childNodes, compressSpans);
481 }
482 };
483 compressSpans(node);
484 },
485
486 _isInline: function(tag){
487 // summary:
488 // Function to determine if the current tag is an inline
489 // element that does formatting, as we don't want to
490 // try to combine inlines with divs on styles.
491 // tag:
492 // The tag to examine
493 // tags:
494 // private
495 switch(tag){
496 case "a":
497 case "b":
498 case "strong":
499 case "s":
500 case "strike":
501 case "i":
502 case "u":
503 case "em":
504 case "sup":
505 case "sub":
506 case "span":
507 case "font":
508 case "big":
509 case "cite":
510 case "q":
511 case "img":
512 case "small":
513 return true;
514 default:
515 return false;
516 }
517 },
518
519 _inserthtmlImpl: function(html){
520 // summary:
521 // Function to trap and over-ride the editor inserthtml implementation
522 // to try and filter it to match the editor's internal styling mode.
523 // Helpful for plugins like PasteFromWord, in that it extra-filters
524 // and normalizes the input if it can.
525 // html:
526 // The HTML string to insert.
527 // tags:
528 // private
529 if(html){
530 var doc = this.editor.document;
531 var div = doc.createElement("div");
532 div.innerHTML = html;
533 div = this._browserFilter(div);
534 html = editorHtml.getChildrenHtml(div);
535 div.innerHTML = "";
536
537 // Call the over-ride, or if not available, just execute it.
538 if(this.editor._oldInsertHtmlImpl){
539 return this.editor._oldInsertHtmlImpl(html);
540 }else{
541 return this.editor.execCommand("inserthtml", html);
542 }
543 }
544 return false;
545 }
546 });
547
548 // Register this plugin.
549 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
550 if(o.plugin){ return; }
551 var name = o.args.name.toLowerCase();
552 if(name === "normalizestyle"){
553 o.plugin = new NormalizeStyle({
554 mode: ("mode" in o.args)?o.args.mode:"semantic",
555 condenseSpans: ("condenseSpans" in o.args)?o.args.condenseSpans:true
556 });
557 }
558 });
559
560 return NormalizeStyle;
561
562 });