Make WordPress Core

Ticket #37718: word-count.patch

File word-count.patch, 6.8 KB (added by jipmoors, 10 years ago)

word-count.js documented

  • 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
    110( 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         */
    246        function WordCounter( settings ) {
    347                var key,
    448                        shortcodes;
    549
     50                // Apply provided settings to object settings.
    651                if ( settings ) {
    752                        for ( key in settings ) {
     53                                // Only apply valid settings.
    854                                if ( settings.hasOwnProperty( key ) ) {
    955                                        this.settings[ key ] = settings[ key ];
    1056                                }
     
    1359
    1460                shortcodes = this.settings.l10n.shortcodes;
    1561
     62                // If there are any localization shortcodes, add this as type in the settings.
    1663                if ( shortcodes && shortcodes.length ) {
    1764                        this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
    1865                }
    1966        }
    2067
     68        // Default settings.
    2169        WordCounter.prototype.settings = {
    2270                HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
    2371                HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
    2472                spaceRegExp: /&nbsp;|&#160;/gi,
    2573                HTMLEntityRegExp: /&\S+?;/g,
     74                // \u2014 = em-dash
    2675                connectorRegExp: /--|\u2014/g,
    2776                removeRegExp: new RegExp( [
    2877                        '[',
     
    59108                                '\u2E00-\u2E7F',
    60109                        ']'
    61110                ].join( '' ), 'g' ),
     111                // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
    62112                astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
    63113                wordsRegExp: /\S\s+/g,
    64114                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                 */
    65126                characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
    66127                l10n: window.wordCountL10n || {}
    67128        };
    68129
     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         */
    69143        WordCounter.prototype.count = function( text, type ) {
    70144                var count = 0;
    71145
     146                // Use default type if none was provided.
    72147                type = type || this.settings.l10n.type;
    73148
     149                // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'.
    74150                if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
    75151                        type = 'words';
    76152                }
    77153
     154                // If we have any text at all.
    78155                if ( text ) {
    79156                        text = text + '\n';
    80157
     158                        // Replace all HTML with a new-line.
    81159                        text = text.replace( this.settings.HTMLRegExp, '\n' );
     160                        // Remove all HTML comments.
    82161                        text = text.replace( this.settings.HTMLcommentRegExp, '' );
    83162
     163                        // If a shortcode regular expression has been provided use it to remove shortcodes.
    84164                        if ( this.settings.shortcodesRegExp ) {
    85165                                text = text.replace( this.settings.shortcodesRegExp, '\n' );
    86166                        }
    87167
     168                        // Normalize non-breaking space to a normal space.
    88169                        text = text.replace( this.settings.spaceRegExp, ' ' );
    89170
    90171                        if ( type === 'words' ) {
     172                                // Remove HTML Entities.
    91173                                text = text.replace( this.settings.HTMLEntityRegExp, '' );
     174                                // Convert connectors to spaces to count attached text as words.
    92175                                text = text.replace( this.settings.connectorRegExp, ' ' );
     176                                // Remove unwanted characters.
    93177                                text = text.replace( this.settings.removeRegExp, '' );
    94178                        } else {
     179                                // Convert HTML Entities to "a".
    95180                                text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
     181                                // Remove surrogate points.
    96182                                text = text.replace( this.settings.astralRegExp, 'a' );
    97183                        }
    98184
     185                        // Match with the selected type regular expression to count the items.
    99186                        text = text.match( this.settings[ type + 'RegExp' ] );
    100187
     188                        // If we have any matches, set the count to the number of items found.
    101189                        if ( text ) {
    102190                                count = text.length;
    103191                        }