35 * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters |
36 * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters |
36 * including spaces. |
37 * including spaces. |
37 * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes. |
38 * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes. |
38 * @param {Object} settings.l10n Optional. Localization object containing specific |
39 * @param {Object} settings.l10n Optional. Localization object containing specific |
39 * configuration for the current localization. |
40 * configuration for the current localization. |
40 * @param {String} settings.l10n.type Optional. Method of finding words to count. |
41 * @param {string} settings.l10n.type Optional. Method of finding words to count. |
41 * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed |
42 * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed |
42 * from the text. |
43 * from the text. |
43 * |
44 * |
44 * @return void |
45 * @return {void} |
45 */ |
46 */ |
46 function WordCounter( settings ) { |
47 function WordCounter( settings ) { |
47 var key, |
48 var key, |
48 shortcodes; |
49 shortcodes; |
49 |
50 |
71 HTMLRegExp: /<\/?[a-z][^>]*?>/gi, |
72 HTMLRegExp: /<\/?[a-z][^>]*?>/gi, |
72 HTMLcommentRegExp: /<!--[\s\S]*?-->/g, |
73 HTMLcommentRegExp: /<!--[\s\S]*?-->/g, |
73 spaceRegExp: / | /gi, |
74 spaceRegExp: / | /gi, |
74 HTMLEntityRegExp: /&\S+?;/g, |
75 HTMLEntityRegExp: /&\S+?;/g, |
75 |
76 |
76 // \u2014 = em-dash |
77 // \u2014 = em-dash. |
77 connectorRegExp: /--|\u2014/g, |
78 connectorRegExp: /--|\u2014/g, |
78 |
79 |
79 // Characters to be removed from input text. |
80 // Characters to be removed from input text. |
80 removeRegExp: new RegExp( [ |
81 removeRegExp: new RegExp( [ |
81 '[', |
82 '[', |
82 |
83 |
83 // Basic Latin (extract) |
84 // Basic Latin (extract). |
84 '\u0021-\u0040\u005B-\u0060\u007B-\u007E', |
85 '\u0021-\u0040\u005B-\u0060\u007B-\u007E', |
85 |
86 |
86 // Latin-1 Supplement (extract) |
87 // Latin-1 Supplement (extract). |
87 '\u0080-\u00BF\u00D7\u00F7', |
88 '\u0080-\u00BF\u00D7\u00F7', |
88 |
89 |
89 /* |
90 /* |
90 * The following range consists of: |
91 * The following range consists of: |
91 * General Punctuation |
92 * General Punctuation |
141 }; |
142 }; |
142 |
143 |
143 /** |
144 /** |
144 * Counts the number of words (or other specified type) in the specified text. |
145 * Counts the number of words (or other specified type) in the specified text. |
145 * |
146 * |
146 * @since 2.6.0 |
147 * @since 2.6.0 |
|
148 * |
147 * @memberof wp.utils.wordcounter |
149 * @memberof wp.utils.wordcounter |
148 * |
150 * |
149 * @param {String} text Text to count elements in. |
151 * @param {string} text Text to count elements in. |
150 * @param {String} type Optional. Specify type to use. |
152 * @param {string} type Optional. Specify type to use. |
151 * |
153 * |
152 * @return {Number} The number of items counted. |
154 * @return {number} The number of items counted. |
153 */ |
155 */ |
154 WordCounter.prototype.count = function( text, type ) { |
156 WordCounter.prototype.count = function( text, type ) { |
155 var count = 0; |
157 var count = 0; |
156 |
158 |
157 // Use default type if none was provided. |
159 // Use default type if none was provided. |