Dotclear

source: admin/js/jsToolBar/jsToolBar.wysiwyg.js @ 0:54703be25dd6

Revision 0:54703be25dd6, 25.7 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * This file is part of DotClear.
3 * Copyright (c) 2005 Nicolas Martin & Olivier Meunier and contributors. All
4 * rights reserved.
5 *
6 * DotClear is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * DotClear is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with DotClear; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 * ***** END LICENSE BLOCK *****
21*/
22
23jsToolBar.prototype.can_wwg = (document.designMode != undefined);
24jsToolBar.prototype.iframe = null;
25jsToolBar.prototype.iwin = null;
26jsToolBar.prototype.ibody = null;
27jsToolBar.prototype.iframe_css = null;
28
29/* Editor methods
30-------------------------------------------------------- */
31jsToolBar.prototype.drawToolBar = jsToolBar.prototype.draw;
32jsToolBar.prototype.draw = function(mode) {
33     mode = mode || 'xhtml';
34     
35     if (this.can_wwg) {
36          this.mode = 'wysiwyg';
37          this.drawToolBar('wysiwyg');
38          this.initWindow();
39     } else {
40          this.drawToolBar(mode);
41     }
42};
43
44jsToolBar.prototype.switchMode = function(mode) {
45     mode = mode || 'xhtml';
46     
47     if (mode == 'xhtml') {
48          this.draw(mode);
49     } else {
50          if (this.wwg_mode) {
51               this.syncContents('iframe');
52          }
53          this.removeEditor();
54          this.textarea.style.display = '';
55          this.drawToolBar(mode);
56     }
57};
58
59jsToolBar.prototype.syncContents = function(from) {
60     from = from || 'textarea';
61     var This = this;
62     if (from == 'textarea') {
63          initContent();
64     } else {
65          this.validBlockquote();
66          var html = this.applyHtmlFilters(this.ibody.innerHTML);
67          if (html == '<br />') { html = '<p></p>'; }
68          this.textarea.value = html;
69     }
70     
71     function initContent() {
72          if (!This.iframe.contentWindow.document || !This.iframe.contentWindow.document.body) {
73               setTimeout(initContent, 1);
74               return;
75          }
76          This.ibody = This.iframe.contentWindow.document.body;
77         
78          if (This.textarea.value != '' && This.textarea.value != '<p></p>') {
79               This.ibody.innerHTML = This.applyWysiwygFilters(This.textarea.value);
80               if (This.ibody.createTextRange) { //cursor at the begin for IE
81                    var IErange = This.ibody.createTextRange();
82                    IErange.execCommand("SelectAll");
83                    IErange.collapse();
84                    IErange.select();
85               }
86          } else if (window.navigator.product != undefined && 
87                                    window.navigator.product == 'Gecko') {
88               This.ibody.innerHTML = '<p><br _moz_editor_blogus_node="TRUE" _moz_dirty=""></p>';
89          } else {
90               var idoc = This.iwin.document;
91               var para = idoc.createElement('p');
92               para.appendChild(idoc.createTextNode(''));
93               while (idoc.body.hasChildNodes()) {
94                    idoc.body.removeChild(idoc.body.lastChild);
95               }
96               idoc.body.appendChild(para);
97          }
98     }
99};
100jsToolBar.prototype.htmlFilters = {
101     tagsoup: function(str){
102          return this.tagsoup2xhtml(str);
103     }
104};
105jsToolBar.prototype.applyHtmlFilters = function(str) {
106     for (var fn in this.htmlFilters) {
107           str = this.htmlFilters[fn].call(this, str);
108     }
109     return str;
110};
111jsToolBar.prototype.wysiwygFilters = {};
112jsToolBar.prototype.applyWysiwygFilters = function( str) {
113     for (var fn in this.wysiwygFilters) {
114           str = this.wysiwygFilters[fn].call(this, str);
115     }
116     return str;
117};
118
119jsToolBar.prototype.switchEdit = function() {
120     if (this.wwg_mode) {
121          this.textarea.style.display = '';
122          this.iframe.style.display = 'none';
123          this.syncContents('iframe');
124          this.drawToolBar('xhtml');
125          this.wwg_mode = false;
126          this.focusEditor();
127     } else {
128          this.iframe.style.display = '';
129          this.textarea.style.display = 'none';
130          this.syncContents('textarea');
131          this.drawToolBar('wysiwyg');
132          this.wwg_mode = true;
133          this.focusEditor();
134     }
135     this.setSwitcher();
136};
137
138/** Creates iframe for editor, inits a blank document
139*/
140jsToolBar.prototype.initWindow = function() {
141     var This = this;
142     
143     this.iframe = document.createElement('iframe');
144     this.textarea.parentNode.insertBefore(this.iframe,this.textarea.nextSibling);
145     
146     this.switcher = document.createElement('ul');
147     this.switcher.className = 'jstSwitcher';
148     this.editor.appendChild(this.switcher);
149     
150     this.iframe.height = this.textarea.offsetHeight + 0;
151     this.iframe.width = this.textarea.offsetWidth + 0;
152     
153     if (this.textarea.tabIndex != undefined) {
154          this.iframe.tabIndex = this.textarea.tabIndex;
155     }
156     
157     function initIframe() {
158          var doc = This.iframe.contentWindow.document;
159          if (!doc) {
160               setTimeout(initIframe,1);
161               return false;
162          }
163         
164          doc.open();
165          var html =
166          '<html>\n'+
167          '<head>\n'+
168          '<style type="text/css">'+This.iframe_css+'</style>\n'+
169          (This.base_url != '' ? '<base href="'+This.base_url+'" />' : '')+
170          '</head>\n'+
171          '<body>\n'+
172          '</body>\n'+
173          '</html>';
174         
175          doc.write(html);
176          doc.close();
177          if (document.all) { // for IE
178               doc.designMode = 'on';
179               // warning : doc is now inaccessible for IE6 sp1
180          }
181         
182          This.iwin = This.iframe.contentWindow;
183         
184          This.syncContents('textarea');
185         
186          if (This.wwg_mode == undefined) {
187               This.wwg_mode = true;
188          }
189         
190          if (This.wwg_mode) {
191               This.textarea.style.display = 'none';
192          } else {
193               This.iframe.style.display = 'none';
194          }
195         
196          // update textarea on submit
197          if (This.textarea.form) {
198               chainHandler(This.textarea.form,'onsubmit', function() {
199                    if (This.wwg_mode) {
200                         This.syncContents('iframe');
201                    }
202               });
203          }
204         
205          for (var evt in This.iwinEvents) {
206               var event = This.iwinEvents[evt];
207               This.addIwinEvent(This.iframe.contentWindow.document, event.type, event.fn, This);
208          }
209         
210          This.setSwitcher();
211          setTimeout(function(){This.focusEditor();},1);
212         
213          return true;
214     }
215     initIframe();
216};
217jsToolBar.prototype.addIwinEvent = function(target, type, fn, scope) {
218     var myFn = function(e){fn.call(scope, e)};
219     addEvent(target, type, myFn, true);
220     // fix memory leak
221     addEvent(scope.iwin, 'unload', function(){
222          removeEvent(target, type, myFn, true);
223     }, true);
224};
225jsToolBar.prototype.iwinEvents = {
226     block1: {
227          type: 'mouseup',
228          fn: function(){ this.adjustBlockLevelCombo() }
229     },
230     block2: {
231          type: 'keyup',
232          fn: function(){ this.adjustBlockLevelCombo() }
233     }
234};
235
236/** Insert a mode switcher after editor area
237*/
238jsToolBar.prototype.switcher_visual_title = 'visual';
239jsToolBar.prototype.switcher_source_title = 'source';
240jsToolBar.prototype.setSwitcher = function() {
241     while (this.switcher.hasChildNodes()) {
242          this.switcher.removeChild(this.switcher.firstChild);
243     }
244     
245     var This = this;
246     function setLink(title,link) {
247          var li = document.createElement('li');
248          if (link) {
249               var a = document.createElement('a');
250               a.href = '#';
251               a.editor = This;
252               a.onclick = function() { this.editor.switchEdit(); return false; };
253               a.appendChild(document.createTextNode(title));
254          } else {
255               li.className = 'jstSwitcherCurrent';
256               a = document.createTextNode(title);
257          }
258         
259          li.appendChild(a);
260          This.switcher.appendChild(li);
261     }
262     
263     setLink(this.switcher_visual_title,!this.wwg_mode);
264     setLink(this.switcher_source_title,this.wwg_mode);
265};
266
267/** Removes editor area and mode switcher
268*/
269jsToolBar.prototype.removeEditor = function() {
270     if (this.iframe != null) {
271          this.iframe.parentNode.removeChild(this.iframe);
272          this.iframe = null;
273     }
274     
275     if (this.switcher != undefined && this.switcher.parentNode != undefined) {
276          this.switcher.parentNode.removeChild(this.switcher);
277     }
278};
279
280/** Focus on the editor area
281*/
282jsToolBar.prototype.focusEditor = function() {
283     if (this.wwg_mode) {
284          try { this.iwin.document.designMode = 'on'; } catch (e) {}; // Firefox needs this
285          var This = this;
286          setTimeout(function() {This.iframe.contentWindow.focus()},1);
287     } else {
288          this.textarea.focus();
289     }
290};
291
292/** Resizer
293*/
294jsToolBar.prototype.resizeSetStartH = function() {
295     if (this.wwg_mode && this.iframe != undefined) {
296          this.dragStartH = this.iframe.offsetHeight;
297          return;
298     }
299     this.dragStartH = this.textarea.offsetHeight + 0;
300};
301jsToolBar.prototype.resizeDragMove = function(event) {
302     var new_height = (this.dragStartH+event.clientY-this.dragStartY)+'px';
303     if (this.iframe != undefined) {
304          this.iframe.style.height = new_height;
305     }
306     this.textarea.style.height = new_height;
307};
308
309/* Editing methods
310-------------------------------------------------------- */
311/** Replaces current selection by given node
312*/
313jsToolBar.prototype.insertNode = function(node) {
314     var range;
315     
316     if (this.iwin.getSelection) { // Gecko
317          var sel = this.iwin.getSelection();
318          range = sel.getRangeAt(0);
319         
320          // deselect all ranges
321          sel.removeAllRanges();
322         
323          // empty range
324          range.deleteContents();
325         
326          // Insert node
327          range.insertNode(node);
328         
329          range.selectNodeContents(node);
330          range.setEndAfter(node);
331          if (range.endContainer.childNodes.length > range.endOffset &&
332          range.endContainer.nodeType != Node.TEXT_NODE) {
333               range.setEnd(range.endContainer.childNodes[range.endOffset], 0);
334          } else {
335               range.setEnd(range.endContainer.childNodes[0]);
336          }
337          sel.addRange(range);
338         
339          sel.collapseToEnd();
340     } else { // IE
341          // lambda element
342          var p = this.iwin.document.createElement('div');
343          p.appendChild(node);
344          range = this.iwin.document.selection.createRange();
345          range.execCommand('delete');
346          // insert innerHTML from element
347          range.pasteHTML(p.innerHTML);
348          range.collapse(false);
349          range.select();
350     }
351     this.iwin.focus();
352};
353
354/** Returns a document fragment with selected nodes
355*/
356jsToolBar.prototype.getSelectedNode = function() {
357     if (this.iwin.getSelection) { // Gecko
358          var sel = this.iwin.getSelection();
359          var range = sel.getRangeAt(0);
360          var content = range.cloneContents();
361     } else { // IE
362          var sel = this.iwin.document.selection;
363          var d = this.iwin.document.createElement('div');
364          d.innerHTML = sel.createRange().htmlText;
365          var content = this.iwin.document.createDocumentFragment();
366          for (var i=0; i < d.childNodes.length; i++) {
367               content.appendChild(d.childNodes[i].cloneNode(true));
368          }
369     }
370     return content;
371};
372
373/** Returns string representation for selected node
374*/
375jsToolBar.prototype.getSelectedText = function() {
376     if (this.iwin.getSelection) { // Gecko
377          return this.iwin.getSelection().toString();
378     } else { // IE
379          var range = this.iwin.document.selection.createRange();
380          return range.text;
381     }
382};
383
384jsToolBar.prototype.replaceNodeByContent = function(node) {
385     var content = this.iwin.document.createDocumentFragment();
386     for (var i=0; i < node.childNodes.length; i++) {
387          content.appendChild(node.childNodes[i].cloneNode(true));
388     }
389     node.parentNode.replaceChild(content, node);
390};
391
392jsToolBar.prototype.getBlockLevel = function() {
393     var blockElts = ['p','h1','h2','h3','h4','h5','h6'];
394     
395     var range, commonAncestorContainer;
396     if (this.iwin.getSelection) { //gecko
397          var selection = this.iwin.getSelection();
398          range = selection.getRangeAt(0);
399          commonAncestorContainer = range.commonAncestorContainer;
400          while (commonAncestorContainer.nodeType != 1) {
401               commonAncestorContainer = commonAncestorContainer.parentNode;
402          }
403     } else { //ie
404          range = this.iwin.document.selection.createRange();
405          commonAncestorContainer = range.parentElement();
406     }
407     
408     var ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
409     while (arrayIndexOf(blockElts, ancestorTagName)==-1 && ancestorTagName!='body') {
410          commonAncestorContainer = commonAncestorContainer.parentNode;
411          ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
412     }
413     if (ancestorTagName == 'body') return null;
414     else return commonAncestorContainer;
415};
416jsToolBar.prototype.adjustBlockLevelCombo = function() {
417     var blockLevel = this.getBlockLevel();
418     if (blockLevel !== null)
419          this.toolNodes.blocks.value = blockLevel.tagName.toLowerCase();
420     else {
421          if (this.mode == 'wysiwyg') this.toolNodes.blocks.value = 'none';
422          if (this.mode == 'xhtml') this.toolNodes.blocks.value = 'nonebis';
423     }
424};
425
426/** HTML code cleanup
427-------------------------------------------------------- */
428jsToolBar.prototype.simpleCleanRegex = new Array(
429     /* Remove every tags we don't need */
430     [/<meta[\w\W]*?>/gim,''],
431     [/<style[\w\W]*?>[\w\W]*?<\/style>/gim, ''],
432     [/<\/?font[\w\W]*?>/gim, ''],
433     
434     
435     /* Replacements */
436     [/<(\/?)(B|b|STRONG)([\s>\/])/g, "<$1strong$3"],
437     [/<(\/?)(I|i|EM)([\s>\/])/g, "<$1em$3"],
438     [/<IMG ([^>]*?[^\/])>/gi, "<img $1 />"],
439     [/<INPUT ([^>]*?[^\/])>/gi, "<input $1 />"],
440     [/<COL ([^>]*?[^\/])>/gi, "<col $1 />"],
441     [/<AREA ([^>]*?[^\/])>/gi, "<area $1 />"],
442     [/<PARAM ([^>]*?[^\/])>/gi, "<param $1 />"],
443     [/<HR ([^>]*?[^\/])>/gi, "<hr $1/>"],
444     [/<BR ([^>]*?[^\/])>/gi, "<br $1/>"],
445     [/<(\/?)U([\s>\/])/gi, "<$1ins$2"],
446     [/<(\/?)STRIKE([\s>\/])/gi, "<$1del$2"],
447     [/<span style="font-weight: normal;">([\w\W]*?)<\/span>/gm, "$1"],
448     [/<span style="font-weight: bold;">([\w\W]*?)<\/span>/gm, "<strong>$1</strong>"],
449     [/<span style="font-style: italic;">([\w\W]*?)<\/span>/gm, "<em>$1</em>"],
450     [/<span style="text-decoration: underline;">([\w\W]*?)<\/span>/gm, "<ins>$1</ins>"],
451     [/<span style="text-decoration: line-through;">([\w\W]*?)<\/span>/gm, "<del>$1</del>"],
452     [/<span style="text-decoration: underline line-through;">([\w\W]*?)<\/span>/gm, "<del><ins>$1</ins></del>"],
453     [/<span style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/span>/gm, "<strong><em>$2</em></strong>"],
454     [/<span style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><strong>$2</strong></ins>"],
455     [/<span style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/span>/gm, "<ins><em>$2</em></ins>"],
456     [/<span style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><strong>$2</strong></del>"],
457     [/<span style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/span>/gm, "<del><em>$2</em></del>"],
458     [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/span>/gm, "<ins><strong><em>$2</em></strong></ins>"],
459     [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><strong><em>$2</em></strong></del>"],
460     [/<span style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/span>/gm, "<del><ins><strong><em>$2</em></strong></ins></del>"],
461     [/<strong style="font-weight: normal;">([\w\W]*?)<\/strong>/gm, "$1"],
462     [/<([a-z]+) style="font-weight: normal;">([\w\W]*?)<\/\1>/gm, "<$1>$2</$1>"],
463     [/<([a-z]+) style="font-weight: bold;">([\w\W]*?)<\/\1>/gm, "<$1><strong>$2</strong></$1>"],
464     [/<([a-z]+) style="font-style: italic;">([\w\W]*?)<\/\1>/gm, "<$1><em>$2</em></$1>"],
465     [/<([a-z]+) style="text-decoration: underline;">([\w\W]*?)<\/\1>/gm, "<ins><$1>$2</$1></ins>"],
466     [/<([a-z]+) style="text-decoration: line-through;">([\w\W]*?)<\/\1>/gm, "<del><$1>$2</$1></del>"],
467     [/<([a-z]+) style="text-decoration: underline line-through;">([\w\W]*?)<\/\1>/gm, "<del><ins><$1>$2</$1></ins></del>"],
468     [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?){2}">([\w\W]*?)<\/\1>/gm, "<$1><strong><em>$3</em></strong></$1>"],
469     [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong>$3</strong></$1></ins>"],
470     [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: underline; ?){2}">([\w\W]*?)<\/\1>/gm, "<ins><$1><em>$3</em></$1></ins>"],
471     [/<([a-z]+) style="(font-weight: bold; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong>$3</strong></$1></del>"],
472     [/<([a-z]+) style="(font-weight: italic; ?|text-decoration: line-through; ?){2}">([\w\W]*?)<\/\1>/gm, "<del><$1><em>$3</em></$1></del>"],
473     [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline; ?){3}">([\w\W]*?)<\/\1>/gm, "<ins><$1><strong><em>$3</em></strong></$1></ins>"],
474     [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><$1><strong><em>$3</em></strong></$1></del>"],
475     [/<([a-z]+) style="(font-weight: bold; ?|font-style: italic; ?|text-decoration: underline line-through; ?){3}">([\w\W]*?)<\/\1>/gm, "<del><ins><$1><strong><em>$3</em></strong></$1></ins></del>"],
476     [/<p><blockquote>(.*)(\n)+<\/blockquote><\/p>/i,"<blockquote>$1</blockquote>\n"],
477     /* mise en forme identique contigue */
478     [/<\/(strong|em|ins|del|q|code)>(\s*?)<\1>/gim, "$2"],
479     [/<(br|BR)>/g, "<br />"],
480     /* opera est trop strict ;)) */
481     [/([^\s])\/>/g, "$1 />"],
482     /* br intempestifs de fin de block */
483     [/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote|div)/gi, "</$1"],
484     [/<\/(h1|h2|h3|h4|h5|h6|ul|ol|li|p|blockquote)>([^\n\u000B\r\f])/gi, "</$1>\n$2"],
485     [/<hr style="width: 100%; height: 2px;" \/>/g, "<hr />"]
486);
487
488/** Cleanup HTML code
489*/
490jsToolBar.prototype.tagsoup2xhtml = function(html) {
491     for (var reg in this.simpleCleanRegex) {
492          html = html.replace(this.simpleCleanRegex[reg][0], this.simpleCleanRegex[reg][1]);
493     }
494     /* tags vides */
495     /* note : on tente de ne pas tenir compte des commentaires html, ceux-ci
496        permettent entre autre d'inserer des commentaires conditionnels pour ie */
497     while ( /(<[^\/!]>|<[^\/!][^>]*[^\/]>)\s*<\/[^>]*[^-]>/.test(html) ) {
498          html = html.replace(/(<[^\/!]>|<[^\/!][^>]*[^\/]>)\s*<\/[^>]*[^-]>/g, "");
499     }
500     
501     /* tous les tags en minuscule */
502     html = html.replace(/<(\/?)([A-Z0-9]+)/g,
503               function(match0, match1, match2) {
504                    return "<" + match1 + match2.toLowerCase();
505               });
506     
507     /* IE laisse souvent des attributs sans guillemets */
508     var myRegexp = /<[^>]+((\s+\w+\s*=\s*)([^"'][\w~@+$,%\/:.#?=&;!*()-]*))[^>]*?>/;
509     while ( myRegexp.test(html)) {
510          html = html.replace(
511               myRegexp,
512               function (str, val1, val2, val3){
513                    var tamponRegex = new RegExp(regexpEscape(val1));
514                    return str.replace(tamponRegex, val2+'"'+val3+'"');
515               }
516          )
517     }
518     
519     /* les navigateurs rajoutent une unite aux longueurs css nulles */
520     /* note: a ameliorer ! */
521     while ( /(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/.test(html)) {
522          html = html.replace(/(<[^>]+style=(["'])[^>]+[\s:]+)0(pt|px)(\2|\s|;)/gi, "$1"+"0$4");
523     }
524     
525     /* correction des fins de lignes : le textarea edite contient des \n
526     * le wysiwyg des \r\n , et le textarea mis a jour SANS etre affiche des \r\n ! */
527     html = html.replace(/\r\n/g,"\n");
528     
529     /* Trim */
530     html = html.replace(/^\s+/gm,'');
531     html = html.replace(/\s+$/gm,'');
532     
533     return html;
534};
535jsToolBar.prototype.validBlockquote = function() {
536     var blockElts = ['address','blockquote','dl','div','fieldset','form','h1',
537                      'h2','h3','h4','h5','h6','hr','ol','p','pre','table','ul'];
538     var BQs = this.iwin.document.getElementsByTagName('blockquote');
539     var bqChilds;
540     
541     for (var bq = 0; bq < BQs.length; bq++) {
542          bqChilds = BQs[bq].childNodes;
543          var frag = this.iwin.document.createDocumentFragment();
544          for (var i = (bqChilds.length-1); i >= 0; i--) {
545               if (bqChilds[i].nodeType == 1 && // Node.ELEMENT_NODE
546                   arrayIndexOf(blockElts, bqChilds[i].tagName.toLowerCase()) >= 0)
547               {
548                    if (frag.childNodes.length > 0) {
549                         var p = this.iwin.document.createElement('p');
550                         p.appendChild(frag);
551                         BQs[bq].replaceChild(p, bqChilds[i+1]);
552                         frag = this.iwin.document.createDocumentFragment();
553                    }
554               } else {
555                    if (frag.childNodes.length > 0) BQs[bq].removeChild(bqChilds[i+1]);
556                    frag.insertBefore(bqChilds[i].cloneNode(true), frag.firstChild);
557               }
558          }
559          if (frag.childNodes.length > 0) {
560               var p = this.iwin.document.createElement('p');
561               p.appendChild(frag);
562               BQs[bq].replaceChild(p, bqChilds[0]);
563          }
564     }
565};
566
567/* Removing text formating */
568jsToolBar.prototype.removeFormatRegexp = new Array(
569     [/(<[a-z][^>]*)margin\s*:[^;]*;/mg, "$1"],
570     [/(<[a-z][^>]*)margin-bottom\s*:[^;]*;/mg, "$1"],
571     [/(<[a-z][^>]*)margin-left\s*:[^;]*;/mg, "$1"],
572     [/(<[a-z][^>]*)margin-right\s*:[^;]*;/mg, "$1"],
573     [/(<[a-z][^>]*)margin-top\s*:[^;]*;/mg, "$1"],
574     
575     [/(<[a-z][^>]*)padding\s*:[^;]*;/mg, "$1"],
576     [/(<[a-z][^>]*)padding-bottom\s*:[^;]*;/mg, "$1"],
577     [/(<[a-z][^>]*)padding-left\s*:[^;]*;/mg, "$1"],
578     [/(<[a-z][^>]*)padding-right\s*:[^;]*;/mg, "$1"],
579     [/(<[a-z][^>]*)padding-top\s*:[^;]*;/mg, "$1"],
580     
581     [/(<[a-z][^>]*)font\s*:[^;]*;/mg, "$1"],
582     [/(<[a-z][^>]*)font-family\s*:[^;]*;/mg, "$1"],
583     [/(<[a-z][^>]*)font-size\s*:[^;]*;/mg, "$1"],
584     [/(<[a-z][^>]*)font-style\s*:[^;]*;/mg, "$1"],
585     [/(<[a-z][^>]*)font-variant\s*:[^;]*;/mg, "$1"],
586     [/(<[a-z][^>]*)font-weight\s*:[^;]*;/mg, "$1"],
587     
588     [/(<[a-z][^>]*)color\s*:[^;]*;/mg, "$1"]
589);
590
591jsToolBar.prototype.removeTextFormating = function(html) {
592     for (var reg in this.removeFormatRegexp) {
593          html = html.replace(this.removeFormatRegexp[reg][0], this.removeFormatRegexp[reg][1]);
594     }
595     
596     html = this.tagsoup2xhtml(html);
597     html = html.replace(/style="\s*?"/mgi,'');
598     return html;
599};
600
601/** Toolbar elements
602-------------------------------------------------------- */
603jsToolBar.prototype.elements.blocks.wysiwyg = {
604     list: ['none','p','h1','h2','h3','h4','h5','h6'],
605     fn: function(opt) {
606          if (opt=='none') {
607               var blockLevel = this.getBlockLevel();
608               if (blockLevel !== null) {
609                    this.replaceNodeByContent(blockLevel);
610               }
611               this.iwin.focus();
612          } else {
613               try { this.iwin.document.execCommand('formatblock',false,'<'+opt+'>');
614               } catch(e){};
615               this.iwin.focus();
616          }
617     }
618};
619
620jsToolBar.prototype.elements.strong.fn.wysiwyg = function() {
621     this.iwin.document.execCommand('bold', false, null);
622     this.iwin.focus();
623};
624
625jsToolBar.prototype.elements.em.fn.wysiwyg = function() {
626     this.iwin.document.execCommand('italic', false, null);
627     this.iwin.focus();
628};
629
630jsToolBar.prototype.elements.ins.fn.wysiwyg = function() {
631     this.iwin.document.execCommand('underline', false, null);
632     this.iwin.focus();
633};
634
635jsToolBar.prototype.elements.del.fn.wysiwyg = function() {
636     this.iwin.document.execCommand('strikethrough', false, null);
637     this.iwin.focus();
638};
639
640jsToolBar.prototype.elements.quote.fn.wysiwyg = function() {
641     var n = this.getSelectedNode();
642     var q = this.iwin.document.createElement('q');
643     q.appendChild(n);
644     this.insertNode(q);
645};
646
647jsToolBar.prototype.elements.code.fn.wysiwyg = function() {
648     var n = this.getSelectedNode();
649     var code = this.iwin.document.createElement('code');
650     code.appendChild(n);
651     this.insertNode(code);
652};
653
654jsToolBar.prototype.elements.br.fn.wysiwyg = function() {
655     var n = this.iwin.document.createElement('br');
656     this.insertNode(n);
657};
658
659jsToolBar.prototype.elements.blockquote.fn.wysiwyg = function() {
660     var n = this.getSelectedNode();
661     var q = this.iwin.document.createElement('blockquote');
662     q.appendChild(n);
663     this.insertNode(q);
664};
665
666jsToolBar.prototype.elements.pre.fn.wysiwyg = function() {
667     this.iwin.document.execCommand('formatblock',false,'<pre>');
668     this.iwin.focus();
669};
670
671jsToolBar.prototype.elements.ul.fn.wysiwyg = function() {
672     this.iwin.document.execCommand('insertunorderedlist',false,null);
673     this.iwin.focus();
674};
675
676jsToolBar.prototype.elements.ol.fn.wysiwyg = function() {
677     this.iwin.document.execCommand('insertorderedlist',false,null);
678     this.iwin.focus();
679};
680
681jsToolBar.prototype.elements.link.fn.wysiwyg = function() {
682     var href, hreflang;
683     var range, commonAncestorContainer;
684     if (this.iwin.getSelection) { //gecko
685          var selection = this.iwin.getSelection();
686          range = selection.getRangeAt(0);
687          commonAncestorContainer = range.commonAncestorContainer;
688          while (commonAncestorContainer.nodeType != 1) {
689               commonAncestorContainer = commonAncestorContainer.parentNode;
690          }
691     } else { //ie
692          range = this.iwin.document.selection.createRange();
693          commonAncestorContainer = range.parentElement();
694     }
695     
696     var ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
697     while (ancestorTagName!='a' && ancestorTagName!='body') {
698          commonAncestorContainer = commonAncestorContainer.parentNode;
699          ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
700     }
701     
702     // Update or remove link?
703     if (ancestorTagName == 'a') {
704          href = commonAncestorContainer.href || '';
705          hreflang = commonAncestorContainer.hreflang || '';
706     }
707     
708     href = window.prompt(this.elements.link.href_prompt,href);
709     
710     // Remove link
711     if (ancestorTagName == 'a' && href=='') {
712          this.replaceNodeByContent(commonAncestorContainer);
713     }
714     if (!href) return; // user cancel
715     
716     hreflang = window.prompt(this.elements.link.hreflang_prompt, hreflang);
717     
718     // Update link
719     if (ancestorTagName == 'a' && href) {
720          commonAncestorContainer.setAttribute('href', href);
721          if (hreflang) {
722               commonAncestorContainer.setAttribute('hreflang', hreflang);
723          } else {
724               commonAncestorContainer.removeAttribute('hreflang');
725          }
726          return;
727     }
728     
729     // Create link
730     var n = this.getSelectedNode();
731     var a = this.iwin.document.createElement('a');
732     a.href = href;
733     if (hreflang) a.setAttribute('hreflang',hreflang);
734     a.appendChild(n);
735     this.insertNode(a);
736};
737
738
739
740// Remove format and Toggle
741jsToolBar.prototype.elements.removeFormat = {
742     type: 'button',
743     title: 'Remove text formating',
744     fn: {}
745};
746jsToolBar.prototype.elements.removeFormat.disabled = !jsToolBar.prototype.can_wwg;
747jsToolBar.prototype.elements.removeFormat.fn.xhtml = function() {
748     var html = this.textarea.value;
749     html = this.removeTextFormating(html);
750     this.textarea.value = html;
751};
752jsToolBar.prototype.elements.removeFormat.fn.wysiwyg = function() {
753     var html = this.iwin.document.body.innerHTML;
754     html = this.removeTextFormating(html);
755     this.iwin.document.body.innerHTML = html;
756};
757/** Utilities
758-------------------------------------------------------- */
759function arrayIndexOf(aArray, aValue){
760     if (typeof Array.indexOf == 'function') {
761          return aArray.indexOf(aValue);
762     } else {
763          var index = -1;
764          var l = aArray.length;
765          for (var i = 0; i < l ; i++) {
766               if (aArray[i] === aValue) {
767                    index = i;
768                    break;
769               }
770          }
771          return index;
772     }
773};
774function addEvent(obj, evType, fn, useCapture) {
775     if (obj.addEventListener){
776          obj.addEventListener(evType, fn, useCapture);
777          return true;
778     } else if (obj.attachEvent){
779          var r = obj.attachEvent("on"+evType, fn);
780          return r;
781     } else {
782          return false;
783     }
784};
785function removeEvent(obj, evType, fn, useCapture) {
786     if (obj.removeEventListener){
787          obj.removeEventListener(evType, fn, useCapture);
788          return true;
789     } else if (obj.detachEvent){
790          var r = obj.detachEvent("on"+evType, fn);
791          return r;
792     } else {
793          return false;
794  }
795};
796function regexpEscape(s) {
797     return s.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1")
798};
Note: See TracBrowser for help on using the repository browser.

Sites map