bgneal@45: /**
bgneal@45: * $Id: editor_plugin_src.js 264 2007-04-26 20:53:09Z spocke $
bgneal@45: *
bgneal@45: * @author Moxiecode
bgneal@45: * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
bgneal@45: */
bgneal@45:
bgneal@45: (function() {
bgneal@45: var Event = tinymce.dom.Event, grep = tinymce.grep, each = tinymce.each, inArray = tinymce.inArray;
bgneal@45:
bgneal@45: function isEmpty(d, e, f) {
bgneal@45: var w, n;
bgneal@45:
bgneal@45: w = d.createTreeWalker(e, NodeFilter.SHOW_ALL, null, false);
bgneal@45: while (n = w.nextNode()) {
bgneal@45: // Filter func
bgneal@45: if (f) {
bgneal@45: if (!f(n))
bgneal@45: return false;
bgneal@45: }
bgneal@45:
bgneal@45: // Non whitespace text node
bgneal@45: if (n.nodeType == 3 && n.nodeValue && /[^\s\u00a0]+/.test(n.nodeValue))
bgneal@45: return false;
bgneal@45:
bgneal@45: // Is non text element byt still content
bgneal@45: if (n.nodeType == 1 && /^(HR|IMG|TABLE)$/.test(n.nodeName))
bgneal@45: return false;
bgneal@45: }
bgneal@45:
bgneal@45: return true;
bgneal@45: };
bgneal@45:
bgneal@45: tinymce.create('tinymce.plugins.Safari', {
bgneal@45: init : function(ed) {
bgneal@45: var t = this, dom;
bgneal@45:
bgneal@45: // Ignore on non webkit
bgneal@45: if (!tinymce.isWebKit)
bgneal@45: return;
bgneal@45:
bgneal@45: t.editor = ed;
bgneal@45: t.webKitFontSizes = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', '-webkit-xxx-large'];
bgneal@45: t.namedFontSizes = ['xx-small', 'x-small','small','medium','large','x-large', 'xx-large'];
bgneal@45:
bgneal@45: // Safari CreateLink command will not work correctly on images that is aligned
bgneal@45: ed.addCommand('CreateLink', function(u, v) {
bgneal@45: var n = ed.selection.getNode(), dom = ed.dom, a;
bgneal@45:
bgneal@45: if (n && (/^(left|right)$/i.test(dom.getStyle(n, 'float', 1)) || /^(left|right)$/i.test(dom.getAttrib(n, 'align')))) {
bgneal@45: a = dom.create('a', {href : v}, n.cloneNode());
bgneal@45: n.parentNode.replaceChild(a, n);
bgneal@45: ed.selection.select(a);
bgneal@45: } else
bgneal@45: ed.getDoc().execCommand("CreateLink", false, v);
bgneal@45: });
bgneal@45:
bgneal@45: ed.onPaste.add(function(ed, e) {
bgneal@45: function removeStyles(e) {
bgneal@45: e = e.target;
bgneal@45:
bgneal@45: if (e.nodeType == 1) {
bgneal@45: e.style.cssText = '';
bgneal@45:
bgneal@45: each(ed.dom.select('*', e), function(e) {
bgneal@45: e.style.cssText = '';
bgneal@45: });
bgneal@45: }
bgneal@45: };
bgneal@45:
bgneal@45: Event.add(ed.getDoc(), 'DOMNodeInserted', removeStyles);
bgneal@45:
bgneal@45: window.setTimeout(function() {
bgneal@45: Event.remove(ed.getDoc(), 'DOMNodeInserted', removeStyles);
bgneal@45: }, 0);
bgneal@45: });
bgneal@45:
bgneal@45: ed.onKeyUp.add(function(ed, e) {
bgneal@45: var h, b, r, n, s;
bgneal@45:
bgneal@45: // If backspace or delete key
bgneal@45: if (e.keyCode == 46 || e.keyCode == 8) {
bgneal@45: b = ed.getBody();
bgneal@45: h = b.innerHTML;
bgneal@45: s = ed.selection;
bgneal@45:
bgneal@45: // If there is no text content or images or hr elements then remove everything
bgneal@45: if (b.childNodes.length == 1 && !/<(img|hr)/.test(h) && tinymce.trim(h.replace(/<[^>]+>/g, '')).length == 0) {
bgneal@45: // Inject paragrah and bogus br
bgneal@45: ed.setContent('
', {format : 'raw'});
bgneal@45:
bgneal@45: // Move caret before bogus br
bgneal@45: n = b.firstChild;
bgneal@45: r = s.getRng();
bgneal@45: r.setStart(n, 0);
bgneal@45: r.setEnd(n, 0);
bgneal@45: s.setRng(r);
bgneal@45: }
bgneal@45: }
bgneal@45: });
bgneal@45:
bgneal@45: // Workaround for FormatBlock bug, http://bugs.webkit.org/show_bug.cgi?id=16004
bgneal@45: ed.addCommand('FormatBlock', function(u, v) {
bgneal@45: var dom = ed.dom, e = dom.getParent(ed.selection.getNode(), dom.isBlock);
bgneal@45:
bgneal@45: if (e)
bgneal@45: dom.replace(dom.create(v), e, 1);
bgneal@45: else
bgneal@45: ed.getDoc().execCommand("FormatBlock", false, v);
bgneal@45: });
bgneal@45:
bgneal@45: // Workaround for InsertHTML bug, http://bugs.webkit.org/show_bug.cgi?id=16382
bgneal@45: ed.addCommand('mceInsertContent', function(u, v) {
bgneal@45: ed.getDoc().execCommand("InsertText", false, 'mce_marker');
bgneal@45: ed.getBody().innerHTML = ed.getBody().innerHTML.replace(/mce_marker/g, ed.dom.processHTML(v) + 'XX');
bgneal@45: ed.selection.select(ed.dom.get('_mce_tmp'));
bgneal@45: ed.getDoc().execCommand("Delete", false, ' ');
bgneal@45: });
bgneal@45:
bgneal@45: ed.onKeyPress.add(function(ed, e) {
bgneal@45: var se, li, lic, r1, r2, n, sel, doc, be, af, pa;
bgneal@45:
bgneal@45: if (e.keyCode == 13) {
bgneal@45: sel = ed.selection;
bgneal@45: se = sel.getNode();
bgneal@45:
bgneal@45: // Workaround for missing shift+enter support, http://bugs.webkit.org/show_bug.cgi?id=16973
bgneal@45: if (e.shiftKey || ed.settings.force_br_newlines && se.nodeName != 'LI') {
bgneal@45: t._insertBR(ed);
bgneal@45: Event.cancel(e);
bgneal@45: }
bgneal@45:
bgneal@45: // Workaround for DIV elements produced by Safari
bgneal@45: if (li = dom.getParent(se, 'LI')) {
bgneal@45: lic = dom.getParent(li, 'OL,UL');
bgneal@45: doc = ed.getDoc();
bgneal@45:
bgneal@45: pa = dom.create('p');
bgneal@45: dom.add(pa, 'br', {mce_bogus : "1"});
bgneal@45:
bgneal@45: if (isEmpty(doc, li)) {
bgneal@45: // If list in list then use browser default behavior
bgneal@45: if (n = dom.getParent(lic.parentNode, 'LI,OL,UL'))
bgneal@45: return;
bgneal@45:
bgneal@45: n = dom.getParent(lic, 'p,h1,h2,h3,h4,h5,h6,div') || lic;
bgneal@45:
bgneal@45: // Create range from the start of block element to the list item
bgneal@45: r1 = doc.createRange();
bgneal@45: r1.setStartBefore(n);
bgneal@45: r1.setEndBefore(li);
bgneal@45:
bgneal@45: // Create range after the list to the end of block element
bgneal@45: r2 = doc.createRange();
bgneal@45: r2.setStartAfter(li);
bgneal@45: r2.setEndAfter(n);
bgneal@45:
bgneal@45: be = r1.cloneContents();
bgneal@45: af = r2.cloneContents();
bgneal@45:
bgneal@45: if (!isEmpty(doc, af))
bgneal@45: dom.insertAfter(af, n);
bgneal@45:
bgneal@45: dom.insertAfter(pa, n);
bgneal@45:
bgneal@45: if (!isEmpty(doc, be))
bgneal@45: dom.insertAfter(be, n);
bgneal@45:
bgneal@45: dom.remove(n);
bgneal@45:
bgneal@45: n = pa.firstChild;
bgneal@45: r1 = doc.createRange();
bgneal@45: r1.setStartBefore(n);
bgneal@45: r1.setEndBefore(n);
bgneal@45: sel.setRng(r1);
bgneal@45:
bgneal@45: return Event.cancel(e);
bgneal@45: }
bgneal@45: }
bgneal@45: }
bgneal@45: });
bgneal@45:
bgneal@45: // Safari doesn't place lists outside block elements
bgneal@45: ed.onExecCommand.add(function(ed, cmd) {
bgneal@45: var sel, dom, bl, bm;
bgneal@45:
bgneal@45: if (cmd == 'InsertUnorderedList' || cmd == 'InsertOrderedList') {
bgneal@45: sel = ed.selection;
bgneal@45: dom = ed.dom;
bgneal@45:
bgneal@45: if (bl = dom.getParent(sel.getNode(), function(n) {return /^(H[1-6]|P|ADDRESS|PRE)$/.test(n.nodeName);})) {
bgneal@45: bm = sel.getBookmark();
bgneal@45: dom.remove(bl, 1);
bgneal@45: sel.moveToBookmark(bm);
bgneal@45: }
bgneal@45: }
bgneal@45: });
bgneal@45:
bgneal@45: // Workaround for bug, http://bugs.webkit.org/show_bug.cgi?id=12250
bgneal@45: ed.onClick.add(function(ed, e) {
bgneal@45: e = e.target;
bgneal@45:
bgneal@45: if (e.nodeName == 'IMG') {
bgneal@45: t.selElm = e;
bgneal@45: ed.selection.select(e);
bgneal@45: } else
bgneal@45: t.selElm = null;
bgneal@45: });
bgneal@45:
bgneal@45: ed.onInit.add(function() {
bgneal@45: t._fixWebKitSpans();
bgneal@45: });
bgneal@45:
bgneal@45: ed.onSetContent.add(function() {
bgneal@45: dom = ed.dom;
bgneal@45:
bgneal@45: // Convert strong,b,em,u,strike to spans
bgneal@45: each(['strong','b','em','u','strike','sub','sup','a'], function(v) {
bgneal@45: each(grep(dom.select(v)).reverse(), function(n) {
bgneal@45: var nn = n.nodeName.toLowerCase(), st;
bgneal@45:
bgneal@45: // Convert anchors into images
bgneal@45: if (nn == 'a') {
bgneal@45: if (n.name)
bgneal@45: dom.replace(dom.create('img', {mce_name : 'a', name : n.name, 'class' : 'mceItemAnchor'}), n);
bgneal@45:
bgneal@45: return;
bgneal@45: }
bgneal@45:
bgneal@45: switch (nn) {
bgneal@45: case 'b':
bgneal@45: case 'strong':
bgneal@45: if (nn == 'b')
bgneal@45: nn = 'strong';
bgneal@45:
bgneal@45: st = 'font-weight: bold;';
bgneal@45: break;
bgneal@45:
bgneal@45: case 'em':
bgneal@45: st = 'font-style: italic;';
bgneal@45: break;
bgneal@45:
bgneal@45: case 'u':
bgneal@45: st = 'text-decoration: underline;';
bgneal@45: break;
bgneal@45:
bgneal@45: case 'sub':
bgneal@45: st = 'vertical-align: sub;';
bgneal@45: break;
bgneal@45:
bgneal@45: case 'sup':
bgneal@45: st = 'vertical-align: super;';
bgneal@45: break;
bgneal@45:
bgneal@45: case 'strike':
bgneal@45: st = 'text-decoration: line-through;';
bgneal@45: break;
bgneal@45: }
bgneal@45:
bgneal@45: dom.replace(dom.create('span', {mce_name : nn, style : st, 'class' : 'Apple-style-span'}), n, 1);
bgneal@45: });
bgneal@45: });
bgneal@45: });
bgneal@45:
bgneal@45: ed.onPreProcess.add(function(ed, o) {
bgneal@45: dom = ed.dom;
bgneal@45:
bgneal@45: each(grep(o.node.getElementsByTagName('span')).reverse(), function(n) {
bgneal@45: var v, bg;
bgneal@45:
bgneal@45: if (o.get) {
bgneal@45: if (dom.hasClass(n, 'Apple-style-span')) {
bgneal@45: bg = n.style.backgroundColor;
bgneal@45:
bgneal@45: switch (dom.getAttrib(n, 'mce_name')) {
bgneal@45: case 'font':
bgneal@45: if (!ed.settings.convert_fonts_to_spans)
bgneal@45: dom.setAttrib(n, 'style', '');
bgneal@45: break;
bgneal@45:
bgneal@45: case 'strong':
bgneal@45: case 'em':
bgneal@45: case 'sub':
bgneal@45: case 'sup':
bgneal@45: dom.setAttrib(n, 'style', '');
bgneal@45: break;
bgneal@45:
bgneal@45: case 'strike':
bgneal@45: case 'u':
bgneal@45: if (!ed.settings.inline_styles)
bgneal@45: dom.setAttrib(n, 'style', '');
bgneal@45: else
bgneal@45: dom.setAttrib(n, 'mce_name', '');
bgneal@45:
bgneal@45: break;
bgneal@45:
bgneal@45: default:
bgneal@45: if (!ed.settings.inline_styles)
bgneal@45: dom.setAttrib(n, 'style', '');
bgneal@45: }
bgneal@45:
bgneal@45:
bgneal@45: if (bg)
bgneal@45: n.style.backgroundColor = bg;
bgneal@45: }
bgneal@45: }
bgneal@45:
bgneal@45: if (dom.hasClass(n, 'mceItemRemoved'))
bgneal@45: dom.remove(n, 1);
bgneal@45: });
bgneal@45: });
bgneal@45:
bgneal@45: ed.onPostProcess.add(function(ed, o) {
bgneal@45: // Safari adds BR at end of all block elements
bgneal@45: o.content = o.content.replace(/
<\/(h[1-6]|div|p|address|pre)>/g, '$1>');
bgneal@45:
bgneal@45: // Safari adds id="undefined" to HR elements
bgneal@45: o.content = o.content.replace(/ id=\"undefined\"/g, '');
bgneal@45: });
bgneal@45: },
bgneal@45:
bgneal@45: getInfo : function() {
bgneal@45: return {
bgneal@45: longname : 'Safari compatibility',
bgneal@45: author : 'Moxiecode Systems AB',
bgneal@45: authorurl : 'http://tinymce.moxiecode.com',
bgneal@45: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',
bgneal@45: version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@45: };
bgneal@45: },
bgneal@45:
bgneal@45: // Internal methods
bgneal@45:
bgneal@45: _fixWebKitSpans : function() {
bgneal@45: var t = this, ed = t.editor;
bgneal@45:
bgneal@45: // Use mutator events on new WebKit
bgneal@45: Event.add(ed.getDoc(), 'DOMNodeInserted', function(e) {
bgneal@45: e = e.target;
bgneal@45:
bgneal@45: if (e && e.nodeType == 1)
bgneal@45: t._fixAppleSpan(e);
bgneal@45: });
bgneal@45: },
bgneal@45:
bgneal@45: _fixAppleSpan : function(e) {
bgneal@45: var ed = this.editor, dom = ed.dom, fz = this.webKitFontSizes, fzn = this.namedFontSizes, s = ed.settings, st, p;
bgneal@45:
bgneal@45: if (dom.getAttrib(e, 'mce_fixed'))
bgneal@45: return;
bgneal@45:
bgneal@45: // Handle Apple style spans
bgneal@45: if (e.nodeName == 'SPAN' && e.className == 'Apple-style-span') {
bgneal@45: st = e.style;
bgneal@45:
bgneal@45: if (!s.convert_fonts_to_spans) {
bgneal@45: if (st.fontSize) {
bgneal@45: dom.setAttrib(e, 'mce_name', 'font');
bgneal@45: dom.setAttrib(e, 'size', inArray(fz, st.fontSize) + 1);
bgneal@45: }
bgneal@45:
bgneal@45: if (st.fontFamily) {
bgneal@45: dom.setAttrib(e, 'mce_name', 'font');
bgneal@45: dom.setAttrib(e, 'face', st.fontFamily);
bgneal@45: }
bgneal@45:
bgneal@45: if (st.color) {
bgneal@45: dom.setAttrib(e, 'mce_name', 'font');
bgneal@45: dom.setAttrib(e, 'color', dom.toHex(st.color));
bgneal@45: }
bgneal@45:
bgneal@45: if (st.backgroundColor) {
bgneal@45: dom.setAttrib(e, 'mce_name', 'font');
bgneal@45: dom.setStyle(e, 'background-color', st.backgroundColor);
bgneal@45: }
bgneal@45: } else {
bgneal@45: if (st.fontSize)
bgneal@45: dom.setStyle(e, 'fontSize', fzn[inArray(fz, st.fontSize)]);
bgneal@45: }
bgneal@45:
bgneal@45: if (st.fontWeight == 'bold')
bgneal@45: dom.setAttrib(e, 'mce_name', 'strong');
bgneal@45:
bgneal@45: if (st.fontStyle == 'italic')
bgneal@45: dom.setAttrib(e, 'mce_name', 'em');
bgneal@45:
bgneal@45: if (st.textDecoration == 'underline')
bgneal@45: dom.setAttrib(e, 'mce_name', 'u');
bgneal@45:
bgneal@45: if (st.textDecoration == 'line-through')
bgneal@45: dom.setAttrib(e, 'mce_name', 'strike');
bgneal@45:
bgneal@45: if (st.verticalAlign == 'super')
bgneal@45: dom.setAttrib(e, 'mce_name', 'sup');
bgneal@45:
bgneal@45: if (st.verticalAlign == 'sub')
bgneal@45: dom.setAttrib(e, 'mce_name', 'sub');
bgneal@45:
bgneal@45: dom.setAttrib(e, 'mce_fixed', '1');
bgneal@45: }
bgneal@45: },
bgneal@45:
bgneal@45: _insertBR : function(ed) {
bgneal@45: var dom = ed.dom, s = ed.selection, r = s.getRng(), br;
bgneal@45:
bgneal@45: // Insert BR element
bgneal@45: r.insertNode(br = dom.create('br'));
bgneal@45:
bgneal@45: // Place caret after BR
bgneal@45: r.setStartAfter(br);
bgneal@45: r.setEndAfter(br);
bgneal@45: s.setRng(r);
bgneal@45:
bgneal@45: // Could not place caret after BR then insert an nbsp entity and move the caret
bgneal@45: if (s.getSel().focusNode == br.previousSibling) {
bgneal@45: s.select(dom.insertAfter(dom.doc.createTextNode('\u00a0'), br));
bgneal@45: s.collapse(1);
bgneal@45: }
bgneal@45:
bgneal@45: // Scroll to new position, scrollIntoView can't be used due to bug: http://bugs.webkit.org/show_bug.cgi?id=16117
bgneal@45: ed.getWin().scrollTo(0, dom.getPos(s.getRng().startContainer).y);
bgneal@45: }
bgneal@45: });
bgneal@45:
bgneal@45: // Register plugin
bgneal@45: tinymce.PluginManager.add('safari', tinymce.plugins.Safari);
bgneal@45: })();
bgneal@45: