Line # Revision Author
1 3 ahitrov@rambler.ru /**
2 * $Id: editor_plugin_src.js 264 2007-04-26 20:53:09Z spocke $
3 *
4 * @author Moxiecode
5 * @copyright Copyright � 2004-2008, Moxiecode Systems AB, All rights reserved.
6 */
7
8 (function() {
9 var Event = tinymce.dom.Event, grep = tinymce.grep, each = tinymce.each, inArray = tinymce.inArray, isOldWebKit = tinymce.isOldWebKit;
10
11 function isEmpty(d, e, f) {
12 var w, n;
13
14 w = d.createTreeWalker(e, NodeFilter.SHOW_ALL, null, false);
15 while (n = w.nextNode()) {
16 // Filter func
17 if (f) {
18 if (!f(n))
19 return false;
20 }
21
22 // Non whitespace text node
23 if (n.nodeType == 3 && n.nodeValue && /[^\s\u00a0]+/.test(n.nodeValue))
24 return false;
25
26 // Is non text element byt still content
27 if (n.nodeType == 1 && /^(HR|IMG|TABLE)$/.test(n.nodeName))
28 return false;
29 }
30
31 return true;
32 };
33
34 tinymce.create('tinymce.plugins.Safari', {
35 init : function(ed) {
36 var t = this, dom;
37
38 // Ignore on non webkit
39 if (!tinymce.isWebKit)
40 return;
41
42 t.editor = ed;
43 t.webKitFontSizes = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', '-webkit-xxx-large'];
44 t.namedFontSizes = ['xx-small', 'x-small','small','medium','large','x-large', 'xx-large'];
45
46 // Safari CreateLink command will not work correctly on images that is aligned
47 ed.addCommand('CreateLink', function(u, v) {
48 var n = ed.selection.getNode(), dom = ed.dom, a;
49
50 if (n && (/^(left|right)$/i.test(dom.getStyle(n, 'float', 1)) || /^(left|right)$/i.test(dom.getAttrib(n, 'align')))) {
51 a = dom.create('a', {href : v}, n.cloneNode());
52 n.parentNode.replaceChild(a, n);
53 ed.selection.select(a);
54 } else
55 ed.getDoc().execCommand("CreateLink", false, v);
56 });
57
58 ed.onPaste.add(function(ed, e) {
59 function removeStyles(e) {
60 e = e.target;
61
62 if (e.nodeType == 1) {
63 e.style.cssText = '';
64
65 each(ed.dom.select('*', e), function(e) {
66 e.style.cssText = '';
67 });
68 }
69 };
70
71 Event.add(ed.getDoc(), 'DOMNodeInserted', removeStyles);
72
73 window.setTimeout(function() {
74 Event.remove(ed.getDoc(), 'DOMNodeInserted', removeStyles);
75 }, 0);
76 });
77
78 ed.onKeyUp.add(function(ed, e) {
79 var h, b;
80
81 // If backspace or delete key
82 if (e.keyCode == 46 || e.keyCode == 8) {
83 b = ed.getBody();
84 h = b.innerHTML;
85
86 // If there is no text content or images or hr elements then remove everything
87 if (b.childNodes.length == 1 && !/<(img|hr)/.test(h) && tinymce.trim(h.replace(/<[^>]+>/g, '')).length == 0)
88 ed.setContent('', {format : 'raw'});
89 }
90 });
91
92 // Workaround for FormatBlock bug, http://bugs.webkit.org/show_bug.cgi?id=16004
93 ed.addCommand('FormatBlock', function(u, v) {
94 var dom = ed.dom, e = dom.getParent(ed.selection.getNode(), dom.isBlock);
95
96 if (e)
97 dom.replace(dom.create(v), e, 1);
98 else
99 ed.getDoc().execCommand("FormatBlock", false, v);
100 });
101
102 // Workaround for InsertHTML bug, http://bugs.webkit.org/show_bug.cgi?id=16382
103 ed.addCommand('mceInsertContent', function(u, v) {
104 ed.getDoc().execCommand("InsertText", false, 'mce_marker');
105 ed.getBody().innerHTML = ed.getBody().innerHTML.replace(/mce_marker/g, ed.dom.processHTML(v) + '<span id="_mce_tmp">XX</span>');
106 ed.selection.select(ed.dom.get('_mce_tmp'));
107 ed.getDoc().execCommand("Delete", false, ' ');
108 });
109
110 ed.onKeyPress.add(function(ed, e) {
111 var se, li, lic, r1, r2, n, sel, doc, be, af, pa;
112
113 if (e.keyCode == 13) {
114 sel = ed.selection;
115 se = sel.getNode();
116
117 // Workaround for missing shift+enter support, http://bugs.webkit.org/show_bug.cgi?id=16973
118 if (e.shiftKey || ed.settings.force_br_newlines && se.nodeName != 'LI') {
119 t._insertBR(ed);
120 Event.cancel(e);
121 }
122
123 // Workaround for DIV elements produced by Safari
124 if (li = dom.getParent(se, 'LI')) {
125 lic = dom.getParent(li, 'OL,UL');
126 doc = ed.getDoc();
127
128 pa = dom.create('p');
129 dom.add(pa, 'br', {mce_bogus : "1"});
130
131 if (isEmpty(doc, li)) {
132 // If list in list then use browser default behavior
133 if (n = dom.getParent(lic.parentNode, 'LI,OL,UL'))
134 return;
135
136 n = dom.getParent(lic, 'p,h1,h2,h3,h4,h5,h6,div') || lic;
137
138 // Create range from the start of block element to the list item
139 r1 = doc.createRange();
140 r1.setStartBefore(n);
141 r1.setEndBefore(li);
142
143 // Create range after the list to the end of block element
144 r2 = doc.createRange();
145 r2.setStartAfter(li);
146 r2.setEndAfter(n);
147
148 be = r1.cloneContents();
149 af = r2.cloneContents();
150
151 if (!isEmpty(doc, af))
152 dom.insertAfter(af, n);
153
154 dom.insertAfter(pa, n);
155
156 if (!isEmpty(doc, be))
157 dom.insertAfter(be, n);
158
159 dom.remove(n);
160
161 n = pa.firstChild;
162 r1 = doc.createRange();
163 r1.setStartBefore(n);
164 r1.setEndBefore(n);
165 sel.setRng(r1);
166
167 return Event.cancel(e);
168 }
169 }
170 }
171 });
172
173 // Safari doesn't place lists outside block elements
174 ed.onExecCommand.add(function(ed, cmd) {
175 var sel, dom, bl, bm;
176
177 if (cmd == 'InsertUnorderedList' || cmd == 'InsertOrderedList') {
178 sel = ed.selection;
179 dom = ed.dom;
180
181 if (bl = dom.getParent(sel.getNode(), function(n) {return /^(H[1-6]|P|ADDRESS|PRE)$/.test(n.nodeName);})) {
182 bm = sel.getBookmark();
183 dom.remove(bl, 1);
184 sel.moveToBookmark(bm);
185 }
186 }
187 });
188
189 // Workaround for bug, http://bugs.webkit.org/show_bug.cgi?id=12250
190 ed.onClick.add(function(ed, e) {
191 e = e.target;
192
193 if (e.nodeName == 'IMG') {
194 t.selElm = e;
195 ed.selection.select(e);
196 } else
197 t.selElm = null;
198 });
199
200 ed.onInit.add(function() {
201 t._fixWebKitSpans();
202
203 if (isOldWebKit)
204 t._patchSafari2x(ed);
205 });
206
207 ed.onSetContent.add(function() {
208 dom = ed.dom;
209
210 // Convert strong,b,em,u,strike to spans
211 each(['strong','b','em','u','strike','sub','sup','a'], function(v) {
212 each(grep(dom.select(v)).reverse(), function(n) {
213 var nn = n.nodeName.toLowerCase(), st;
214
215 // Convert anchors into images
216 if (nn == 'a') {
217 if (n.name)
218 dom.replace(dom.create('img', {mce_name : 'a', name : n.name, 'class' : 'mceItemAnchor'}), n);
219
220 return;
221 }
222
223 switch (nn) {
224 case 'b':
225 case 'strong':
226 if (nn == 'b')
227 nn = 'strong';
228
229 st = 'font-weight: bold;';
230 break;
231
232 case 'em':
233 st = 'font-style: italic;';
234 break;
235
236 case 'u':
237 st = 'text-decoration: underline;';
238 break;
239
240 case 'sub':
241 st = 'vertical-align: sub;';
242 break;
243
244 case 'sup':
245 st = 'vertical-align: super;';
246 break;
247
248 case 'strike':
249 st = 'text-decoration: line-through;';
250 break;
251 }
252
253 dom.replace(dom.create('span', {mce_name : nn, style : st, 'class' : 'Apple-style-span'}), n, 1);
254 });
255 });
256 });
257
258 ed.onPreProcess.add(function(ed, o) {
259 dom = ed.dom;
260
261 each(grep(o.node.getElementsByTagName('span')).reverse(), function(n) {
262 var v, bg;
263
264 if (o.get) {
265 if (dom.hasClass(n, 'Apple-style-span')) {
266 bg = n.style.backgroundColor;
267
268 switch (dom.getAttrib(n, 'mce_name')) {
269 case 'font':
270 if (!ed.settings.convert_fonts_to_spans)
271 dom.setAttrib(n, 'style', '');
272 break;
273
274 case 'strong':
275 case 'em':
276 case 'sub':
277 case 'sup':
278 dom.setAttrib(n, 'style', '');
279 break;
280
281 case 'strike':
282 case 'u':
283 if (!ed.settings.inline_styles)
284 dom.setAttrib(n, 'style', '');
285 else
286 dom.setAttrib(n, 'mce_name', '');
287
288 break;
289
290 default:
291 if (!ed.settings.inline_styles)
292 dom.setAttrib(n, 'style', '');
293 }
294
295
296 if (bg)
297 n.style.backgroundColor = bg;
298 }
299 }
300
301 if (dom.hasClass(n, 'mceItemRemoved'))
302 dom.remove(n, 1);
303 });
304 });
305
306 ed.onPostProcess.add(function(ed, o) {
307 // Safari adds BR at end of all block elements
308 o.content = o.content.replace(/<br \/><\/(h[1-6]|div|p|address|pre)>/g, '</$1>');
309
310 // Safari adds id="undefined" to HR elements
311 o.content = o.content.replace(/ id=\"undefined\"/g, '');
312 });
313 },
314
315 getInfo : function() {
316 return {
317 longname : 'Safari compatibility',
318 author : 'Moxiecode Systems AB',
319 authorurl : 'http://tinymce.moxiecode.com',
320 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',
321 version : tinymce.majorVersion + "." + tinymce.minorVersion
322 };
323 },
324
325 // Internal methods
326
327 _fixWebKitSpans : function() {
328 var t = this, ed = t.editor;
329
330 if (!isOldWebKit) {
331 // Use mutator events on new WebKit
332 Event.add(ed.getDoc(), 'DOMNodeInserted', function(e) {
333 e = e.target;
334
335 if (e && e.nodeType == 1)
336 t._fixAppleSpan(e);
337 });
338 } else {
339 // Do post command processing in old WebKit since the browser crashes on Mutator events :(
340 ed.onExecCommand.add(function() {
341 each(ed.dom.select('span'), function(n) {
342 t._fixAppleSpan(n);
343 });
344
345 ed.nodeChanged();
346 });
347 }
348 },
349
350 _fixAppleSpan : function(e) {
351 var ed = this.editor, dom = ed.dom, fz = this.webKitFontSizes, fzn = this.namedFontSizes, s = ed.settings, st, p;
352
353 if (dom.getAttrib(e, 'mce_fixed'))
354 return;
355
356 // Handle Apple style spans
357 if (e.nodeName == 'SPAN' && e.className == 'Apple-style-span') {
358 st = e.style;
359
360 if (!s.convert_fonts_to_spans) {
361 if (st.fontSize) {
362 dom.setAttrib(e, 'mce_name', 'font');
363 dom.setAttrib(e, 'size', inArray(fz, st.fontSize) + 1);
364 }
365
366 if (st.fontFamily) {
367 dom.setAttrib(e, 'mce_name', 'font');
368 dom.setAttrib(e, 'face', st.fontFamily);
369 }
370
371 if (st.color) {
372 dom.setAttrib(e, 'mce_name', 'font');
373 dom.setAttrib(e, 'color', dom.toHex(st.color));
374 }
375
376 if (st.backgroundColor) {
377 dom.setAttrib(e, 'mce_name', 'font');
378 dom.setStyle(e, 'background-color', st.backgroundColor);
379 }
380 } else {
381 if (st.fontSize)
382 dom.setStyle(e, 'fontSize', fzn[inArray(fz, st.fontSize)]);
383 }
384
385 if (st.fontWeight == 'bold')
386 dom.setAttrib(e, 'mce_name', 'strong');
387
388 if (st.fontStyle == 'italic')
389 dom.setAttrib(e, 'mce_name', 'em');
390
391 if (st.textDecoration == 'underline')
392 dom.setAttrib(e, 'mce_name', 'u');
393
394 if (st.textDecoration == 'line-through')
395 dom.setAttrib(e, 'mce_name', 'strike');
396
397 if (st.verticalAlign == 'super')
398 dom.setAttrib(e, 'mce_name', 'sup');
399
400 if (st.verticalAlign == 'sub')
401 dom.setAttrib(e, 'mce_name', 'sub');
402
403 dom.setAttrib(e, 'mce_fixed', '1');
404 }
405 },
406
407 _patchSafari2x : function(ed) {
408 var t = this, setContent, getNode, dom = ed.dom, lr;
409
410 // Inline dialogs
411 if (ed.windowManager.onBeforeOpen) {
412 ed.windowManager.onBeforeOpen.add(function() {
413 r = ed.selection.getRng();
414 });
415 }
416
417 // Fake select on 2.x
418 ed.selection.select = function(n) {
419 this.getSel().setBaseAndExtent(n, 0, n, 1);
420 };
421
422 getNode = ed.selection.getNode;
423 ed.selection.getNode = function() {
424 return t.selElm || getNode.call(this);
425 };
426
427 // Fake range on Safari 2.x
428 ed.selection.getRng = function() {
429 var t = this, s = t.getSel(), d = ed.getDoc(), r, rb, ra, di;
430
431 // Fake range on Safari 2.x
432 if (s.anchorNode) {
433 r = d.createRange();
434
435 try {
436 // Setup before range
437 rb = d.createRange();
438 rb.setStart(s.anchorNode, s.anchorOffset);
439 rb.collapse(1);
440
441 // Setup after range
442 ra = d.createRange();
443 ra.setStart(s.focusNode, s.focusOffset);
444 ra.collapse(1);
445
446 // Setup start/end points by comparing locations
447 di = rb.compareBoundaryPoints(rb.START_TO_END, ra) < 0;
448 r.setStart(di ? s.anchorNode : s.focusNode, di ? s.anchorOffset : s.focusOffset);
449 r.setEnd(di ? s.focusNode : s.anchorNode, di ? s.focusOffset : s.anchorOffset);
450
451 lr = r;
452 } catch (ex) {
453 // Sometimes fails, at least we tried to do it by the book. I hope Safari 2.x will go disappear soooon!!!
454 }
455 }
456
457 return r || lr;
458 };
459
460 // Fix setContent so it works
461 setContent = ed.selection.setContent;
462 ed.selection.setContent = function(h, s) {
463 var r = this.getRng(), b;
464
465 try {
466 setContent.call(this, h, s);
467 } catch (ex) {
468 // Workaround for Safari 2.x
469 b = dom.create('body');
470 b.innerHTML = h;
471
472 each(b.childNodes, function(n) {
473 r.insertNode(n.cloneNode(true));
474 });
475 }
476 };
477 },
478
479 _insertBR : function(ed) {
480 var dom = ed.dom, s = ed.selection, r = s.getRng(), br;
481
482 // Insert BR element
483 r.insertNode(br = dom.create('br'));
484
485 // Place caret after BR
486 r.setStartAfter(br);
487 r.setEndAfter(br);
488 s.setRng(r);
489
490 // Could not place caret after BR then insert an nbsp entity and move the caret
491 if (s.getSel().focusNode == br.previousSibling) {
492 s.select(dom.insertAfter(dom.doc.createTextNode('\u00a0'), br));
493 s.collapse(1);
494 }
495
496 // Scroll to new position, scrollIntoView can't be used due to bug: http://bugs.webkit.org/show_bug.cgi?id=16117
497 ed.getWin().scrollTo(0, dom.getPos(s.getRng().startContainer).y);
498 }
499 });
500
501 // Register plugin
502 tinymce.PluginManager.add('safari', tinymce.plugins.Safari);
503 })();
504