From 50159974c9dff73fcfa1015e5eaf3a9b14e4e65c Mon Sep 17 00:00:00 2001 From: Ella Iseulde Van Dorpe <*@iseulde.com> Date: Tue, 21 Jul 2015 15:24:24 +0000 Subject: [PATCH] Editor: word count: exclude HTML comments and entities Also make sure `type` is one of the three allowed types and remove unnecessary regex flags. See #30966. Built from https://develop.svn.wordpress.org/trunk@33344 git-svn-id: http://core.svn.wordpress.org/trunk@33316 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/word-count.js | 17 +++++++++++++---- wp-admin/js/word-count.min.js | 2 +- wp-includes/version.php | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/wp-admin/js/word-count.js b/wp-admin/js/word-count.js index 025412e346..c8cfb2f52d 100644 --- a/wp-admin/js/word-count.js +++ b/wp-admin/js/word-count.js @@ -14,14 +14,16 @@ shortcodes = this.settings.l10n.shortcodes; if ( shortcodes && shortcodes.length ) { - this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' ); + this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' ); } } WordCounter.prototype.settings = { HTMLRegExp: /<\/?[a-z][^>]*?>/gi, + HTMLcommentRegExp: //g, spaceRegExp: / | /gi, - connectorRegExp: /--|\u2014/gi, + HTMLEntityRegExp: /&\S+?;/g, + connectorRegExp: /--|\u2014/g, removeRegExp: new RegExp( [ '[', // Basic Latin (extract) @@ -60,19 +62,24 @@ astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, wordsRegExp: /\S\s+/g, charactersRegExp: /\S/g, - allRegExp: /[^\f\n\r\t\v\u00ad\u2028\u2029]/g, + allRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, l10n: window.wordCountL10n || {} }; WordCounter.prototype.count = function( text, type ) { var count = 0; - type = type || this.settings.l10n.type || 'words'; + type = type || this.settings.l10n.type; + + if ( type !== 'characters' && type !== 'all' ) { + type = 'words'; + } if ( text ) { text = text + '\n'; text = text.replace( this.settings.HTMLRegExp, '\n' ); + text = text.replace( this.settings.HTMLcommentRegExp, '' ); if ( this.settings.shortcodesRegExp ) { text = text.replace( this.settings.shortcodesRegExp, '\n' ); @@ -81,9 +88,11 @@ text = text.replace( this.settings.spaceRegExp, ' ' ); if ( type === 'words' ) { + text = text.replace( this.settings.HTMLEntityRegExp, '' ); text = text.replace( this.settings.connectorRegExp, ' ' ); text = text.replace( this.settings.removeRegExp, '' ); } else { + text = text.replace( this.settings.HTMLEntityRegExp, 'a' ); text = text.replace( this.settings.astralRegExp, 'a' ); } diff --git a/wp-admin/js/word-count.min.js b/wp-admin/js/word-count.min.js index 2bca545f40..0b16f0d8b7 100644 --- a/wp-admin/js/word-count.min.js +++ b/wp-admin/js/word-count.min.js @@ -1 +1 @@ -!function(){function a(a){var b,c;if(a)for(b in a)a.hasOwnProperty(b)&&(this.settings[b]=a[b]);c=this.settings.l10n.shortcodes,c&&c.length&&(this.settings.shortcodesRegExp=new RegExp("\\[\\/?(?:"+c.join("|")+")[^\\]]*?\\]","gi"))}a.prototype.settings={HTMLRegExp:/<\/?[a-z][^>]*?>/gi,spaceRegExp:/ | /gi,connectorRegExp:/--|\u2014/gi,removeRegExp:new RegExp(["[","!-@[-`{-~","\x80-\xbf\xd7\xf7","\u2000-\u2bff","\u2e00-\u2e7f","]"].join(""),"g"),astralRegExp:/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,wordsRegExp:/\S\s+/g,charactersRegExp:/\S/g,allRegExp:/[^\f\n\r\t\v\u00ad\u2028\u2029]/g,l10n:window.wordCountL10n||{}},a.prototype.count=function(a,b){var c=0;return b=b||this.settings.l10n.type||"words",a&&(a+="\n",a=a.replace(this.settings.HTMLRegExp,"\n"),this.settings.shortcodesRegExp&&(a=a.replace(this.settings.shortcodesRegExp,"\n")),a=a.replace(this.settings.spaceRegExp," "),"words"===b?(a=a.replace(this.settings.connectorRegExp," "),a=a.replace(this.settings.removeRegExp,"")):a=a.replace(this.settings.astralRegExp,"a"),a=a.match(this.settings[b+"RegExp"]),a&&(c=a.length)),c},window.wp=window.wp||{},window.wp.utils=window.wp.utils||{},window.wp.utils.WordCounter=a}(); \ No newline at end of file +!function(){function a(a){var b,c;if(a)for(b in a)a.hasOwnProperty(b)&&(this.settings[b]=a[b]);c=this.settings.l10n.shortcodes,c&&c.length&&(this.settings.shortcodesRegExp=new RegExp("\\[\\/?(?:"+c.join("|")+")[^\\]]*?\\]","g"))}a.prototype.settings={HTMLRegExp:/<\/?[a-z][^>]*?>/gi,HTMLcommentRegExp://g,spaceRegExp:/ | /gi,HTMLEntityRegExp:/&\S+?;/g,connectorRegExp:/--|\u2014/g,removeRegExp:new RegExp(["[","!-@[-`{-~","\x80-\xbf\xd7\xf7","\u2000-\u2bff","\u2e00-\u2e7f","]"].join(""),"g"),astralRegExp:/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,wordsRegExp:/\S\s+/g,charactersRegExp:/\S/g,allRegExp:/[^\f\n\r\t\v\u00AD\u2028\u2029]/g,l10n:window.wordCountL10n||{}},a.prototype.count=function(a,b){var c=0;return b=b||this.settings.l10n.type,"characters"!==b&&"all"!==b&&(b="words"),a&&(a+="\n",a=a.replace(this.settings.HTMLRegExp,"\n"),a=a.replace(this.settings.HTMLcommentRegExp,""),this.settings.shortcodesRegExp&&(a=a.replace(this.settings.shortcodesRegExp,"\n")),a=a.replace(this.settings.spaceRegExp," "),"words"===b?(a=a.replace(this.settings.HTMLEntityRegExp,""),a=a.replace(this.settings.connectorRegExp," "),a=a.replace(this.settings.removeRegExp,"")):(a=a.replace(this.settings.HTMLEntityRegExp,"a"),a=a.replace(this.settings.astralRegExp,"a")),a=a.match(this.settings[b+"RegExp"]),a&&(c=a.length)),c},window.wp=window.wp||{},window.wp.utils=window.wp.utils||{},window.wp.utils.WordCounter=a}(); \ No newline at end of file diff --git a/wp-includes/version.php b/wp-includes/version.php index d32beec285..2e3367ac43 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta3-33343'; +$wp_version = '4.3-beta3-33344'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.