Ticket #37718: word-count.patch
| File word-count.patch, 6.8 KB (added by , 10 years ago) |
|---|
-
src/wp-admin/js/word-count.js
diff --git src/wp-admin/js/word-count.js src/wp-admin/js/word-count.js index c0255fd..b32d227 100644
1 /** 2 * Word or character counting functionality. Count words or characters in a provided text string. 3 * 4 * @summary Count words or characters in a text. 5 * 6 * @namespace wp.utils 7 * @since 2.6 8 */ 9 1 10 ( function() { 11 /** 12 * Word counting utility 13 * 14 * @namespace wp.utils.wordcounter 15 * @memberof wp.utils 16 * 17 * @class 18 * 19 * @param {Object} settings Optional. Key-value object containing overrides for 20 * settings. 21 * @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements. 22 * @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments. 23 * @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space 24 * characters. 25 * @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities. 26 * @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that 27 * split words. 28 * @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted 29 * characters to reduce false-positives. 30 * @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted 31 * characters when searching for non-words. 32 * @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces. 33 * @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which 34 * are non-spaces. 35 * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters 36 * including spaces. 37 * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes. 38 * @param {Object} settings.l10n Optional. Localization object containing specific 39 * configuration for the current localization. 40 * @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 * from the text. 43 * 44 * @return void 45 */ 2 46 function WordCounter( settings ) { 3 47 var key, 4 48 shortcodes; 5 49 50 // Apply provided settings to object settings. 6 51 if ( settings ) { 7 52 for ( key in settings ) { 53 // Only apply valid settings. 8 54 if ( settings.hasOwnProperty( key ) ) { 9 55 this.settings[ key ] = settings[ key ]; 10 56 } … … 13 59 14 60 shortcodes = this.settings.l10n.shortcodes; 15 61 62 // If there are any localization shortcodes, add this as type in the settings. 16 63 if ( shortcodes && shortcodes.length ) { 17 64 this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' ); 18 65 } 19 66 } 20 67 68 // Default settings. 21 69 WordCounter.prototype.settings = { 22 70 HTMLRegExp: /<\/?[a-z][^>]*?>/gi, 23 71 HTMLcommentRegExp: /<!--[\s\S]*?-->/g, 24 72 spaceRegExp: / | /gi, 25 73 HTMLEntityRegExp: /&\S+?;/g, 74 // \u2014 = em-dash 26 75 connectorRegExp: /--|\u2014/g, 27 76 removeRegExp: new RegExp( [ 28 77 '[', … … 59 108 '\u2E00-\u2E7F', 60 109 ']' 61 110 ].join( '' ), 'g' ), 111 // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF 62 112 astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, 63 113 wordsRegExp: /\S\s+/g, 64 114 characters_excluding_spacesRegExp: /\S/g, 115 /* 116 * Match anything that is not a formatting character, excluding: 117 * \f = form feed 118 * \n = new line 119 * \r = carriage return 120 * \t = tab 121 * \v = vertical tab 122 * \u00AD = soft hyphen 123 * \u2028 = line separator 124 * \u2029 = paragraph separator 125 */ 65 126 characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, 66 127 l10n: window.wordCountL10n || {} 67 128 }; 68 129 130 /** 131 * Counts the number of words (or other specified type) in the specified text. 132 * 133 * @summary Count the number of elements in a text. 134 * 135 * @since 2.6 136 * @memberof wp.utils.wordcounter 137 * 138 * @param {String} text Text to count elements in. 139 * @param {String} type Optional. Specify type to use. 140 * 141 * @return {Number} The number of items counted. 142 */ 69 143 WordCounter.prototype.count = function( text, type ) { 70 144 var count = 0; 71 145 146 // Use default type if none was provided. 72 147 type = type || this.settings.l10n.type; 73 148 149 // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'. 74 150 if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) { 75 151 type = 'words'; 76 152 } 77 153 154 // If we have any text at all. 78 155 if ( text ) { 79 156 text = text + '\n'; 80 157 158 // Replace all HTML with a new-line. 81 159 text = text.replace( this.settings.HTMLRegExp, '\n' ); 160 // Remove all HTML comments. 82 161 text = text.replace( this.settings.HTMLcommentRegExp, '' ); 83 162 163 // If a shortcode regular expression has been provided use it to remove shortcodes. 84 164 if ( this.settings.shortcodesRegExp ) { 85 165 text = text.replace( this.settings.shortcodesRegExp, '\n' ); 86 166 } 87 167 168 // Normalize non-breaking space to a normal space. 88 169 text = text.replace( this.settings.spaceRegExp, ' ' ); 89 170 90 171 if ( type === 'words' ) { 172 // Remove HTML Entities. 91 173 text = text.replace( this.settings.HTMLEntityRegExp, '' ); 174 // Convert connectors to spaces to count attached text as words. 92 175 text = text.replace( this.settings.connectorRegExp, ' ' ); 176 // Remove unwanted characters. 93 177 text = text.replace( this.settings.removeRegExp, '' ); 94 178 } else { 179 // Convert HTML Entities to "a". 95 180 text = text.replace( this.settings.HTMLEntityRegExp, 'a' ); 181 // Remove surrogate points. 96 182 text = text.replace( this.settings.astralRegExp, 'a' ); 97 183 } 98 184 185 // Match with the selected type regular expression to count the items. 99 186 text = text.match( this.settings[ type + 'RegExp' ] ); 100 187 188 // If we have any matches, set the count to the number of items found. 101 189 if ( text ) { 102 190 count = text.length; 103 191 }