Skip to content

Commit e2fab60

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@33344 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 180d8b3 commit e2fab60

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/wp-admin/js/word-count.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
shortcodes = this.settings.l10n.shortcodes;
1515

1616
if ( shortcodes && shortcodes.length ) {
17-
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' );
17+
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
1818
}
1919
}
2020

2121
WordCounter.prototype.settings = {
2222
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
23+
HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
2324
spaceRegExp: /&nbsp;|&#160;/gi,
24-
connectorRegExp: /--|\u2014/gi,
25+
HTMLEntityRegExp: /&\S+?;/g,
26+
connectorRegExp: /--|\u2014/g,
2527
removeRegExp: new RegExp( [
2628
'[',
2729
// Basic Latin (extract)
@@ -60,19 +62,24 @@
6062
astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
6163
wordsRegExp: /\S\s+/g,
6264
charactersRegExp: /\S/g,
63-
allRegExp: /[^\f\n\r\t\v\u00ad\u2028\u2029]/g,
65+
allRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
6466
l10n: window.wordCountL10n || {}
6567
};
6668

6769
WordCounter.prototype.count = function( text, type ) {
6870
var count = 0;
6971

70-
type = type || this.settings.l10n.type || 'words';
72+
type = type || this.settings.l10n.type;
73+
74+
if ( type !== 'characters' && type !== 'all' ) {
75+
type = 'words';
76+
}
7177

7278
if ( text ) {
7379
text = text + '\n';
7480

7581
text = text.replace( this.settings.HTMLRegExp, '\n' );
82+
text = text.replace( this.settings.HTMLcommentRegExp, '' );
7683

7784
if ( this.settings.shortcodesRegExp ) {
7885
text = text.replace( this.settings.shortcodesRegExp, '\n' );
@@ -81,9 +88,11 @@
8188
text = text.replace( this.settings.spaceRegExp, ' ' );
8289

8390
if ( type === 'words' ) {
91+
text = text.replace( this.settings.HTMLEntityRegExp, '' );
8492
text = text.replace( this.settings.connectorRegExp, ' ' );
8593
text = text.replace( this.settings.removeRegExp, '' );
8694
} else {
95+
text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
8796
text = text.replace( this.settings.astralRegExp, 'a' );
8897
}
8998

tests/qunit/wp-admin/js/word-count.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@
5858
words: 1,
5959
characters: 1,
6060
all: 1
61+
},
62+
{
63+
message: 'HTML comment.',
64+
string: 'one<!-- comment -->two three',
65+
words: 2,
66+
characters: 11,
67+
all: 12
68+
},
69+
{
70+
message: 'HTML entity.',
71+
string: '&gt; test',
72+
words: 1,
73+
characters: 5,
74+
all: 6
6175
}
6276
], function( test ) {
6377
_.each( [ 'words', 'characters', 'all' ], function( type ) {

0 commit comments

Comments
 (0)