1 /** |
|
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; |
|
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 /* |
|
59 // WebKit generates spans out of thin air this patch used to remove them but it will also remove styles we want so it's disabled for now |
|
60 ed.onPaste.add(function(ed, e) { |
|
61 function removeStyles(e) { |
|
62 e = e.target; |
|
63 |
|
64 if (e.nodeType == 1) { |
|
65 e.style.cssText = ''; |
|
66 |
|
67 each(ed.dom.select('*', e), function(e) { |
|
68 e.style.cssText = ''; |
|
69 }); |
|
70 } |
|
71 }; |
|
72 |
|
73 Event.add(ed.getDoc(), 'DOMNodeInserted', removeStyles); |
|
74 |
|
75 window.setTimeout(function() { |
|
76 Event.remove(ed.getDoc(), 'DOMNodeInserted', removeStyles); |
|
77 }, 0); |
|
78 }); |
|
79 */ |
|
80 ed.onKeyUp.add(function(ed, e) { |
|
81 var h, b, r, n, s; |
|
82 |
|
83 // If backspace or delete key |
|
84 if (e.keyCode == 46 || e.keyCode == 8) { |
|
85 b = ed.getBody(); |
|
86 h = b.innerHTML; |
|
87 s = ed.selection; |
|
88 |
|
89 // If there is no text content or images or hr elements then remove everything |
|
90 if (b.childNodes.length == 1 && !/<(img|hr)/.test(h) && tinymce.trim(h.replace(/<[^>]+>/g, '')).length == 0) { |
|
91 // Inject paragrah and bogus br |
|
92 ed.setContent('<p><br mce_bogus="1" /></p>', {format : 'raw'}); |
|
93 |
|
94 // Move caret before bogus br |
|
95 n = b.firstChild; |
|
96 r = s.getRng(); |
|
97 r.setStart(n, 0); |
|
98 r.setEnd(n, 0); |
|
99 s.setRng(r); |
|
100 } |
|
101 } |
|
102 }); |
|
103 |
|
104 // Workaround for FormatBlock bug, http://bugs.webkit.org/show_bug.cgi?id=16004 |
|
105 ed.addCommand('FormatBlock', function(u, v) { |
|
106 var dom = ed.dom, e = dom.getParent(ed.selection.getNode(), dom.isBlock); |
|
107 |
|
108 if (e) |
|
109 dom.replace(dom.create(v), e, 1); |
|
110 else |
|
111 ed.getDoc().execCommand("FormatBlock", false, v); |
|
112 }); |
|
113 |
|
114 // Workaround for InsertHTML bug, http://bugs.webkit.org/show_bug.cgi?id=16382 |
|
115 ed.addCommand('mceInsertContent', function(u, v) { |
|
116 ed.getDoc().execCommand("InsertText", false, 'mce_marker'); |
|
117 ed.getBody().innerHTML = ed.getBody().innerHTML.replace(/mce_marker/g, ed.dom.processHTML(v) + '<span id="_mce_tmp">XX</span>'); |
|
118 ed.selection.select(ed.dom.get('_mce_tmp')); |
|
119 ed.getDoc().execCommand("Delete", false, ' '); |
|
120 }); |
|
121 |
|
122 /* ed.onKeyDown.add(function(ed, e) { |
|
123 // Ctrl+A select all will fail on WebKit since if you paste the contents you selected it will produce a odd div wrapper |
|
124 if ((e.ctrlKey || e.metaKey) && e.keyCode == 65) { |
|
125 ed.selection.select(ed.getBody(), 1); |
|
126 return Event.cancel(e); |
|
127 } |
|
128 });*/ |
|
129 |
|
130 ed.onKeyPress.add(function(ed, e) { |
|
131 var se, li, lic, r1, r2, n, sel, doc, be, af, pa; |
|
132 |
|
133 if (e.keyCode == 13) { |
|
134 sel = ed.selection; |
|
135 se = sel.getNode(); |
|
136 |
|
137 // Workaround for missing shift+enter support, http://bugs.webkit.org/show_bug.cgi?id=16973 |
|
138 if (e.shiftKey || ed.settings.force_br_newlines && se.nodeName != 'LI') { |
|
139 t._insertBR(ed); |
|
140 Event.cancel(e); |
|
141 } |
|
142 |
|
143 // Workaround for DIV elements produced by Safari |
|
144 if (li = dom.getParent(se, 'LI')) { |
|
145 lic = dom.getParent(li, 'OL,UL'); |
|
146 doc = ed.getDoc(); |
|
147 |
|
148 pa = dom.create('p'); |
|
149 dom.add(pa, 'br', {mce_bogus : "1"}); |
|
150 |
|
151 if (isEmpty(doc, li)) { |
|
152 // If list in list then use browser default behavior |
|
153 if (n = dom.getParent(lic.parentNode, 'LI,OL,UL')) |
|
154 return; |
|
155 |
|
156 n = dom.getParent(lic, 'p,h1,h2,h3,h4,h5,h6,div') || lic; |
|
157 |
|
158 // Create range from the start of block element to the list item |
|
159 r1 = doc.createRange(); |
|
160 r1.setStartBefore(n); |
|
161 r1.setEndBefore(li); |
|
162 |
|
163 // Create range after the list to the end of block element |
|
164 r2 = doc.createRange(); |
|
165 r2.setStartAfter(li); |
|
166 r2.setEndAfter(n); |
|
167 |
|
168 be = r1.cloneContents(); |
|
169 af = r2.cloneContents(); |
|
170 |
|
171 if (!isEmpty(doc, af)) |
|
172 dom.insertAfter(af, n); |
|
173 |
|
174 dom.insertAfter(pa, n); |
|
175 |
|
176 if (!isEmpty(doc, be)) |
|
177 dom.insertAfter(be, n); |
|
178 |
|
179 dom.remove(n); |
|
180 |
|
181 n = pa.firstChild; |
|
182 r1 = doc.createRange(); |
|
183 r1.setStartBefore(n); |
|
184 r1.setEndBefore(n); |
|
185 sel.setRng(r1); |
|
186 |
|
187 return Event.cancel(e); |
|
188 } |
|
189 } |
|
190 } |
|
191 }); |
|
192 |
|
193 // Safari doesn't place lists outside block elements |
|
194 ed.onExecCommand.add(function(ed, cmd) { |
|
195 var sel, dom, bl, bm; |
|
196 |
|
197 if (cmd == 'InsertUnorderedList' || cmd == 'InsertOrderedList') { |
|
198 sel = ed.selection; |
|
199 dom = ed.dom; |
|
200 |
|
201 if (bl = dom.getParent(sel.getNode(), function(n) {return /^(H[1-6]|P|ADDRESS|PRE)$/.test(n.nodeName);})) { |
|
202 bm = sel.getBookmark(); |
|
203 dom.remove(bl, 1); |
|
204 sel.moveToBookmark(bm); |
|
205 } |
|
206 } |
|
207 }); |
|
208 |
|
209 // Workaround for bug, http://bugs.webkit.org/show_bug.cgi?id=12250 |
|
210 ed.onClick.add(function(ed, e) { |
|
211 e = e.target; |
|
212 |
|
213 if (e.nodeName == 'IMG') { |
|
214 t.selElm = e; |
|
215 ed.selection.select(e); |
|
216 } else |
|
217 t.selElm = null; |
|
218 }); |
|
219 |
|
220 ed.onInit.add(function() { |
|
221 t._fixWebKitSpans(); |
|
222 }); |
|
223 |
|
224 ed.onSetContent.add(function() { |
|
225 dom = ed.dom; |
|
226 |
|
227 // Convert strong,b,em,u,strike to spans |
|
228 each(['strong','b','em','u','strike','sub','sup','a'], function(v) { |
|
229 each(grep(dom.select(v)).reverse(), function(n) { |
|
230 var nn = n.nodeName.toLowerCase(), st; |
|
231 |
|
232 // Convert anchors into images |
|
233 if (nn == 'a') { |
|
234 if (n.name) |
|
235 dom.replace(dom.create('img', {mce_name : 'a', name : n.name, 'class' : 'mceItemAnchor'}), n); |
|
236 |
|
237 return; |
|
238 } |
|
239 |
|
240 switch (nn) { |
|
241 case 'b': |
|
242 case 'strong': |
|
243 if (nn == 'b') |
|
244 nn = 'strong'; |
|
245 |
|
246 st = 'font-weight: bold;'; |
|
247 break; |
|
248 |
|
249 case 'em': |
|
250 st = 'font-style: italic;'; |
|
251 break; |
|
252 |
|
253 case 'u': |
|
254 st = 'text-decoration: underline;'; |
|
255 break; |
|
256 |
|
257 case 'sub': |
|
258 st = 'vertical-align: sub;'; |
|
259 break; |
|
260 |
|
261 case 'sup': |
|
262 st = 'vertical-align: super;'; |
|
263 break; |
|
264 |
|
265 case 'strike': |
|
266 st = 'text-decoration: line-through;'; |
|
267 break; |
|
268 } |
|
269 |
|
270 dom.replace(dom.create('span', {mce_name : nn, style : st, 'class' : 'Apple-style-span'}), n, 1); |
|
271 }); |
|
272 }); |
|
273 }); |
|
274 |
|
275 ed.onPreProcess.add(function(ed, o) { |
|
276 dom = ed.dom; |
|
277 |
|
278 each(grep(o.node.getElementsByTagName('span')).reverse(), function(n) { |
|
279 var v, bg; |
|
280 |
|
281 if (o.get) { |
|
282 if (dom.hasClass(n, 'Apple-style-span')) { |
|
283 bg = n.style.backgroundColor; |
|
284 |
|
285 switch (dom.getAttrib(n, 'mce_name')) { |
|
286 case 'font': |
|
287 if (!ed.settings.convert_fonts_to_spans) |
|
288 dom.setAttrib(n, 'style', ''); |
|
289 break; |
|
290 |
|
291 case 'strong': |
|
292 case 'em': |
|
293 case 'sub': |
|
294 case 'sup': |
|
295 dom.setAttrib(n, 'style', ''); |
|
296 break; |
|
297 |
|
298 case 'strike': |
|
299 case 'u': |
|
300 if (!ed.settings.inline_styles) |
|
301 dom.setAttrib(n, 'style', ''); |
|
302 else |
|
303 dom.setAttrib(n, 'mce_name', ''); |
|
304 |
|
305 break; |
|
306 |
|
307 default: |
|
308 if (!ed.settings.inline_styles) |
|
309 dom.setAttrib(n, 'style', ''); |
|
310 } |
|
311 |
|
312 |
|
313 if (bg) |
|
314 n.style.backgroundColor = bg; |
|
315 } |
|
316 } |
|
317 |
|
318 if (dom.hasClass(n, 'mceItemRemoved')) |
|
319 dom.remove(n, 1); |
|
320 }); |
|
321 }); |
|
322 |
|
323 ed.onPostProcess.add(function(ed, o) { |
|
324 // Safari adds BR at end of all block elements |
|
325 o.content = o.content.replace(/<br \/><\/(h[1-6]|div|p|address|pre)>/g, '</$1>'); |
|
326 |
|
327 // Safari adds id="undefined" to HR elements |
|
328 o.content = o.content.replace(/ id=\"undefined\"/g, ''); |
|
329 }); |
|
330 }, |
|
331 |
|
332 getInfo : function() { |
|
333 return { |
|
334 longname : 'Safari compatibility', |
|
335 author : 'Moxiecode Systems AB', |
|
336 authorurl : 'http://tinymce.moxiecode.com', |
|
337 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari', |
|
338 version : tinymce.majorVersion + "." + tinymce.minorVersion |
|
339 }; |
|
340 }, |
|
341 |
|
342 // Internal methods |
|
343 |
|
344 _fixWebKitSpans : function() { |
|
345 var t = this, ed = t.editor; |
|
346 |
|
347 // Use mutator events on new WebKit |
|
348 Event.add(ed.getDoc(), 'DOMNodeInserted', function(e) { |
|
349 e = e.target; |
|
350 |
|
351 if (e && e.nodeType == 1) |
|
352 t._fixAppleSpan(e); |
|
353 }); |
|
354 }, |
|
355 |
|
356 _fixAppleSpan : function(e) { |
|
357 var ed = this.editor, dom = ed.dom, fz = this.webKitFontSizes, fzn = this.namedFontSizes, s = ed.settings, st, p; |
|
358 |
|
359 if (dom.getAttrib(e, 'mce_fixed')) |
|
360 return; |
|
361 |
|
362 // Handle Apple style spans |
|
363 if (e.nodeName == 'SPAN' && e.className == 'Apple-style-span') { |
|
364 st = e.style; |
|
365 |
|
366 if (!s.convert_fonts_to_spans) { |
|
367 if (st.fontSize) { |
|
368 dom.setAttrib(e, 'mce_name', 'font'); |
|
369 dom.setAttrib(e, 'size', inArray(fz, st.fontSize) + 1); |
|
370 } |
|
371 |
|
372 if (st.fontFamily) { |
|
373 dom.setAttrib(e, 'mce_name', 'font'); |
|
374 dom.setAttrib(e, 'face', st.fontFamily); |
|
375 } |
|
376 |
|
377 if (st.color) { |
|
378 dom.setAttrib(e, 'mce_name', 'font'); |
|
379 dom.setAttrib(e, 'color', dom.toHex(st.color)); |
|
380 } |
|
381 |
|
382 if (st.backgroundColor) { |
|
383 dom.setAttrib(e, 'mce_name', 'font'); |
|
384 dom.setStyle(e, 'background-color', st.backgroundColor); |
|
385 } |
|
386 } else { |
|
387 if (st.fontSize) |
|
388 dom.setStyle(e, 'fontSize', fzn[inArray(fz, st.fontSize)]); |
|
389 } |
|
390 |
|
391 if (st.fontWeight == 'bold') |
|
392 dom.setAttrib(e, 'mce_name', 'strong'); |
|
393 |
|
394 if (st.fontStyle == 'italic') |
|
395 dom.setAttrib(e, 'mce_name', 'em'); |
|
396 |
|
397 if (st.textDecoration == 'underline') |
|
398 dom.setAttrib(e, 'mce_name', 'u'); |
|
399 |
|
400 if (st.textDecoration == 'line-through') |
|
401 dom.setAttrib(e, 'mce_name', 'strike'); |
|
402 |
|
403 if (st.verticalAlign == 'super') |
|
404 dom.setAttrib(e, 'mce_name', 'sup'); |
|
405 |
|
406 if (st.verticalAlign == 'sub') |
|
407 dom.setAttrib(e, 'mce_name', 'sub'); |
|
408 |
|
409 dom.setAttrib(e, 'mce_fixed', '1'); |
|
410 } |
|
411 }, |
|
412 |
|
413 _insertBR : function(ed) { |
|
414 var dom = ed.dom, s = ed.selection, r = s.getRng(), br; |
|
415 |
|
416 // Insert BR element |
|
417 r.insertNode(br = dom.create('br')); |
|
418 |
|
419 // Place caret after BR |
|
420 r.setStartAfter(br); |
|
421 r.setEndAfter(br); |
|
422 s.setRng(r); |
|
423 |
|
424 // Could not place caret after BR then insert an nbsp entity and move the caret |
|
425 if (s.getSel().focusNode == br.previousSibling) { |
|
426 s.select(dom.insertAfter(dom.doc.createTextNode('\u00a0'), br)); |
|
427 s.collapse(1); |
|
428 } |
|
429 |
|
430 // Scroll to new position, scrollIntoView can't be used due to bug: http://bugs.webkit.org/show_bug.cgi?id=16117 |
|
431 ed.getWin().scrollTo(0, dom.getPos(s.getRng().startContainer).y); |
|
432 } |
|
433 }); |
|
434 |
|
435 // Register plugin |
|
436 tinymce.PluginManager.add('safari', tinymce.plugins.Safari); |
|
437 })(); |
|
438 |
|