Upgrade TinyMCE to v3.4.5
[citadel.git] / webcit / tiny_mce / plugins / wordcount / editor_plugin_src.js
index 5cb92fa0f08f406a1f0ff9a29bc12fa27c371f84..e94743bae1abb7c5c7d0b98420cd5edfcb35f97e 100644 (file)
@@ -9,7 +9,7 @@
  */\r
 \r
 (function() {\r
-    tinymce.create('tinymce.plugins.WordCount', {\r
+       tinymce.create('tinymce.plugins.WordCount', {\r
                block : 0,\r
                id : null,\r
                countre : null,\r
@@ -18,8 +18,8 @@
                init : function(ed, url) {\r
                        var t = this, last = 0;\r
 \r
-                       t.countre = ed.getParam('wordcount_countregex', /\S\s+/g);\r
-                       t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$¿'"_+=\\\/-]*/g);\r
+                       t.countre = ed.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’\r
+                       t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);\r
                        t.id = ed.id + '-word-count';\r
 \r
                        ed.onPostRender.add(function(ed, cm) {\r
 \r
                                        if (row)\r
                                                tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');\r
-                               } else\r
+                               } else {\r
                                        tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');\r
+                               }\r
                        });\r
 \r
-            ed.onInit.add(function(ed) {\r
+                       ed.onInit.add(function(ed) {\r
                                ed.selection.onSetContent.add(function() {\r
                                        t._count(ed);\r
                                });\r
                        });\r
                },\r
 \r
+               _getCount : function(ed) {\r
+                       var tc = 0;\r
+                       var tx = ed.getContent({ format: 'raw' });\r
+\r
+                       if (tx) {\r
+                                       tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces\r
+                                       tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars\r
+\r
+                                       // deal with html entities\r
+                                       tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');\r
+                                       tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation\r
+\r
+                                       var wordArray = tx.match(this.countre);\r
+                                       if (wordArray) {\r
+                                                       tc = wordArray.length;\r
+                                       }\r
+                       }\r
+\r
+                       return tc;\r
+               },\r
+\r
                _count : function(ed) {\r
-                       var t = this, tc = 0;\r
+                       var t = this;\r
 \r
                        // Keep multiple calls from happening at the same time\r
                        if (t.block)\r
                        t.block = 1;\r
 \r
                        setTimeout(function() {\r
-                               var tx = ed.getContent({format : 'raw'});\r
-\r
-                               if (tx) {\r
-                                       tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars\r
-                                       tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation\r
-                                       tx.replace(t.countre, function() {tc++;}); // count the words\r
+                               if (!ed.destroyed) {\r
+                                       var tc = t._getCount(ed);\r
+                                       tinymce.DOM.setHTML(t.id, tc.toString());\r
+                                       setTimeout(function() {t.block = 0;}, 2000);\r
                                }\r
-\r
-                               tinymce.DOM.setHTML(t.id, tc.toString());\r
-\r
-                               setTimeout(function() {t.block = 0;}, 2000);\r
                        }, 1);\r
                },\r
 \r
-        getInfo: function() {\r
+               getInfo: function() {\r
                        return {\r
                                longname : 'Word Count plugin',\r
                                author : 'Moxiecode Systems AB',\r
                                infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',\r
                                version : tinymce.majorVersion + "." + tinymce.minorVersion\r
                        };\r
-        }\r
-    });\r
+               }\r
+       });\r
 \r
-    tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
+       tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
 })();\r