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