2 function WordCounter( settings ) {
7 for ( key in settings ) {
8 if ( settings.hasOwnProperty( key ) ) {
9 this.settings[ key ] = settings[ key ];
14 shortcodes = this.settings.l10n.shortcodes;
16 if ( shortcodes && shortcodes.length ) {
17 this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
21 WordCounter.prototype.settings = {
22 HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
23 HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
24 spaceRegExp: / | /gi,
25 HTMLEntityRegExp: /&\S+?;/g,
26 connectorRegExp: /--|\u2014/g,
27 removeRegExp: new RegExp( [
29 // Basic Latin (extract)
30 '\u0021-\u0040\u005B-\u0060\u007B-\u007E',
31 // Latin-1 Supplement (extract)
32 '\u0080-\u00BF\u00D7\u00F7',
33 // General Punctuation
34 // Superscripts and Subscripts
36 // Combining Diacritical Marks for Symbols
40 // Mathematical Operators
41 // Miscellaneous Technical
43 // Optical Character Recognition
44 // Enclosed Alphanumerics
48 // Miscellaneous Symbols
50 // Miscellaneous Mathematical Symbols-A
51 // Supplemental Arrows-A
53 // Supplemental Arrows-B
54 // Miscellaneous Mathematical Symbols-B
55 // Supplemental Mathematical Operators
56 // Miscellaneous Symbols and Arrows
58 // Supplemental Punctuation
62 astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
63 wordsRegExp: /\S\s+/g,
64 characters_excluding_spacesRegExp: /\S/g,
65 characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
66 l10n: window.wordCountL10n || {}
69 WordCounter.prototype.count = function( text, type ) {
72 type = type || this.settings.l10n.type;
74 if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
81 text = text.replace( this.settings.HTMLRegExp, '\n' );
82 text = text.replace( this.settings.HTMLcommentRegExp, '' );
84 if ( this.settings.shortcodesRegExp ) {
85 text = text.replace( this.settings.shortcodesRegExp, '\n' );
88 text = text.replace( this.settings.spaceRegExp, ' ' );
90 if ( type === 'words' ) {
91 text = text.replace( this.settings.HTMLEntityRegExp, '' );
92 text = text.replace( this.settings.connectorRegExp, ' ' );
93 text = text.replace( this.settings.removeRegExp, '' );
95 text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
96 text = text.replace( this.settings.astralRegExp, 'a' );
99 text = text.match( this.settings[ type + 'RegExp' ] );
109 window.wp = window.wp || {};
110 window.wp.utils = window.wp.utils || {};
111 window.wp.utils.WordCounter = WordCounter;