annotate media/js/tiny_mce/plugins/safari/editor_plugin_src.js @ 177:9b63ad1f2ad2

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