Przeglądaj źródła

rename method to more general one, move regexp to section which is not update with every change event. Fix unit test.

Mateusz Samsel 6 lat temu
rodzic
commit
94bce13e5b

+ 22 - 17
packages/ckeditor5-word-count/src/wordcount.js

@@ -105,6 +105,24 @@ export default class WordCount extends Plugin {
 		 * @type {module:ui/view~View}
 		 */
 		this._outputView;
+
+		/**
+		 * The regular expression used to recognize words in the editor's content.
+		 *
+		 * @readonly
+		 * @private
+		 * @type {RegExp}
+		 */
+		this._wordsMatchRegExp = env.features.isRegExpUnicodePropertySupported ?
+			// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
+			// Groups:
+			// {L} - Any kind of letter from any language.
+			// {N} - Any kind of numeric character in any script.
+			// {M} - A character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.).
+			// {Pd} - Any kind of hyphen or dash.
+			// {Pc} - A punctuation character such as an underscore that connects words.
+			new RegExp( '[\\p{L}\\p{N}\\p{M}\\p{Pd}\\p{Pc}]+', 'gu' ) :
+			/[_\-a-zA-Z0-9À-ž]+/gu;
 	}
 
 	/**
@@ -120,7 +138,7 @@ export default class WordCount extends Plugin {
 	init() {
 		const editor = this.editor;
 
-		editor.model.document.on( 'change:data', throttle( this._calculateWordsAndCharacters.bind( this ), 250 ) );
+		editor.model.document.on( 'change:data', throttle( this._refreshStats.bind( this ), 250 ) );
 
 		if ( typeof this._config.onUpdate == 'function' ) {
 			this.on( 'update', ( evt, data ) => {
@@ -222,29 +240,16 @@ export default class WordCount extends Plugin {
 
 	/**
 	 * Determines the number of words and characters in the current editor's model and assigns it to {@link #characters} and {@link #words}.
-	 * It also fires {@link #event:update}.
+	 * It also fires an {@link #event:update}.
 	 *
 	 * @private
 	 * @fires update
 	 */
-	_calculateWordsAndCharacters() {
+	_refreshStats() {
 		const txt = modelElementToPlainText( this.editor.model.document.getRoot() );
+		const detectedWords = txt.match( this._wordsMatchRegExp ) || [];
 
 		this.characters = txt.replace( /\n/g, '' ).length;
-
-		// Groups:
-		// {L} - Any kind of letter from any language.
-		// {N} - Any kind of numeric character in any script.
-		// {M} - A character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.).
-		// {Pd} - Any kind of hyphen or dash.
-		// {Pc} - A punctuation character such as an underscore that connects words.
-		const wordsMatchRegExp = env.features.isRegExpUnicodePropertySupported ?
-			// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
-			new RegExp( '[\\p{L}\\p{N}\\p{M}\\p{Pd}\\p{Pc}]+', 'gu' ) :
-			/[_\-a-zA-Z0-9À-ž]+/gu;
-
-		const detectedWords = txt.match( wordsMatchRegExp ) || [];
-
 		this.words = detectedWords.length;
 
 		this.fire( 'update', {

+ 10 - 10
packages/ckeditor5-word-count/tests/wordcount.js

@@ -61,7 +61,7 @@ describe( 'WordCount', () => {
 				'<paragraph>1234</paragraph>' +
 				'<paragraph>(@#$%^*())</paragraph>' );
 
-			wordCountPlugin._calculateWordsAndCharacters();
+			wordCountPlugin._refreshStats();
 
 			expect( wordCountPlugin.words ).to.equal( 6 );
 		} );
@@ -69,7 +69,7 @@ describe( 'WordCount', () => {
 		it( 'counts characters', () => {
 			setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
 
-			wordCountPlugin._calculateWordsAndCharacters();
+			wordCountPlugin._refreshStats();
 
 			expect( wordCountPlugin.characters ).to.equal( 12 );
 		} );
@@ -91,7 +91,7 @@ describe( 'WordCount', () => {
 				'</tableRow>' +
 				'</table>' );
 
-			wordCountPlugin._calculateWordsAndCharacters();
+			wordCountPlugin._refreshStats();
 
 			expect( wordCountPlugin.characters ).to.equal( 9 );
 		} );
@@ -104,7 +104,7 @@ describe( 'WordCount', () => {
 			expect( wordCountPlugin.words ).to.equal( 0 );
 
 			setModelData( model, '<paragraph>שמש 太陽 ดวงอาทิตย์ شمس ਸੂਰਜ słońce</paragraph>' );
-			wordCountPlugin._calculateWordsAndCharacters();
+			wordCountPlugin._refreshStats();
 
 			expect( wordCountPlugin.words ).to.equal( 6 );
 		} );
@@ -124,7 +124,7 @@ describe( 'WordCount', () => {
 				expect( wordCountPlugin.words ).to.equal( 0 );
 
 				setModelData( model, '<paragraph>hello world.</paragraph>' );
-				wordCountPlugin._calculateWordsAndCharacters();
+				wordCountPlugin._refreshStats();
 
 				expect( wordCountPlugin.words ).to.equal( 2 );
 			} );
@@ -135,14 +135,14 @@ describe( 'WordCount', () => {
 				const fake = sinon.fake();
 				wordCountPlugin.on( 'update', fake );
 
-				wordCountPlugin._calculateWordsAndCharacters();
+				wordCountPlugin._refreshStats();
 
 				sinon.assert.calledOnce( fake );
 				sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 0, characters: 0 } );
 
-				// _calculateWordsAndCharacters is throttled, so for this test case is run manually
+				// _refreshStats is throttled, so for this test case is run manually
 				setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
-				wordCountPlugin._calculateWordsAndCharacters();
+				wordCountPlugin._refreshStats();
 
 				sinon.assert.calledTwice( fake );
 				sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 2, characters: 12 } );
@@ -179,7 +179,7 @@ describe( 'WordCount', () => {
 			setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
 				'<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
 
-			wordCountPlugin._calculateWordsAndCharacters();
+			wordCountPlugin._refreshStats();
 
 			expect( container.innerText ).to.equal( 'Words: 5Characters: 23' );
 		} );
@@ -228,7 +228,7 @@ describe( 'WordCount', () => {
 		} );
 	} );
 
-	describe( '_calculateWordsAndCharacters and throttle', () => {
+	describe( '_refreshStats and throttle', () => {
 		beforeEach( done => {
 			// We need to flush initial throttle value after editor's initialization
 			setTimeout( done, DELAY );