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
This commit is contained in:
parent
180d8b3eb0
commit
e2fab60c51
@ -14,14 +14,16 @@
|
|||||||
shortcodes = this.settings.l10n.shortcodes;
|
shortcodes = this.settings.l10n.shortcodes;
|
||||||
|
|
||||||
if ( shortcodes && shortcodes.length ) {
|
if ( shortcodes && shortcodes.length ) {
|
||||||
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' );
|
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WordCounter.prototype.settings = {
|
WordCounter.prototype.settings = {
|
||||||
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
|
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
|
||||||
|
HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
|
||||||
spaceRegExp: / | /gi,
|
spaceRegExp: / | /gi,
|
||||||
connectorRegExp: /--|\u2014/gi,
|
HTMLEntityRegExp: /&\S+?;/g,
|
||||||
|
connectorRegExp: /--|\u2014/g,
|
||||||
removeRegExp: new RegExp( [
|
removeRegExp: new RegExp( [
|
||||||
'[',
|
'[',
|
||||||
// Basic Latin (extract)
|
// Basic Latin (extract)
|
||||||
@ -60,19 +62,24 @@
|
|||||||
astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
|
astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
|
||||||
wordsRegExp: /\S\s+/g,
|
wordsRegExp: /\S\s+/g,
|
||||||
charactersRegExp: /\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 || {}
|
l10n: window.wordCountL10n || {}
|
||||||
};
|
};
|
||||||
|
|
||||||
WordCounter.prototype.count = function( text, type ) {
|
WordCounter.prototype.count = function( text, type ) {
|
||||||
var count = 0;
|
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 ) {
|
if ( text ) {
|
||||||
text = text + '\n';
|
text = text + '\n';
|
||||||
|
|
||||||
text = text.replace( this.settings.HTMLRegExp, '\n' );
|
text = text.replace( this.settings.HTMLRegExp, '\n' );
|
||||||
|
text = text.replace( this.settings.HTMLcommentRegExp, '' );
|
||||||
|
|
||||||
if ( this.settings.shortcodesRegExp ) {
|
if ( this.settings.shortcodesRegExp ) {
|
||||||
text = text.replace( this.settings.shortcodesRegExp, '\n' );
|
text = text.replace( this.settings.shortcodesRegExp, '\n' );
|
||||||
@ -81,9 +88,11 @@
|
|||||||
text = text.replace( this.settings.spaceRegExp, ' ' );
|
text = text.replace( this.settings.spaceRegExp, ' ' );
|
||||||
|
|
||||||
if ( type === 'words' ) {
|
if ( type === 'words' ) {
|
||||||
|
text = text.replace( this.settings.HTMLEntityRegExp, '' );
|
||||||
text = text.replace( this.settings.connectorRegExp, ' ' );
|
text = text.replace( this.settings.connectorRegExp, ' ' );
|
||||||
text = text.replace( this.settings.removeRegExp, '' );
|
text = text.replace( this.settings.removeRegExp, '' );
|
||||||
} else {
|
} else {
|
||||||
|
text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
|
||||||
text = text.replace( this.settings.astralRegExp, 'a' );
|
text = text.replace( this.settings.astralRegExp, 'a' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +58,20 @@
|
|||||||
words: 1,
|
words: 1,
|
||||||
characters: 1,
|
characters: 1,
|
||||||
all: 1
|
all: 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: 'HTML comment.',
|
||||||
|
string: 'one<!-- comment -->two three',
|
||||||
|
words: 2,
|
||||||
|
characters: 11,
|
||||||
|
all: 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: 'HTML entity.',
|
||||||
|
string: '> test',
|
||||||
|
words: 1,
|
||||||
|
characters: 5,
|
||||||
|
all: 6
|
||||||
}
|
}
|
||||||
], function( test ) {
|
], function( test ) {
|
||||||
_.each( [ 'words', 'characters', 'all' ], function( type ) {
|
_.each( [ 'words', 'characters', 'all' ], function( type ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user