4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;
14 tinymce.create('tinymce.plugins.SpellcheckerPlugin', {
15 getInfo : function() {
17 longname : 'Spellchecker',
18 author : 'Moxiecode Systems AB',
19 authorurl : 'http://tinymce.moxiecode.com',
20 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',
21 version : tinymce.majorVersion + "." + tinymce.minorVersion
25 init : function(ed, url) {
30 t.rpcUrl = ed.getParam("spellchecker_rpc_url", "{backend}");
32 if (t.rpcUrl == '{backend}') {
33 // Sniff if the browser supports native spellchecking (Don't know of a better way)
39 // Disable the context menu when spellchecking is active
40 ed.onContextMenu.addToTop(function(ed, e) {
47 ed.addCommand('mceSpellCheck', function() {
48 if (t.rpcUrl == '{backend}') {
49 // Enable/disable native spellchecker
50 t.editor.getBody().spellcheck = t.active = !t.active;
55 ed.setProgressState(1);
56 t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {
60 ed.setProgressState(0);
63 ed.setProgressState(0);
65 if (ed.getParam('spellchecker_report_no_misspellings', true))
66 ed.windowManager.alert('spellchecker.no_mpell');
73 if (ed.settings.content_css !== false)
74 ed.contentCSS.push(url + '/css/content.css');
76 ed.onClick.add(t._showMenu, t);
77 ed.onContextMenu.add(t._showMenu, t);
78 ed.onBeforeGetContent.add(function() {
83 ed.onNodeChange.add(function(ed, cm) {
84 cm.setActive('spellchecker', t.active);
87 ed.onSetContent.add(function() {
91 ed.onBeforeGetContent.add(function() {
95 ed.onBeforeExecCommand.add(function(ed, cmd) {
96 if (cmd == 'mceFullScreen')
100 // Find selected language
102 each(ed.getParam('spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv', 'hash'), function(v, k) {
103 if (k.indexOf('+') === 0) {
112 createControl : function(n, cm) {
113 var t = this, c, ed = t.editor;
115 if (n == 'spellchecker') {
116 // Use basic button if we use the native spellchecker
117 if (t.rpcUrl == '{backend}') {
118 // Create simple toggle button if we have native support
120 c = cm.createButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
125 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
127 c.onRenderMenu.add(function(c, m) {
128 m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
129 each(t.languages, function(v, k) {
130 var o = {icon : 1}, mi;
132 o.onclick = function() {
133 if (v == t.selectedLang) {
137 t.selectedItem.setSelected(0);
144 mi.setSelected(v == t.selectedLang);
146 if (v == t.selectedLang)
155 // Internal functions
157 _walk : function(n, f) {
158 var d = this.editor.getDoc(), w;
160 if (d.createTreeWalker) {
161 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);
163 while ((n = w.nextNode()) != null)
166 tinymce.walk(n, f, 'childNodes');
169 _getSeparators : function() {
170 var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');
172 // Build word separator regexp
173 for (i=0; i<str.length; i++)
174 re += '\\' + str.charAt(i);
179 _getWords : function() {
180 var ed = this.editor, wl = [], tx = '', lo = {}, rawWords = [];
183 this._walk(ed.getBody(), function(n) {
185 tx += n.nodeValue + ' ';
188 // split the text up into individual words
189 if (ed.getParam('spellchecker_word_pattern')) {
190 // look for words that match the pattern
191 rawWords = tx.match('(' + ed.getParam('spellchecker_word_pattern') + ')', 'gi');
193 // Split words by separator
194 tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');
195 tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));
196 rawWords = tx.split(' ');
199 // Build word array and remove duplicates
200 each(rawWords, function(v) {
210 _removeWords : function(w) {
211 var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();
213 each(dom.select('span').reverse(), function(n) {
214 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {
215 if (!w || dom.decode(n.innerHTML) == w)
220 se.moveToBookmark(b);
223 _markWords : function(wl) {
224 var ed = this.editor, dom = ed.dom, doc = ed.getDoc(), se = ed.selection, b = se.getBookmark(), nl = [],
225 w = wl.join('|'), re = this._getSeparators(), rx = new RegExp('(^|[' + re + '])(' + w + ')(?=[' + re + ']|$)', 'g');
227 // Collect all text nodes
228 this._walk(ed.getBody(), function(n) {
229 if (n.nodeType == 3) {
234 // Wrap incorrect words in spans
235 each(nl, function(n) {
236 var node, elem, txt, pos, v = n.nodeValue;
239 // Encode the content
241 // Create container element
242 elem = dom.create('span', {'class' : 'mceItemHidden'});
244 // Following code fixes IE issues by creating text nodes
245 // using DOM methods instead of innerHTML.
246 // Bug #3124: <PRE> elements content is broken after spellchecking.
247 // Bug #1408: Preceding whitespace characters are removed
248 // @TODO: I'm not sure that both are still issues on IE9.
250 // Enclose mispelled words with temporal tag
251 v = v.replace(rx, '$1<mcespell>$2</mcespell>');
252 // Loop over the content finding mispelled words
253 while ((pos = v.indexOf('<mcespell>')) != -1) {
254 // Add text node for the content before the word
255 txt = v.substring(0, pos);
257 node = doc.createTextNode(dom.decode(txt));
258 elem.appendChild(node);
260 v = v.substring(pos+10);
261 pos = v.indexOf('</mcespell>');
262 txt = v.substring(0, pos);
263 v = v.substring(pos+11);
264 // Add span element for the word
265 elem.appendChild(dom.create('span', {'class' : 'mceItemHiddenSpellWord'}, txt));
267 // Add text node for the rest of the content
269 node = doc.createTextNode(dom.decode(v));
270 elem.appendChild(node);
273 // Other browsers preserve whitespace characters on innerHTML usage
274 elem.innerHTML = v.replace(rx, '$1<span class="mceItemHiddenSpellWord">$2</span>');
277 // Finally, replace the node with the container
278 dom.replace(elem, n);
282 se.moveToBookmark(b);
285 _showMenu : function(ed, e) {
286 var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin()), wordSpan = e.target;
288 e = 0; // Fixes IE memory leak
291 m = ed.controlManager.createDropMenu('spellcheckermenu', {'class' : 'mceNoIcons'});
295 if (dom.hasClass(wordSpan, 'mceItemHiddenSpellWord')) {
297 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
299 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(wordSpan.innerHTML)], function(r) {
305 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
306 each(r, function(v) {
307 m.add({title : v, onclick : function() {
308 dom.replace(ed.getDoc().createTextNode(v), wordSpan);
315 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
317 ignoreRpc = t.editor.getParam("spellchecker_enable_ignore_rpc", '');
319 title : 'spellchecker.ignore_word',
320 onclick : function() {
321 var word = wordSpan.innerHTML;
323 dom.remove(wordSpan, 1);
326 // tell the server if we need to
328 ed.setProgressState(1);
329 t._sendRPC('ignoreWord', [t.selectedLang, word], function(r) {
330 ed.setProgressState(0);
337 title : 'spellchecker.ignore_words',
338 onclick : function() {
339 var word = wordSpan.innerHTML;
341 t._removeWords(dom.decode(word));
344 // tell the server if we need to
346 ed.setProgressState(1);
347 t._sendRPC('ignoreWords', [t.selectedLang, word], function(r) {
348 ed.setProgressState(0);
354 if (t.editor.getParam("spellchecker_enable_learn_rpc")) {
356 title : 'spellchecker.learn_word',
357 onclick : function() {
358 var word = wordSpan.innerHTML;
360 dom.remove(wordSpan, 1);
363 ed.setProgressState(1);
364 t._sendRPC('learnWord', [t.selectedLang, word], function(r) {
365 ed.setProgressState(0);
374 p1 = DOM.getPos(ed.getContentAreaContainer());
375 m.settings.offset_x = p1.x;
376 m.settings.offset_y = p1.y;
378 ed.selection.select(wordSpan);
379 p1 = dom.getPos(wordSpan);
380 m.showMenu(p1.x, p1.y + wordSpan.offsetHeight - vp.y);
382 return tinymce.dom.Event.cancel(e);
387 _checkDone : function() {
388 var t = this, ed = t.editor, dom = ed.dom, o;
390 each(dom.select('span'), function(n) {
391 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {
402 var t = this, la = t.active;
412 t.editor.nodeChanged();
416 _sendRPC : function(m, p, cb) {
419 JSONRequest.sendRPC({
424 error : function(e, x) {
425 t.editor.setProgressState(0);
426 t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));
433 tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);