Ticket #30966: 30966.15.patch
File 30966.15.patch, 5.4 KB (added by , 9 years ago) |
---|
-
src/wp-admin/js/word-count.js
1 ( function( ) {1 ( function( window, wp, l10n ) { 2 2 function WordCounter( settings ) { 3 3 var key, 4 4 shortcodes; … … 13 13 14 14 shortcodes = this.settings.l10n.shortcodes; 15 15 16 if ( shortcodes && shortcodes.length ) {16 if ( shortcodes.length ) { 17 17 this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' ); 18 18 } 19 19 } … … 60 60 ']' 61 61 ].join( '' ), 'g' ), 62 62 astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, 63 wordsRegExp: /\S\s+/g, 64 charactersRegExp: /\S/g, 65 allRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, 66 l10n: window.wordCountL10n || {} 63 l10n: l10n || {} 67 64 }; 68 65 69 WordCounter.prototype.count = function( text, type) {66 WordCounter.prototype.count = function( text, characters ) { 70 67 var count = 0; 71 68 72 type = type || this.settings.l10n.type; 73 74 if ( type !== 'characters' && type !== 'all' ) { 75 type = 'words'; 69 if ( characters == null ) { 70 characters = this.settings.l10n.characters; 76 71 } 77 72 78 73 if ( text ) { … … 87 82 88 83 text = text.replace( this.settings.spaceRegExp, ' ' ); 89 84 90 if ( type === 'words' ) { 85 if ( characters ) { 86 text = text.replace( this.settings.HTMLEntityRegExp, 'a' ); 87 text = text.replace( this.settings.astralRegExp, 'a' ); 88 } else { 91 89 text = text.replace( this.settings.HTMLEntityRegExp, '' ); 92 90 text = text.replace( this.settings.connectorRegExp, ' ' ); 93 91 text = text.replace( this.settings.removeRegExp, '' ); 94 } else {95 text = text.replace( this.settings.HTMLEntityRegExp, 'a' );96 text = text.replace( this.settings.astralRegExp, 'a' );97 92 } 98 93 99 text = text.match( this.settings[ type + 'RegExp' ]);94 text = text.match( characters ? /[^\f\n\r\t\v\u00AD\u2028\u2029]/g : /\S\s+/g ); 100 95 101 96 if ( text ) { 102 97 count = text.length; … … 106 101 return count; 107 102 }; 108 103 109 w indow.wp = window.wp || {};110 w indow.wp.utils = window.wp.utils || {};111 w indow.wp.utils.WordCounter = WordCounter;112 } )( );104 wp = wp || {}; 105 wp.utils = wp.utils || {}; 106 wp.utils.WordCounter = WordCounter; 107 } )( window, window.wp, window.wordCountL10n ); -
src/wp-includes/script-loader.php
402 402 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); 403 403 did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( 404 404 /* translators: If your word count is based on single characters (East Asian characters), 405 enter 'characters', or 'all' to include spaces. Otherwise, enter 'words'. 406 Do not translate into your own language. */ 407 'type' => _x( 'words', 'word count: words, characters or all?' ), 405 enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ 406 'characters' => _x( 'words', 'word count: words or characters?' ) === 'characters', 408 407 'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array() 409 408 ) ); 410 409 -
tests/qunit/wp-admin/js/word-count.js
7 7 message: 'Basic test.', 8 8 string: 'one two three', 9 9 words: 3, 10 characters: 11, 11 all: 13 10 characters: 13 12 11 }, 13 12 { 14 13 message: 'HTML tags.', 15 14 string: 'one <em class="test">two</em><br />three', 16 15 words: 3, 17 characters: 11, 18 all: 12 16 characters: 12 19 17 }, 20 18 { 21 19 message: 'Line breaks.', 22 20 string: 'one\ntwo\nthree', 23 21 words: 3, 24 characters: 11, 25 all: 11 22 characters: 11 26 23 }, 27 24 { 28 25 message: 'Encoded spaces.', 29 26 string: 'one two three', 30 27 words: 3, 31 characters: 11, 32 all: 13 28 characters: 13 33 29 }, 34 30 { 35 31 message: 'Punctuation.', 36 32 string: 'It\'s two three \u2026 4?', 37 33 words: 3, 38 characters: 15, 39 all: 19 34 characters: 19 40 35 }, 41 36 { 42 37 message: 'Em dash.', 43 38 string: 'one\u2014two--three', 44 39 words: 3, 45 characters: 14, 46 all: 14 40 characters: 14 47 41 }, 48 42 { 49 43 message: 'Shortcodes.', 50 44 string: 'one [shortcode attribute="value"]two[/shortcode]three', 51 45 words: 3, 52 characters: 11, 53 all: 12 46 characters: 12 54 47 }, 55 48 { 56 49 message: 'Astrals.', 57 50 string: '\uD83D\uDCA9', 58 51 words: 1, 59 characters: 1, 60 all: 1 52 characters: 1 61 53 }, 62 54 { 63 55 message: 'HTML comment.', 64 56 string: 'one<!-- comment -->two three', 65 57 words: 2, 66 characters: 11, 67 all: 12 58 characters: 12 68 59 }, 69 60 { 70 61 message: 'HTML entity.', 71 62 string: '> test', 72 63 words: 1, 73 characters: 5, 74 all: 6 64 characters: 6 75 65 } 76 66 ], function( test ) { 77 _.each( [ 'words', 'characters' , 'all'], function( type ) {78 assert.equal( wordCounter.count( test.string, type ), test[ type ], test.message + ' (' + type + ')' );67 _.each( [ 'words', 'characters' ], function( type ) { 68 assert.equal( wordCounter.count( test.string, type === 'characters' ), test[ type ], test.message + ' (' + type + ')' ); 79 69 } ); 80 70 } ); 81 71 } );