Ticket #30966: 30966.5.patch
File 30966.5.patch, 11.2 KB (added by , 9 years ago) |
---|
-
Gruntfile.js
394 394 } 395 395 }, 396 396 uglify: { 397 options: { 398 ASCIIOnly: true 399 }, 397 400 core: { 398 401 expand: true, 399 402 cwd: SOURCE_DIR, -
src/wp-admin/js/post.js
203 203 jQuery(document).ready( function($) { 204 204 var stamp, visibility, $submitButtons, updateVisibility, updateText, 205 205 sticky = '', 206 last = 0,207 206 $textarea = $('#content'), 208 207 $document = $(document), 209 208 $editSlugWrap = $('#edit-slug-box'), … … 788 787 }); 789 788 } 790 789 791 // word count792 if ( typeof(wpWordCount) != 'undefined' ) {793 $document.triggerHandler('wpcountwords', [ $textarea.val() ]);794 795 $textarea.keyup( function(e) {796 var k = e.keyCode || e.charCode;797 798 if ( k == last )799 return true;800 801 if ( 13 == k || 8 == last || 46 == last )802 $document.triggerHandler('wpcountwords', [ $textarea.val() ]);803 804 last = k;805 return true;806 });807 }808 809 790 wptitlehint = function(id) { 810 791 id = id || 'title'; 811 792 … … 935 916 } 936 917 }); 937 918 }); 919 920 ( function( $, counter ) { 921 $( function() { 922 var $content = $( '#content' ), 923 $count = $( '#wp-word-count' ).find( '.word-count' ), 924 prevCount = 0, 925 contentEditor; 926 927 function update() { 928 var text, count; 929 930 if ( ! contentEditor || contentEditor.isHidden() ) { 931 text = $content.val(); 932 } else { 933 text = contentEditor.getContent( { format: 'raw' } ); 934 } 935 936 count = counter.count( text ); 937 938 if ( count !== prevCount ) { 939 $count.text( count ); 940 } 941 942 prevCount = count; 943 } 944 945 $( document ).on( 'tinymce-editor-init', function( event, editor ) { 946 if ( editor.id !== 'content' ) { 947 return; 948 } 949 950 contentEditor = editor; 951 952 editor.on( 'nodechange keyup', _.debounce( update, 500 ) ); 953 } ); 954 955 $content.on( 'input keyup', _.debounce( update, 500 ) ); 956 957 update(); 958 } ); 959 } )( jQuery, new wp.utils.WordCounter() ); -
src/wp-admin/js/word-count.js
1 /* global wordCountL10n */ 2 var wpWordCount; 3 (function($,undefined) { 4 wpWordCount = { 5 6 settings : { 7 strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags 8 clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc. 9 w : /\S\s+/g, // word-counting regexp 10 c : /\S/g // char-counting regexp for asian languages 11 }, 12 13 block : 0, 14 15 wc : function(tx, type) { 16 var t = this, w = $('.word-count'), tc = 0; 17 18 if ( type === undefined ) 19 type = wordCountL10n.type; 20 if ( type !== 'w' && type !== 'c' ) 21 type = 'w'; 22 23 if ( t.block ) 24 return; 25 26 t.block = 1; 27 28 setTimeout( function() { 29 if ( tx ) { 30 tx = tx.replace( t.settings.strip, ' ' ).replace( / | /gi, ' ' ); 31 tx = tx.replace( t.settings.clean, '' ); 32 tx.replace( t.settings[type], function(){tc++;} ); 1 ( function() { 2 function WordCounter( settings ) { 3 var key; 4 5 if ( settings ) { 6 for ( key in settings ) { 7 if ( settings.hasOwnProperty( key ) ) { 8 this.settings[ key ] = settings[ key ]; 33 9 } 34 w.html(tc.toString()); 10 } 11 } 35 12 36 setTimeout( function() { t.block = 0; }, 2000 );37 }, 1);13 if ( this.settings.l10n.shortcodes ) { 14 this.settings.shortcodeRegExp = new RegExp( '\\[\\/?(?:' + this.settings.l10n.shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' ); 38 15 } 16 17 this.settings.excludeHTMLRegExp = new RegExp( '<(' + this.settings.excludeHTML.join( '|' ) + ')[^>]*?>[\\s\\S]*?<\\/\\1>', 'gi' ); 18 this.settings.contractRegExp = new RegExp( '[' + this.settings.contract + ']', 'g' ); 19 this.settings.expandRegExp = new RegExp( '[' + this.settings.expand + ']', 'g' ); 20 } 21 22 WordCounter.prototype.settings = { 23 excludeHTML: [ 'code', 'form', 'noscript', 'script' ], 24 HTMLRegExp: /<\/?[a-z][^>]*?>/gi, 25 spaceRegExp: / | /gi, 26 HTMLEntitiesRegExp: /&#?[a-z0-9]+?;/gi, 27 wordsRegExp: /\S\s+/g, 28 charactersRegExp: /\S/g, 29 // Just the apostrophe and hyphen. 30 contract: '\'\u2019\\-\u2010\u2011', 31 expand: [ 32 // Extract form "Basic Latin". 33 '\u0021-\u0040\u005B-\u0060\u007B-\u007E', 34 // Extract from "Latin-1 Supplement". 35 '\u00A1-\u00BF\u00D7\u00F7', 36 // "Combining Diacritical Marks". 37 '\u0300-\u036F', 38 // Punctuation, symbols, operators... 39 '\u2000-\u2BFF', 40 // "Supplemental Punctuation". 41 '\u2E00-\u2E7F' 42 ].join( '' ), 43 l10n: window.wordCountL10n || {} 44 }; 45 46 WordCounter.prototype.count = function( text, type ) { 47 var count = 0; 48 49 type = type || this.settings.l10n.type || 'words'; 50 51 if ( text ) { 52 text = ' ' + text + ' '; 53 54 text = text.replace( this.settings.excludeHTMLRegExp, ' ' ); 55 text = text.replace( this.settings.HTMLRegExp, ' ' ); 56 text = text.replace( this.settings.shortcodeRegExp, ' ' ); 57 text = text.replace( this.settings.spaceRegExp, ' ' ); 58 text = text.replace( this.settings.HTMLEntitiesRegExp, '' ); 59 // en/em dash shorthand is an exception 60 text = text.replace( /--/g, ' ' ); 61 text = text.replace( this.settings.contractRegExp, '' ); 62 text = text.replace( this.settings.expandRegExp, ' ' ); 63 64 text = text.match( this.settings[ type + 'RegExp' ] ); 65 66 if ( text ) { 67 count = text.length; 68 } 69 } 70 71 return count; 39 72 }; 40 73 41 $(document).bind( 'wpcountwords', function(e, txt) {42 wpWordCount.wc(txt);43 });44 } (jQuery));74 window.wp = window.wp || {}; 75 window.wp.utils = window.wp.utils || {}; 76 window.wp.utils.WordCounter = WordCounter; 77 } )(); -
src/wp-includes/js/tinymce/plugins/wordpress/plugin.js
7 7 var DOM = tinymce.DOM, 8 8 each = tinymce.each, 9 9 __ = editor.editorManager.i18n.translate, 10 wpAdvButton, style, 11 last = 0; 10 wpAdvButton, style; 12 11 13 12 if ( typeof window.jQuery !== 'undefined' ) { 14 13 window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); … … 363 362 } 364 363 }); 365 364 366 // Word count367 if ( typeof window.jQuery !== 'undefined' ) {368 editor.on( 'keyup', function( e ) {369 var key = e.keyCode || e.charCode;370 371 if ( key === last ) {372 return;373 }374 375 if ( 13 === key || 8 === last || 46 === last ) {376 window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );377 }378 379 last = key;380 });381 }382 383 365 editor.on( 'SaveContent', function( e ) { 384 366 // If editor is hidden, we just want the textarea's value to be saved 385 367 if ( ! editor.inline && editor.isHidden() ) { -
src/wp-includes/script-loader.php
371 371 372 372 $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 ); 373 373 374 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery'), false, 1 );374 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); 375 375 did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( 376 376 /* translators: If your word count is based on single characters (East Asian characters), 377 377 enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ 378 'type' => 'characters' == _x( 'words', 'word count: words or characters?' ) ? 'c' : 'w', 378 'type' => _x( 'words', 'word count: words or characters?' ), 379 'shortcodes' => array_keys( $GLOBALS['shortcode_tags'] ) 379 380 ) ); 380 381 381 382 $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 ); … … 451 452 'tagDelimiter' => _x( ',', 'tag delimiter' ), 452 453 ) ); 453 454 454 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box' ), false, 1 );455 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count' ), false, 1 ); 455 456 did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 456 457 'ok' => __('OK'), 457 458 'cancel' => __('Cancel'), -
tests/qunit/index.html
32 32 <script src="../../src/wp-includes/js/customize-models.js"></script> 33 33 <script src="../../src/wp-includes/js/shortcode.js"></script> 34 34 <script src="../../src/wp-admin/js/customize-controls.js"></script> 35 <script src="../../src/wp-admin/js/word-count.js"></script> 35 36 36 37 <!-- Unit tests --> 37 38 <script src="wp-admin/js/password-strength-meter.js"></script> … … 40 41 <script src="wp-includes/js/shortcode.js"></script> 41 42 <script src="wp-admin/js/customize-controls.js"></script> 42 43 <script src="wp-admin/js/customize-controls-utils.js"></script> 44 <script src="wp-admin/js/word-count.js"></script> 43 45 44 46 <!-- Customizer templates for sections --> 45 47 <script type="text/html" id="tmpl-customize-section-default"> -
tests/qunit/wp-admin/js/word-count.js
1 ( function( QUnit ) { 2 var wordCounter = new window.wp.utils.WordCounter( { 3 l10n: { 4 shortcodes: [ 'shortcode' ] 5 } 6 } ); 7 8 QUnit.module( 'word-count' ); 9 10 QUnit.test( 'All.', function( assert ) { 11 var tests = [ 12 { 13 string: 'one two three', 14 wordCount: 3, 15 charCount: 11 16 }, 17 { 18 string: 'one <I> two </I> three', 19 wordCount: 3, 20 charCount: 11 21 }, 22 { 23 string: '<p class="class"> one two three </p>', 24 wordCount: 3, 25 charCount: 11 26 }, 27 { 28 string: 'one\ntwo\nthree', 29 wordCount: 3, 30 charCount: 11 31 }, 32 { 33 string: 'one two three', 34 wordCount: 3, 35 charCount: 11 36 }, 37 { 38 string: 'one two three ... 4 ?', 39 wordCount: 3, 40 charCount: 11 41 }, 42 { 43 string: '\u03BC\u1FC6\u03BD\u03B9\u03BD \u1F04\u03B5\u03B9\u03B4\u03B5 \u03B8\u03B5\u1F70', 44 wordCount: 3, 45 charCount: 13 46 }, 47 { 48 string: 'one two--three!', 49 wordCount: 3, 50 charCount: 11 51 }, 52 { 53 string: 'one two\u2013three!', 54 wordCount: 3, 55 charCount: 11 56 }, 57 { 58 string: 'one [shortcode attr="value"] two [/shortcode] three', 59 wordCount: 3, 60 charCount: 11 61 }, 62 { 63 string: 'It\'s two three', 64 wordCount: 3, 65 charCount: 11 66 }, 67 { 68 string: 'one two three<script>function script() {}</script>', 69 wordCount: 3, 70 charCount: 11 71 } 72 ]; 73 74 var i = tests.length; 75 76 while ( i-- ) { 77 assert.equal( wordCounter.count( tests[ i ].string ), tests[ i ].wordCount ); 78 assert.equal( wordCounter.count( tests[ i ].string, 'characters' ), tests[ i ].charCount ); 79 } 80 } ); 81 } )( window.QUnit );