Ticket #30966: 30966.2.patch
File 30966.2.patch, 11.0 KB (added by , 10 years ago) |
---|
-
src/wp-admin/js/post.js
376 376 jQuery(document).ready( function($) { 377 377 var stamp, visibility, $submitButtons, updateVisibility, updateText, 378 378 sticky = '', 379 last = 0,380 co = $('#content'),381 379 $document = $(document), 382 380 $editSlugWrap = $('#edit-slug-box'), 383 381 postId = $('#post_ID').val() || 0, … … 956 954 }); 957 955 } 958 956 959 // word count960 if ( typeof(wpWordCount) != 'undefined' ) {961 $document.triggerHandler('wpcountwords', [ co.val() ]);962 963 co.keyup( function(e) {964 var k = e.keyCode || e.charCode;965 966 if ( k == last )967 return true;968 969 if ( 13 == k || 8 == last || 46 == last )970 $document.triggerHandler('wpcountwords', [ co.val() ]);971 972 last = k;973 return true;974 });975 }976 977 957 wptitlehint = function(id) { 978 958 id = id || 'title'; 979 959 … … 1092 1072 }); 1093 1073 } 1094 1074 }); 1075 1076 ( function( $, wordCounter ) { 1077 $( function() { 1078 var counter = new wordCounter(), 1079 $content = $( '#content' ), 1080 $count = $( '#wp-word-count' ).find( '.word-count' ), 1081 prevCount = 0, 1082 contentEditor; 1083 1084 function update() { 1085 var text, count; 1086 1087 if ( ! contentEditor || contentEditor.isHidden() ) { 1088 text = $content.val(); 1089 } else { 1090 text = contentEditor.getContent( { format: 'raw' } ); 1091 } 1092 1093 count = counter.count( text ); 1094 1095 if ( count !== prevCount ) { 1096 $count.text( count ); 1097 } 1098 } 1099 1100 $( document ).on( 'tinymce-editor-init', function( event, editor ) { 1101 if ( editor.id !== 'content' ) { 1102 return; 1103 } 1104 1105 contentEditor = editor; 1106 1107 editor.on( 'nodechange keyup', _.debounce( update, 500 ) ); 1108 } ); 1109 1110 $content.on( 'input keyup', _.debounce( update, 500 ) ); 1111 1112 update(); 1113 } ); 1114 } )( jQuery, 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 contractMore: '', 32 expand: [ 33 // Extract form "Basic Latin" 34 '\u0021-\u0040\u005B-\u0060\u007B-\u007E', 35 // Extract from "Latin-1 Supplement" 36 '\u00A1-\u00BF\u00D7\u00F7', 37 // "Combining Diacritical Marks" 38 '\u0300-\u036F', 39 // Punctuation, symbols, operators... 40 '\u2000-\u2BFF', 41 // "Supplemental Punctuation" 42 '\u2E00-\u2E7F' 43 ].join( '' ), 44 expandMore: '', 45 l10n: window.wordCountL10n || {} 46 }; 47 48 wordCounter.prototype.count = function( text, type ) { 49 var count = 0; 50 51 type = type || this.settings.l10n.type || 'words'; 52 53 if ( text ) { 54 text = ' ' + text + ' '; 55 56 text = text.replace( this.settings.excludeHTMLRegExp, ' ' ); 57 text = text.replace( this.settings.HTMLRegExp, ' ' ); 58 text = text.replace( this.settings.shortcodeRegExp, ' ' ); 59 text = text.replace( this.settings.spaceRegExp, ' ' ); 60 text = text.replace( this.settings.HTMLEntitiesRegExp, '' ); 61 // en/em dash shorthand is an exception 62 text = text.replace( /--/g, ' ' ); 63 text = text.replace( this.settings.contractRegExp, '' ); 64 text = text.replace( this.settings.expandRegExp, ' ' ); 65 66 text = text.match( this.settings[ type + 'RegExp' ] ); 67 68 if ( text ) { 69 count = text.length; 70 } 71 } 72 73 return count; 39 74 }; 40 75 41 $(document).bind( 'wpcountwords', function(e, txt) {42 wpWordCount.wc(txt);43 });44 } (jQuery));76 window.wp = window.wp || {}; 77 window.wp.utils = window.wp.utils || {}; 78 window.wp.utils.wordCounter = wordCounter; 79 } )(); -
src/wp-includes/js/tinymce/plugins/wordpress/plugin.js
4 4 tinymce.ui.FloatPanel.zIndex = 100100; 5 5 6 6 tinymce.PluginManager.add( 'wordpress', function( editor ) { 7 var DOM = tinymce.DOM, wpAdvButton, modKey, style, 8 last = 0; 7 var DOM = tinymce.DOM, wpAdvButton, modKey, style; 9 8 10 9 if ( typeof window.jQuery !== 'undefined' ) { 11 10 window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); … … 360 359 } 361 360 }); 362 361 363 // Word count364 if ( typeof window.jQuery !== 'undefined' ) {365 editor.on( 'keyup', function( e ) {366 var key = e.keyCode || e.charCode;367 368 if ( key === last ) {369 return;370 }371 372 if ( 13 === key || 8 === last || 46 === last ) {373 window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );374 }375 376 last = key;377 });378 }379 380 362 editor.on( 'SaveContent', function( e ) { 381 363 // If editor is hidden, we just want the textarea's value to be saved 382 364 if ( ! editor.inline && editor.isHidden() ) { -
src/wp-includes/script-loader.php
368 368 369 369 $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 ); 370 370 371 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery'), false, 1 );371 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); 372 372 did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( 373 373 /* translators: If your word count is based on single characters (East Asian characters), 374 374 enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ 375 'type' => 'characters' == _x( 'words', 'word count: words or characters?' ) ? 'c' : 'w', 375 'type' => _x( 'words', 'word count: words or characters?' ), 376 'shortcodes' => array_keys( $GLOBALS['shortcode_tags'] ) 376 377 ) ); 377 378 378 379 $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 ); … … 438 439 439 440 $scripts->add( 'postbox', "/wp-admin/js/postbox$suffix.js", array('jquery-ui-sortable'), false, 1 ); 440 441 441 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox' ), false, 1 );442 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox', 'underscore', 'word-count'), false, 1 ); 442 443 did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 443 444 'ok' => __('OK'), 444 445 'cancel' => __('Cancel'), -
tests/qunit/index.html
34 34 <script src="../../src/wp-includes/js/customize-models.js"></script> 35 35 <script src="../../src/wp-includes/js/shortcode.js"></script> 36 36 <script src="../../src/wp-admin/js/customize-controls.js"></script> 37 <script src="../../src/wp-admin/js/word-count.js"></script> 37 38 38 39 <!-- Unit tests --> 39 40 <script src="wp-admin/js/password-strength-meter.js"></script> … … 42 43 <script src="wp-includes/js/shortcode.js"></script> 43 44 <script src="wp-admin/js/customize-controls.js"></script> 44 45 <script src="wp-admin/js/customize-controls-utils.js"></script> 46 <script src="wp-admin/js/word-count.js"></script> 45 47 </div> 46 48 </body> 47 49 </html> -
tests/qunit/wp-admin/js/word-count.js
1 jQuery( function() { 2 module( 'word-count' ); 3 4 var wordCount = new wp.utils.wordCounter( { 5 l10n: { 6 shortcodes: [ 'shortcode' ] 7 } 8 } ); 9 10 test( 'all', function() { 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 equal( wordCount.count( tests[ i ].string ), tests[ i ].wordCount ); 78 equal( wordCount.count( tests[ i ].string, 'characters' ), tests[ i ].charCount ); 79 } 80 } ); 81 } );