/static/scripts/tiny_mce/plugins/safari/editor_plugin_src.js

http://n23.googlecode.com/ · JavaScript · 460 lines · 292 code · 97 blank · 71 comment · 79 complexity · 60b5be3b6f6076e766a41ceb37805709 MD5 · raw file

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