wordcount.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. it( 'should not throw an error if container is not specified', () => {
  123. return VirtualTestEditor.create( {
  124. plugins: [ WordCount, Paragraph ]
  125. } )
  126. .then( editor => {
  127. return editor.destroy();
  128. } );
  129. } );
  130. } );
  131. } );
  132. describe( '_calculateWordsAndCharacters and throttle', () => {
  133. beforeEach( done => {
  134. // We need to flush initial throttle value after editor's initialization
  135. setTimeout( done, DELAY );
  136. } );
  137. it( 'gets update after model data change', done => {
  138. const fake = sinon.fake();
  139. wordCountPlugin.on( 'update', fake );
  140. // Initial change in model should be immediately reflected in word-count
  141. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  142. sinon.assert.calledOnce( fake );
  143. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  144. // Subsequent updates should be throttle and run with last parameters
  145. setTimeout( () => {
  146. sinon.assert.calledTwice( fake );
  147. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  148. done();
  149. }, DELAY );
  150. setModelData( model, '<paragraph>Hello world</paragraph>' );
  151. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  152. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  153. } );
  154. it( 'is not update after selection change', done => {
  155. setModelData( model, '<paragraph>Hello[] world.</paragraph>' );
  156. const fake = sinon.fake();
  157. const fakeSelectionChange = sinon.fake();
  158. wordCountPlugin.on( 'update', fake );
  159. model.document.on( 'change', fakeSelectionChange );
  160. model.change( writer => {
  161. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 1 ] ) );
  162. writer.setSelection( range );
  163. } );
  164. model.change( writer => {
  165. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 10 ] ) );
  166. writer.setSelection( range );
  167. } );
  168. setTimeout( () => {
  169. sinon.assert.notCalled( fake );
  170. sinon.assert.called( fakeSelectionChange );
  171. done();
  172. }, DELAY );
  173. } );
  174. } );
  175. describe( 'custom config options', () => {
  176. it( 'displayWords = false', done => {
  177. VirtualTestEditor.create( {
  178. plugins: [ WordCount, Paragraph ],
  179. wordCount: {
  180. displayWords: false
  181. }
  182. } )
  183. .then( editor => {
  184. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  185. const container = wordCountPlugin.wordCountContainer;
  186. expect( container.innerText ).to.equal( 'Characters: 0' );
  187. } )
  188. .then( done )
  189. .catch( done );
  190. } );
  191. it( 'displayCharacters = false', done => {
  192. VirtualTestEditor.create( {
  193. plugins: [ WordCount, Paragraph ],
  194. wordCount: {
  195. displayCharacters: false
  196. }
  197. } )
  198. .then( editor => {
  199. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  200. const container = wordCountPlugin.wordCountContainer;
  201. expect( container.innerText ).to.equal( 'Words: 0' );
  202. } )
  203. .then( done )
  204. .catch( done );
  205. } );
  206. it( 'should call function registered under config.wordCount.onUpdate', () => {
  207. const fake = sinon.fake();
  208. return VirtualTestEditor.create( {
  209. plugins: [ WordCount, Paragraph ],
  210. wordCount: {
  211. onUpdate: fake
  212. }
  213. } )
  214. .then( editor => {
  215. sinon.assert.calledWithExactly( fake, { words: 0, characters: 0 } );
  216. setModelData( editor.model, '<paragraph>Foo Bar</paragraph>' );
  217. } )
  218. .then( () => new Promise( resolve => {
  219. setTimeout( resolve, DELAY );
  220. } ) )
  221. .then( () => {
  222. sinon.assert.calledWithExactly( fake, { words: 2, characters: 7 } );
  223. } );
  224. } );
  225. it( 'should append word count container in element referenced in config.wordCount.container', () => {
  226. const element = document.createElement( 'div' );
  227. expect( element.children.length ).to.equal( 0 );
  228. return VirtualTestEditor.create( {
  229. plugins: [ WordCount, Paragraph ],
  230. wordCount: {
  231. container: element
  232. }
  233. } )
  234. .then( editor => {
  235. expect( element.children.length ).to.equal( 1 );
  236. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  237. expect( element.firstElementChild ).to.equal( wordCountPlugin.wordCountContainer );
  238. } );
  239. } );
  240. } );
  241. describe( 'translations', () => {
  242. before( () => {
  243. addTranslations( 'pl', {
  244. 'Words: %0': 'Słowa: %0',
  245. 'Characters: %0': 'Znaki: %0'
  246. } );
  247. addTranslations( 'en', {
  248. 'Words: %0': 'Words: %0',
  249. 'Characters: %0': 'Characters: %0'
  250. } );
  251. } );
  252. after( () => {
  253. clearTranslations();
  254. } );
  255. it( 'applies proper language translations', done => {
  256. VirtualTestEditor.create( {
  257. plugins: [ WordCount, Paragraph ],
  258. language: 'pl'
  259. } )
  260. .then( editor => {
  261. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  262. const container = wordCountPlugin.wordCountContainer;
  263. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  264. } )
  265. .then( done )
  266. .catch( done );
  267. } );
  268. } );
  269. } );