4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 /*global tinymce:true */
13 tinymce.PluginManager.add('wordcount', function(editor) {
14 var self = this, countre, cleanre;
16 countre = editor.getParam('wordcount_countregex', /[\w\u2019\x27\-]+/g); // u2019 == ’
17 cleanre = editor.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\x27\x22_+=\\\/\-]*/g);
20 editor.theme.panel.find('#wordcount').text(['Words: {0}', self.getCount()]);
23 editor.on('init', function() {
24 var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0];
27 window.setTimeout(function() {
31 text: ['Words: {0}', self.getCount()],
35 editor.on('setcontent beforeaddundo', update);
37 editor.on('keyup', function(e) {
38 if (e.keyCode == 32) {
46 self.getCount = function() {
47 var tx = editor.getContent({format: 'raw'});
51 tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces
52 tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars
54 // deal with html entities
55 tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
56 tx = tx.replace(cleanre, ''); // remove numbers and punctuation
58 var wordArray = tx.match(countre);
60 tc = wordArray.length;