wordcount.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  14. import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  15. import featureDetection from '@ckeditor/ckeditor5-utils/src/featuredetection';
  16. // Delay related to word-count throttling.
  17. const DELAY = 255;
  18. describe( 'WordCount', () => {
  19. testUtils.createSinonSandbox();
  20. let wordCountPlugin, editor, model;
  21. beforeEach( () => {
  22. return VirtualTestEditor.create( {
  23. plugins: [ WordCount, Paragraph, ShiftEnter, TableEditing ]
  24. } )
  25. .then( _editor => {
  26. editor = _editor;
  27. model = editor.model;
  28. wordCountPlugin = editor.plugins.get( 'WordCount' );
  29. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  30. } );
  31. } );
  32. describe( 'constructor()', () => {
  33. it( 'has defined "words" property', () => {
  34. expect( wordCountPlugin.words ).to.equal( 0 );
  35. } );
  36. it( 'has defined "characters" property', () => {
  37. expect( wordCountPlugin.characters ).to.equal( 0 );
  38. } );
  39. it( 'has "WordCount" plugin name', () => {
  40. expect( WordCount.pluginName ).to.equal( 'WordCount' );
  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. it( 'should not count enter as a character', () => {
  59. expect( wordCountPlugin.characters ).to.equal( 0 );
  60. setModelData( model, '<paragraph>Fo<softBreak></softBreak>o</paragraph>' +
  61. '<paragraph>Foo</paragraph>' +
  62. '<table>' +
  63. '<tableRow>' +
  64. '<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
  65. '</tableRow>' +
  66. '<tableRow>' +
  67. '<tableCell></tableCell><tableCell><paragraph>foo</paragraph></tableCell><tableCell></tableCell>' +
  68. '</tableRow>' +
  69. '<tableRow>' +
  70. '<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
  71. '</tableRow>' +
  72. '</table>' );
  73. wordCountPlugin._calculateWordsAndCharacters();
  74. expect( wordCountPlugin.characters ).to.equal( 9 );
  75. } );
  76. it( 'should count international words', function() {
  77. if ( !featureDetection.isUnicodePropertySupported ) {
  78. this.skip();
  79. }
  80. expect( wordCountPlugin.words ).to.equal( 0 );
  81. setModelData( model, '<paragraph>שמש 太陽 ดวงอาทิตย์ شمس ਸੂਰਜ słońce</paragraph>' );
  82. wordCountPlugin._calculateWordsAndCharacters();
  83. expect( wordCountPlugin.words ).to.equal( 6 );
  84. } );
  85. describe( 'update event', () => {
  86. it( 'fires update event with actual amount of characters and words', () => {
  87. const fake = sinon.fake();
  88. wordCountPlugin.on( 'update', fake );
  89. wordCountPlugin._calculateWordsAndCharacters();
  90. sinon.assert.calledOnce( fake );
  91. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 0, characters: 0 } );
  92. // _calculateWordsAndCharacters is throttled, so for this test case is run manually
  93. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  94. wordCountPlugin._calculateWordsAndCharacters();
  95. sinon.assert.calledTwice( fake );
  96. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 2, characters: 12 } );
  97. } );
  98. } );
  99. } );
  100. describe( 'self-updating element', () => {
  101. let container;
  102. beforeEach( () => {
  103. container = wordCountPlugin.wordCountContainer;
  104. } );
  105. it( 'provides html element', () => {
  106. expect( container ).to.be.instanceof( HTMLElement );
  107. } );
  108. it( 'provided element has proper structure', () => {
  109. expect( container.tagName ).to.equal( 'DIV' );
  110. expect( container.classList.contains( 'ck' ) ).to.be.true;
  111. expect( container.classList.contains( 'ck-word-count' ) ).to.be.true;
  112. const children = Array.from( container.children );
  113. expect( children.length ).to.equal( 2 );
  114. expect( children[ 0 ].tagName ).to.equal( 'DIV' );
  115. expect( children[ 0 ].innerHTML ).to.equal( 'Words: 0' );
  116. expect( children[ 1 ].tagName ).to.equal( 'DIV' );
  117. expect( children[ 1 ].innerHTML ).to.equal( 'Characters: 0' );
  118. } );
  119. it( 'updates container content', () => {
  120. expect( container.innerText ).to.equal( 'Words: 0Characters: 0' );
  121. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  122. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  123. wordCountPlugin._calculateWordsAndCharacters();
  124. // There is \n between paragraph which has to be included into calculations
  125. expect( container.innerText ).to.equal( 'Words: 5Characters: 23' );
  126. } );
  127. it( 'subsequent calls provides the same element', () => {
  128. const newContainer = wordCountPlugin.wordCountContainer;
  129. expect( container ).to.equal( newContainer );
  130. } );
  131. describe( 'destroy()', () => {
  132. it( 'html element is removed', done => {
  133. const frag = document.createDocumentFragment();
  134. frag.appendChild( container );
  135. expect( frag.querySelector( '*' ) ).to.be.instanceof( HTMLElement );
  136. editor.destroy()
  137. .then( () => {
  138. expect( frag.querySelector( '*' ) ).to.be.null;
  139. } )
  140. .then( done )
  141. .catch( done );
  142. } );
  143. it( 'method is called', done => {
  144. const spy = sinon.spy( wordCountPlugin, 'destroy' );
  145. editor.destroy()
  146. .then( () => {
  147. sinon.assert.calledOnce( spy );
  148. } )
  149. .then( done )
  150. .catch( done );
  151. } );
  152. it( 'should not throw an error if container is not specified', () => {
  153. return VirtualTestEditor.create( {
  154. plugins: [ WordCount, Paragraph ]
  155. } )
  156. .then( editor => {
  157. return editor.destroy();
  158. } );
  159. } );
  160. } );
  161. } );
  162. describe( '_calculateWordsAndCharacters and throttle', () => {
  163. beforeEach( done => {
  164. // We need to flush initial throttle value after editor's initialization
  165. setTimeout( done, DELAY );
  166. } );
  167. it( 'gets update after model data change', done => {
  168. const fake = sinon.fake();
  169. wordCountPlugin.on( 'update', fake );
  170. // Initial change in model should be immediately reflected in word-count
  171. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  172. sinon.assert.calledOnce( fake );
  173. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  174. // Subsequent updates should be throttle and run with last parameters
  175. setTimeout( () => {
  176. sinon.assert.calledTwice( fake );
  177. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  178. done();
  179. }, DELAY );
  180. setModelData( model, '<paragraph>Hello world</paragraph>' );
  181. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  182. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  183. } );
  184. it( 'is not update after selection change', done => {
  185. setModelData( model, '<paragraph>Hello[] world.</paragraph>' );
  186. const fake = sinon.fake();
  187. const fakeSelectionChange = sinon.fake();
  188. wordCountPlugin.on( 'update', fake );
  189. model.document.on( 'change', fakeSelectionChange );
  190. model.change( writer => {
  191. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 1 ] ) );
  192. writer.setSelection( range );
  193. } );
  194. model.change( writer => {
  195. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 10 ] ) );
  196. writer.setSelection( range );
  197. } );
  198. setTimeout( () => {
  199. sinon.assert.notCalled( fake );
  200. sinon.assert.called( fakeSelectionChange );
  201. done();
  202. }, DELAY );
  203. } );
  204. } );
  205. describe( 'custom config options', () => {
  206. it( 'displayWords = false', done => {
  207. VirtualTestEditor.create( {
  208. plugins: [ WordCount, Paragraph ],
  209. wordCount: {
  210. displayWords: false
  211. }
  212. } )
  213. .then( editor => {
  214. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  215. const container = wordCountPlugin.wordCountContainer;
  216. expect( container.innerText ).to.equal( 'Characters: 0' );
  217. } )
  218. .then( done )
  219. .catch( done );
  220. } );
  221. it( 'displayCharacters = false', done => {
  222. VirtualTestEditor.create( {
  223. plugins: [ WordCount, Paragraph ],
  224. wordCount: {
  225. displayCharacters: false
  226. }
  227. } )
  228. .then( editor => {
  229. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  230. const container = wordCountPlugin.wordCountContainer;
  231. expect( container.innerText ).to.equal( 'Words: 0' );
  232. } )
  233. .then( done )
  234. .catch( done );
  235. } );
  236. it( 'should call function registered under config.wordCount.onUpdate', () => {
  237. const fake = sinon.fake();
  238. return VirtualTestEditor.create( {
  239. plugins: [ WordCount, Paragraph ],
  240. wordCount: {
  241. onUpdate: fake
  242. }
  243. } )
  244. .then( editor => {
  245. sinon.assert.calledWithExactly( fake, { words: 0, characters: 0 } );
  246. setModelData( editor.model, '<paragraph>Foo Bar</paragraph>' );
  247. } )
  248. .then( () => new Promise( resolve => {
  249. setTimeout( resolve, DELAY );
  250. } ) )
  251. .then( () => {
  252. sinon.assert.calledWithExactly( fake, { words: 2, characters: 7 } );
  253. } );
  254. } );
  255. it( 'should append word count container in element referenced in config.wordCount.container', () => {
  256. const element = document.createElement( 'div' );
  257. expect( element.children.length ).to.equal( 0 );
  258. return VirtualTestEditor.create( {
  259. plugins: [ WordCount, Paragraph ],
  260. wordCount: {
  261. container: element
  262. }
  263. } )
  264. .then( editor => {
  265. expect( element.children.length ).to.equal( 1 );
  266. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  267. expect( element.firstElementChild ).to.equal( wordCountPlugin.wordCountContainer );
  268. } );
  269. } );
  270. } );
  271. describe( 'translations', () => {
  272. before( () => {
  273. addTranslations( 'pl', {
  274. 'Words: %0': 'Słowa: %0',
  275. 'Characters: %0': 'Znaki: %0'
  276. } );
  277. addTranslations( 'en', {
  278. 'Words: %0': 'Words: %0',
  279. 'Characters: %0': 'Characters: %0'
  280. } );
  281. } );
  282. after( () => {
  283. clearTranslations();
  284. } );
  285. it( 'applies proper language translations', done => {
  286. VirtualTestEditor.create( {
  287. plugins: [ WordCount, Paragraph ],
  288. language: 'pl'
  289. } )
  290. .then( editor => {
  291. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  292. const container = wordCountPlugin.wordCountContainer;
  293. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  294. } )
  295. .then( done )
  296. .catch( done );
  297. } );
  298. } );
  299. } );