8
0

wordcount.js 9.8 KB

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