5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
9 var Event = tinymce.dom.Event, grep = tinymce.grep, each = tinymce.each, inArray = tinymce.inArray, isOldWebKit = tinymce.isOldWebKit;
11 function isEmpty(d, e, f) {
14 w = d.createTreeWalker(e, NodeFilter.SHOW_ALL, null, false);
15 while (n = w.nextNode()) {
22 // Non whitespace text node
23 if (n.nodeType == 3 && n.nodeValue && /[^\s\u00a0]+/.test(n.nodeValue))
26 // Is non text element byt still content
27 if (n.nodeType == 1 && /^(HR|IMG|TABLE)$/.test(n.nodeName))
34 tinymce.create('tinymce.plugins.Safari', {
38 // Ignore on non webkit
39 if (!tinymce.isWebKit)
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'];
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;
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);
55 ed.getDoc().execCommand("CreateLink", false, v);
58 ed.onPaste.add(function(ed, e) {
59 function removeStyles(e) {
62 if (e.nodeType == 1) {
65 each(ed.dom.select('*', e), function(e) {
71 Event.add(ed.getDoc(), 'DOMNodeInserted', removeStyles);
73 window.setTimeout(function() {
74 Event.remove(ed.getDoc(), 'DOMNodeInserted', removeStyles);
78 ed.onKeyUp.add(function(ed, e) {
81 // If backspace or delete key
82 if (e.keyCode == 46 || e.keyCode == 8) {
87 // If there is no text content or images or hr elements then remove everything
88 if (b.childNodes.length == 1 && !/<(img|hr)/.test(h) && tinymce.trim(h.replace(/<[^>]+>/g, '')).length == 0) {
89 // Inject paragrah and bogus br
90 ed.setContent('<p><br mce_bogus="1" /></p>', {format : 'raw'});
92 // Move caret before bogus br
102 // Workaround for FormatBlock bug, http://bugs.webkit.org/show_bug.cgi?id=16004
103 ed.addCommand('FormatBlock', function(u, v) {
104 var dom = ed.dom, e = dom.getParent(ed.selection.getNode(), dom.isBlock);
107 dom.replace(dom.create(v), e, 1);
109 ed.getDoc().execCommand("FormatBlock", false, v);
112 // Workaround for InsertHTML bug, http://bugs.webkit.org/show_bug.cgi?id=16382
113 ed.addCommand('mceInsertContent', function(u, v) {
114 ed.getDoc().execCommand("InsertText", false, 'mce_marker');
115 ed.getBody().innerHTML = ed.getBody().innerHTML.replace(/mce_marker/g, ed.dom.processHTML(v) + '<span id="_mce_tmp">XX</span>');
116 ed.selection.select(ed.dom.get('_mce_tmp'));
117 ed.getDoc().execCommand("Delete", false, ' ');
120 ed.onKeyPress.add(function(ed, e) {
121 var se, li, lic, r1, r2, n, sel, doc, be, af, pa;
123 if (e.keyCode == 13) {
127 // Workaround for missing shift+enter support, http://bugs.webkit.org/show_bug.cgi?id=16973
128 if (e.shiftKey || ed.settings.force_br_newlines && se.nodeName != 'LI') {
133 // Workaround for DIV elements produced by Safari
134 if (li = dom.getParent(se, 'LI')) {
135 lic = dom.getParent(li, 'OL,UL');
138 pa = dom.create('p');
139 dom.add(pa, 'br', {mce_bogus : "1"});
141 if (isEmpty(doc, li)) {
142 // If list in list then use browser default behavior
143 if (n = dom.getParent(lic.parentNode, 'LI,OL,UL'))
146 n = dom.getParent(lic, 'p,h1,h2,h3,h4,h5,h6,div') || lic;
148 // Create range from the start of block element to the list item
149 r1 = doc.createRange();
150 r1.setStartBefore(n);
153 // Create range after the list to the end of block element
154 r2 = doc.createRange();
155 r2.setStartAfter(li);
158 be = r1.cloneContents();
159 af = r2.cloneContents();
161 if (!isEmpty(doc, af))
162 dom.insertAfter(af, n);
164 dom.insertAfter(pa, n);
166 if (!isEmpty(doc, be))
167 dom.insertAfter(be, n);
172 r1 = doc.createRange();
173 r1.setStartBefore(n);
177 return Event.cancel(e);
183 // Safari doesn't place lists outside block elements
184 ed.onExecCommand.add(function(ed, cmd) {
185 var sel, dom, bl, bm;
187 if (cmd == 'InsertUnorderedList' || cmd == 'InsertOrderedList') {
191 if (bl = dom.getParent(sel.getNode(), function(n) {return /^(H[1-6]|P|ADDRESS|PRE)$/.test(n.nodeName);})) {
192 bm = sel.getBookmark();
194 sel.moveToBookmark(bm);
199 // Workaround for bug, http://bugs.webkit.org/show_bug.cgi?id=12250
200 ed.onClick.add(function(ed, e) {
203 if (e.nodeName == 'IMG') {
205 ed.selection.select(e);
210 ed.onInit.add(function() {
214 t._patchSafari2x(ed);
217 ed.onSetContent.add(function() {
220 // Convert strong,b,em,u,strike to spans
221 each(['strong','b','em','u','strike','sub','sup','a'], function(v) {
222 each(grep(dom.select(v)).reverse(), function(n) {
223 var nn = n.nodeName.toLowerCase(), st;
225 // Convert anchors into images
228 dom.replace(dom.create('img', {mce_name : 'a', name : n.name, 'class' : 'mceItemAnchor'}), n);
239 st = 'font-weight: bold;';
243 st = 'font-style: italic;';
247 st = 'text-decoration: underline;';
251 st = 'vertical-align: sub;';
255 st = 'vertical-align: super;';
259 st = 'text-decoration: line-through;';
263 dom.replace(dom.create('span', {mce_name : nn, style : st, 'class' : 'Apple-style-span'}), n, 1);
268 ed.onPreProcess.add(function(ed, o) {
271 each(grep(o.node.getElementsByTagName('span')).reverse(), function(n) {
275 if (dom.hasClass(n, 'Apple-style-span')) {
276 bg = n.style.backgroundColor;
278 switch (dom.getAttrib(n, 'mce_name')) {
280 if (!ed.settings.convert_fonts_to_spans)
281 dom.setAttrib(n, 'style', '');
288 dom.setAttrib(n, 'style', '');
293 if (!ed.settings.inline_styles)
294 dom.setAttrib(n, 'style', '');
296 dom.setAttrib(n, 'mce_name', '');
301 if (!ed.settings.inline_styles)
302 dom.setAttrib(n, 'style', '');
307 n.style.backgroundColor = bg;
311 if (dom.hasClass(n, 'mceItemRemoved'))
316 ed.onPostProcess.add(function(ed, o) {
317 // Safari adds BR at end of all block elements
318 o.content = o.content.replace(/<br \/><\/(h[1-6]|div|p|address|pre)>/g, '</$1>');
320 // Safari adds id="undefined" to HR elements
321 o.content = o.content.replace(/ id=\"undefined\"/g, '');
325 getInfo : function() {
327 longname : 'Safari compatibility',
328 author : 'Moxiecode Systems AB',
329 authorurl : 'http://tinymce.moxiecode.com',
330 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',
331 version : tinymce.majorVersion + "." + tinymce.minorVersion
337 _fixWebKitSpans : function() {
338 var t = this, ed = t.editor;
341 // Use mutator events on new WebKit
342 Event.add(ed.getDoc(), 'DOMNodeInserted', function(e) {
345 if (e && e.nodeType == 1)
349 // Do post command processing in old WebKit since the browser crashes on Mutator events :(
350 ed.onExecCommand.add(function() {
351 each(ed.dom.select('span'), function(n) {
360 _fixAppleSpan : function(e) {
361 var ed = this.editor, dom = ed.dom, fz = this.webKitFontSizes, fzn = this.namedFontSizes, s = ed.settings, st, p;
363 if (dom.getAttrib(e, 'mce_fixed'))
366 // Handle Apple style spans
367 if (e.nodeName == 'SPAN' && e.className == 'Apple-style-span') {
370 if (!s.convert_fonts_to_spans) {
372 dom.setAttrib(e, 'mce_name', 'font');
373 dom.setAttrib(e, 'size', inArray(fz, st.fontSize) + 1);
377 dom.setAttrib(e, 'mce_name', 'font');
378 dom.setAttrib(e, 'face', st.fontFamily);
382 dom.setAttrib(e, 'mce_name', 'font');
383 dom.setAttrib(e, 'color', dom.toHex(st.color));
386 if (st.backgroundColor) {
387 dom.setAttrib(e, 'mce_name', 'font');
388 dom.setStyle(e, 'background-color', st.backgroundColor);
392 dom.setStyle(e, 'fontSize', fzn[inArray(fz, st.fontSize)]);
395 if (st.fontWeight == 'bold')
396 dom.setAttrib(e, 'mce_name', 'strong');
398 if (st.fontStyle == 'italic')
399 dom.setAttrib(e, 'mce_name', 'em');
401 if (st.textDecoration == 'underline')
402 dom.setAttrib(e, 'mce_name', 'u');
404 if (st.textDecoration == 'line-through')
405 dom.setAttrib(e, 'mce_name', 'strike');
407 if (st.verticalAlign == 'super')
408 dom.setAttrib(e, 'mce_name', 'sup');
410 if (st.verticalAlign == 'sub')
411 dom.setAttrib(e, 'mce_name', 'sub');
413 dom.setAttrib(e, 'mce_fixed', '1');
417 _patchSafari2x : function(ed) {
418 var t = this, setContent, getNode, dom = ed.dom, lr;
421 if (ed.windowManager.onBeforeOpen) {
422 ed.windowManager.onBeforeOpen.add(function() {
423 r = ed.selection.getRng();
427 // Fake select on 2.x
428 ed.selection.select = function(n) {
429 this.getSel().setBaseAndExtent(n, 0, n, 1);
432 getNode = ed.selection.getNode;
433 ed.selection.getNode = function() {
434 return t.selElm || getNode.call(this);
437 // Fake range on Safari 2.x
438 ed.selection.getRng = function() {
439 var t = this, s = t.getSel(), d = ed.getDoc(), r, rb, ra, di;
441 // Fake range on Safari 2.x
446 // Setup before range
447 rb = d.createRange();
448 rb.setStart(s.anchorNode, s.anchorOffset);
452 ra = d.createRange();
453 ra.setStart(s.focusNode, s.focusOffset);
456 // Setup start/end points by comparing locations
457 di = rb.compareBoundaryPoints(rb.START_TO_END, ra) < 0;
458 r.setStart(di ? s.anchorNode : s.focusNode, di ? s.anchorOffset : s.focusOffset);
459 r.setEnd(di ? s.focusNode : s.anchorNode, di ? s.focusOffset : s.anchorOffset);
463 // Sometimes fails, at least we tried to do it by the book. I hope Safari 2.x will go disappear soooon!!!
470 // Fix setContent so it works
471 setContent = ed.selection.setContent;
472 ed.selection.setContent = function(h, s) {
473 var r = this.getRng(), b;
476 setContent.call(this, h, s);
478 // Workaround for Safari 2.x
479 b = dom.create('body');
482 each(b.childNodes, function(n) {
483 r.insertNode(n.cloneNode(true));
489 _insertBR : function(ed) {
490 var dom = ed.dom, s = ed.selection, r = s.getRng(), br;
493 r.insertNode(br = dom.create('br'));
495 // Place caret after BR
500 // Could not place caret after BR then insert an nbsp entity and move the caret
501 if (s.getSel().focusNode == br.previousSibling) {
502 s.select(dom.insertAfter(dom.doc.createTextNode('\u00a0'), br));
506 // Scroll to new position, scrollIntoView can't be used due to bug: http://bugs.webkit.org/show_bug.cgi?id=16117
507 ed.getWin().scrollTo(0, dom.getPos(s.getRng().startContainer).y);
512 tinymce.PluginManager.add('safari', tinymce.plugins.Safari);