8
0
Просмотр исходного кода

Improve words detection, remove enter from characters included in output, fix tests.

Mateusz Samsel 6 лет назад
Родитель
Сommit
95830bab86

+ 8 - 2
packages/ckeditor5-word-count/src/wordcount.js

@@ -12,6 +12,7 @@ import { modelElementToPlainText } from './utils';
 import { throttle, isElement } from 'lodash-es';
 import View from '@ckeditor/ckeditor5-ui/src/view';
 import Template from '@ckeditor/ckeditor5-ui/src/template';
+import featureDetection from '@ckeditor/ckeditor5-utils/src/featuredetection';
 
 /**
  * The word count plugin.
@@ -229,9 +230,14 @@ export default class WordCount extends Plugin {
 	_calculateWordsAndCharacters() {
 		const txt = modelElementToPlainText( this.editor.model.document.getRoot() );
 
-		this.characters = txt.length;
+		this.characters = txt.replace( /\n/g, '' ).length;
 
-		this.words = ( txt.match( /[_a-zA-Z0-9À-ž]+/gu ) || [] ).length;
+		const wordsMatch = featureDetection.isUnicodePropertySupported ?
+			txt.match( new RegExp( '[\\p{L}\\p{N}\\p{M}\\p{Pd}\\p{Pc}]+', 'gu' ) ) :
+			/* istanbul ignore next */
+			txt.match( /[_\-a-zA-Z0-9À-ž]+/gu );
+
+		this.words = ( wordsMatch || [] ).length;
 
 		this.fire( 'update', {
 			words: this.words,

+ 27 - 3
packages/ckeditor5-word-count/tests/wordcount.js

@@ -13,6 +13,8 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
+import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
+import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
 
 // Delay related to word-count throttling.
 const DELAY = 255;
@@ -24,7 +26,7 @@ describe( 'WordCount', () => {
 
 	beforeEach( () => {
 		return VirtualTestEditor.create( {
-			plugins: [ WordCount, Paragraph ]
+			plugins: [ WordCount, Paragraph, ShiftEnter, TableEditing ]
 		} )
 			.then( _editor => {
 				editor = _editor;
@@ -56,7 +58,7 @@ describe( 'WordCount', () => {
 			setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
 				'<paragraph><$text foo="true">Hello</$text> world.</paragraph>' +
 				'<paragraph>1234</paragraph>' +
-				'<paragraph>(-@#$%^*())</paragraph>' );
+				'<paragraph>(@#$%^*())</paragraph>' );
 
 			wordCountPlugin._calculateWordsAndCharacters();
 
@@ -71,6 +73,28 @@ describe( 'WordCount', () => {
 			expect( wordCountPlugin.characters ).to.equal( 12 );
 		} );
 
+		it( 'should not count enter as a character', () => {
+			expect( wordCountPlugin.characters ).to.equal( 0 );
+
+			setModelData( model, '<paragraph>Fo<softBreak></softBreak>o</paragraph>' +
+				'<paragraph>Foo</paragraph>' +
+				'<table>' +
+				'<tableRow>' +
+					'<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
+				'</tableRow>' +
+				'<tableRow>' +
+					'<tableCell></tableCell><tableCell><paragraph>foo</paragraph></tableCell><tableCell></tableCell>' +
+				'</tableRow>' +
+				'<tableRow>' +
+					'<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
+				'</tableRow>' +
+				'</table>' );
+
+			wordCountPlugin._calculateWordsAndCharacters();
+
+			expect( wordCountPlugin.characters ).to.equal( 9 );
+		} );
+
 		describe( 'update event', () => {
 			it( 'fires update event with actual amount of characters and words', () => {
 				const fake = sinon.fake();
@@ -123,7 +147,7 @@ describe( 'WordCount', () => {
 			wordCountPlugin._calculateWordsAndCharacters();
 
 			// There is \n between paragraph which has to be included into calculations
-			expect( container.innerText ).to.equal( 'Words: 5Characters: 24' );
+			expect( container.innerText ).to.equal( 'Words: 5Characters: 23' );
 		} );
 
 		it( 'subsequent calls provides the same element', () => {