wordcount.js 9.6 KB

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