wordcount.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global HTMLElement, setTimeout, document */
  6. import WordCount from '../src/wordcount';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  12. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  13. describe( 'WordCount', () => {
  14. testUtils.createSinonSandbox();
  15. let wordCountPlugin, editor, model;
  16. beforeEach( () => {
  17. return VirtualTestEditor.create( {
  18. plugins: [ WordCount, Paragraph ]
  19. } )
  20. .then( _editor => {
  21. editor = _editor;
  22. model = editor.model;
  23. wordCountPlugin = editor.plugins.get( 'WordCount' );
  24. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  25. } );
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'has defined "words" property', () => {
  29. expect( wordCountPlugin.words ).to.equal( 0 );
  30. } );
  31. it( 'has defined "characters" property', () => {
  32. expect( wordCountPlugin.characters ).to.equal( 0 );
  33. } );
  34. it( 'has defined "_outputView" property', () => {
  35. expect( wordCountPlugin._outputView ).to.be.undefined;
  36. } );
  37. it( 'has "WordCount" plugin name', () => {
  38. expect( WordCount.pluginName ).to.equal( 'WordCount' );
  39. } );
  40. } );
  41. describe( 'functionality', () => {
  42. it( 'counts words', () => {
  43. expect( wordCountPlugin.words ).to.equal( 0 );
  44. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  45. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' +
  46. '<paragraph>1234</paragraph>' +
  47. '<paragraph>(-@#$%^*())</paragraph>' );
  48. wordCountPlugin._calculateWordsAndCharacters();
  49. expect( wordCountPlugin.words ).to.equal( 6 );
  50. } );
  51. it( 'counts characters', () => {
  52. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  53. wordCountPlugin._calculateWordsAndCharacters();
  54. expect( wordCountPlugin.characters ).to.equal( 12 );
  55. } );
  56. describe( 'update event', () => {
  57. it( 'fires update event with actual amount of characters and words', () => {
  58. const fake = sinon.fake();
  59. wordCountPlugin.on( 'update', fake );
  60. wordCountPlugin._calculateWordsAndCharacters();
  61. sinon.assert.calledOnce( fake );
  62. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 0, characters: 0 } );
  63. // _calculateWordsAndCharacters is throttled, so for this test case is run manually
  64. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  65. wordCountPlugin._calculateWordsAndCharacters();
  66. sinon.assert.calledTwice( fake );
  67. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 2, characters: 12 } );
  68. } );
  69. } );
  70. } );
  71. describe( 'self-updating element', () => {
  72. let container;
  73. beforeEach( () => {
  74. container = wordCountPlugin.getWordCountContainer();
  75. } );
  76. it( 'provides html element', () => {
  77. expect( container ).to.be.instanceof( HTMLElement );
  78. } );
  79. it( 'provided element has proper structure', () => {
  80. expect( container.tagName ).to.equal( 'DIV' );
  81. expect( container.classList.contains( 'ck' ) ).to.be.true;
  82. expect( container.classList.contains( 'ck-word-count' ) ).to.be.true;
  83. const children = Array.from( container.children );
  84. expect( children.length ).to.equal( 2 );
  85. expect( children[ 0 ].tagName ).to.equal( 'DIV' );
  86. expect( children[ 0 ].innerHTML ).to.equal( 'Words: 0' );
  87. expect( children[ 1 ].tagName ).to.equal( 'DIV' );
  88. expect( children[ 1 ].innerHTML ).to.equal( 'Characters: 0' );
  89. } );
  90. it( 'updates container content', () => {
  91. expect( container.innerText ).to.equal( 'Words: 0Characters: 0' );
  92. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  93. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  94. wordCountPlugin._calculateWordsAndCharacters();
  95. // There is \n between paragraph which has to be included into calculations
  96. expect( container.innerText ).to.equal( 'Words: 5Characters: 24' );
  97. } );
  98. it( 'subsequent calls provides the same element', () => {
  99. const newContainer = wordCountPlugin.getWordCountContainer();
  100. expect( container ).to.equal( newContainer );
  101. } );
  102. describe( 'destroy()', () => {
  103. it( 'html element is removed', done => {
  104. const frag = document.createDocumentFragment();
  105. frag.appendChild( container );
  106. expect( frag.querySelector( '*' ) ).to.be.instanceof( HTMLElement );
  107. editor.destroy()
  108. .then( () => {
  109. expect( frag.querySelector( '*' ) ).to.be.null;
  110. } )
  111. .then( done )
  112. .catch( done );
  113. } );
  114. it( 'method is called', done => {
  115. const spy = sinon.spy( wordCountPlugin, 'destroy' );
  116. editor.destroy()
  117. .then( () => {
  118. sinon.assert.calledOnce( spy );
  119. } )
  120. .then( done )
  121. .catch( done );
  122. } );
  123. } );
  124. } );
  125. describe( '_calculateWordsAndCharacters and throttle', () => {
  126. beforeEach( done => {
  127. // We need to flush initial throttle value after editor's initialization
  128. setTimeout( done, 255 );
  129. } );
  130. it( 'gets update after model data change', done => {
  131. const fake = sinon.fake();
  132. wordCountPlugin.on( 'update', fake );
  133. // Initial change in model should be immediately reflected in word-count
  134. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  135. sinon.assert.calledOnce( fake );
  136. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  137. // Subsequent updates should be throttle and run with last parameters
  138. setTimeout( () => {
  139. sinon.assert.calledTwice( fake );
  140. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  141. done();
  142. }, 255 );
  143. setModelData( model, '<paragraph>Hello world</paragraph>' );
  144. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  145. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  146. } );
  147. it( 'is not update after selection change', done => {
  148. setModelData( model, '<paragraph>Hello[] world.</paragraph>' );
  149. const fake = sinon.fake();
  150. const fakeSelectionChange = sinon.fake();
  151. wordCountPlugin.on( 'update', fake );
  152. model.document.on( 'change', fakeSelectionChange );
  153. model.change( writer => {
  154. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 1 ] ) );
  155. writer.setSelection( range );
  156. } );
  157. model.change( writer => {
  158. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 10 ] ) );
  159. writer.setSelection( range );
  160. } );
  161. setTimeout( () => {
  162. sinon.assert.notCalled( fake );
  163. sinon.assert.called( fakeSelectionChange );
  164. done();
  165. }, 255 );
  166. } );
  167. } );
  168. describe( 'custom config options', () => {
  169. it( 'displayWords = false', done => {
  170. VirtualTestEditor.create( {
  171. plugins: [ WordCount, Paragraph ],
  172. wordCount: {
  173. displayWords: false
  174. }
  175. } )
  176. .then( editor => {
  177. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  178. const container = wordCountPlugin.getWordCountContainer();
  179. expect( container.innerText ).to.equal( 'Characters: 0' );
  180. } )
  181. .then( done )
  182. .catch( done );
  183. } );
  184. it( 'displayCharacters = false', done => {
  185. VirtualTestEditor.create( {
  186. plugins: [ WordCount, Paragraph ],
  187. wordCount: {
  188. displayCharacters: false
  189. }
  190. } )
  191. .then( editor => {
  192. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  193. const container = wordCountPlugin.getWordCountContainer();
  194. expect( container.innerText ).to.equal( 'Words: 0' );
  195. } )
  196. .then( done )
  197. .catch( done );
  198. } );
  199. } );
  200. describe( 'translations', () => {
  201. before( () => {
  202. addTranslations( 'pl', {
  203. 'Words: %0': 'Słowa: %0',
  204. 'Characters: %0': 'Znaki: %0'
  205. } );
  206. addTranslations( 'en', {
  207. 'Words: %0': 'Words: %0',
  208. 'Characters: %0': 'Characters: %0'
  209. } );
  210. } );
  211. after( () => {
  212. clearTranslations();
  213. } );
  214. it( 'applies proper language translations', done => {
  215. VirtualTestEditor.create( {
  216. plugins: [ WordCount, Paragraph ],
  217. language: 'pl'
  218. } )
  219. .then( editor => {
  220. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  221. const container = wordCountPlugin.getWordCountContainer();
  222. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  223. } )
  224. .then( done )
  225. .catch( done );
  226. } );
  227. } );
  228. } );