wordcount.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 env from '@ckeditor/ckeditor5-utils/src/env';
  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. describe( '#words property', () => {
  34. it( 'is defined', () => {
  35. expect( wordCountPlugin.words ).to.equal( 0 );
  36. } );
  37. it( 'returns the number of words right away', () => {
  38. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  39. expect( wordCountPlugin.words ).to.equal( 2 );
  40. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world</paragraph>' );
  41. expect( wordCountPlugin.words ).to.equal( 2 );
  42. setModelData( model, '<paragraph><$text foo="true">Hello</$text></paragraph>' );
  43. expect( wordCountPlugin.words ).to.equal( 1 );
  44. setModelData( model, '' );
  45. expect( wordCountPlugin.words ).to.equal( 0 );
  46. } );
  47. it( 'is observable', done => {
  48. const spy = sinon.spy();
  49. wordCountPlugin.on( 'change:words', spy );
  50. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  51. setModelData( model, '<paragraph><$text foo="true">Hello</$text></paragraph>' );
  52. setTimeout( () => {
  53. // The #update event fired once because it is throttled.
  54. sinon.assert.calledOnce( spy );
  55. expect( spy.firstCall.args[ 2 ] ).to.equal( 1 );
  56. done();
  57. }, DELAY );
  58. } );
  59. } );
  60. describe( '#characters property', () => {
  61. it( 'is defined', () => {
  62. expect( wordCountPlugin.characters ).to.equal( 0 );
  63. } );
  64. it( 'returns the number of characters right away', () => {
  65. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  66. expect( wordCountPlugin.characters ).to.equal( 12 );
  67. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world</paragraph>' );
  68. expect( wordCountPlugin.characters ).to.equal( 11 );
  69. setModelData( model, '<paragraph><$text foo="true">Hello</$text></paragraph>' );
  70. expect( wordCountPlugin.characters ).to.equal( 5 );
  71. setModelData( model, '' );
  72. expect( wordCountPlugin.characters ).to.equal( 0 );
  73. } );
  74. it( 'is observable', done => {
  75. const spy = sinon.spy();
  76. wordCountPlugin.on( 'change:characters', spy );
  77. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  78. setModelData( model, '<paragraph><$text foo="true">Hello</$text></paragraph>' );
  79. setTimeout( () => {
  80. // The #update event fired once because it is throttled.
  81. sinon.assert.calledOnce( spy );
  82. expect( spy.firstCall.args[ 2 ] ).to.equal( 5 );
  83. done();
  84. }, DELAY );
  85. } );
  86. } );
  87. it( 'has a name', () => {
  88. expect( WordCount.pluginName ).to.equal( 'WordCount' );
  89. } );
  90. } );
  91. describe( 'functionality', () => {
  92. it( 'counts words', () => {
  93. expect( wordCountPlugin.words ).to.equal( 0 );
  94. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  95. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' +
  96. '<paragraph>1234</paragraph>' +
  97. '<paragraph>(@#$%^*())</paragraph>' );
  98. wordCountPlugin._refreshStats();
  99. expect( wordCountPlugin.words ).to.equal( 6 );
  100. } );
  101. it( 'counts characters', () => {
  102. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  103. wordCountPlugin._refreshStats();
  104. expect( wordCountPlugin.characters ).to.equal( 12 );
  105. } );
  106. it( 'should not count enter as a character', () => {
  107. expect( wordCountPlugin.characters ).to.equal( 0 );
  108. setModelData( model, '<paragraph>Fo<softBreak></softBreak>o</paragraph>' +
  109. '<paragraph>Foo</paragraph>' +
  110. '<table>' +
  111. '<tableRow>' +
  112. '<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
  113. '</tableRow>' +
  114. '<tableRow>' +
  115. '<tableCell></tableCell><tableCell><paragraph>foo</paragraph></tableCell><tableCell></tableCell>' +
  116. '</tableRow>' +
  117. '<tableRow>' +
  118. '<tableCell></tableCell><tableCell></tableCell><tableCell></tableCell>' +
  119. '</tableRow>' +
  120. '</table>' );
  121. wordCountPlugin._refreshStats();
  122. expect( wordCountPlugin.characters ).to.equal( 9 );
  123. } );
  124. it( 'should count international words', function() {
  125. if ( !env.features.isRegExpUnicodePropertySupported ) {
  126. this.skip();
  127. }
  128. expect( wordCountPlugin.words ).to.equal( 0 );
  129. setModelData( model, '<paragraph>שמש 太陽 ดวงอาทิตย์ شمس ਸੂਰਜ słońce</paragraph>' );
  130. wordCountPlugin._refreshStats();
  131. expect( wordCountPlugin.words ).to.equal( 6 );
  132. } );
  133. describe( 'ES2018 RegExp Unicode property fallback', () => {
  134. const originalPropertiesSupport = env.features.isRegExpUnicodePropertySupported;
  135. before( () => {
  136. env.features.isRegExpUnicodePropertySupported = false;
  137. } );
  138. after( () => {
  139. env.features.isRegExpUnicodePropertySupported = originalPropertiesSupport;
  140. } );
  141. it( 'should use different regexp when unicode properties are not supported', () => {
  142. expect( wordCountPlugin.words ).to.equal( 0 );
  143. setModelData( model, '<paragraph>hello world.</paragraph>' );
  144. wordCountPlugin._refreshStats();
  145. expect( wordCountPlugin.words ).to.equal( 2 );
  146. } );
  147. } );
  148. describe( '#update event', () => {
  149. it( 'fires with the actual number of characters and words', () => {
  150. const fake = sinon.fake();
  151. wordCountPlugin.on( 'update', fake );
  152. wordCountPlugin._refreshStats();
  153. sinon.assert.calledOnce( fake );
  154. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 0, characters: 0 } );
  155. // _refreshStats is throttled, so for this test case is run manually
  156. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  157. wordCountPlugin._refreshStats();
  158. sinon.assert.calledTwice( fake );
  159. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 2, characters: 12 } );
  160. } );
  161. it( 'should be fired after editor initialization', () => {
  162. const fake = sinon.fake();
  163. return VirtualTestEditor.create( {
  164. plugins: [ WordCount, Paragraph, ShiftEnter, TableEditing ],
  165. wordCount: {
  166. onUpdate: fake
  167. }
  168. } )
  169. .then( () => {
  170. sinon.assert.calledOnce( fake );
  171. } );
  172. } );
  173. } );
  174. } );
  175. describe( 'self-updating element', () => {
  176. let container;
  177. beforeEach( () => {
  178. container = wordCountPlugin.wordCountContainer;
  179. } );
  180. it( 'provides html element', () => {
  181. expect( container ).to.be.instanceof( HTMLElement );
  182. } );
  183. it( 'provided element has proper structure', () => {
  184. expect( container.tagName ).to.equal( 'DIV' );
  185. expect( container.classList.contains( 'ck' ) ).to.be.true;
  186. expect( container.classList.contains( 'ck-word-count' ) ).to.be.true;
  187. const children = Array.from( container.children );
  188. expect( children.length ).to.equal( 2 );
  189. expect( children[ 0 ].tagName ).to.equal( 'DIV' );
  190. expect( children[ 0 ].innerHTML ).to.equal( 'Words: 0' );
  191. expect( children[ 1 ].tagName ).to.equal( 'DIV' );
  192. expect( children[ 1 ].innerHTML ).to.equal( 'Characters: 0' );
  193. } );
  194. it( 'updates container content', () => {
  195. expect( container.innerText ).to.equal( 'Words: 0Characters: 0' );
  196. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  197. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  198. wordCountPlugin._refreshStats();
  199. expect( container.innerText ).to.equal( 'Words: 5Characters: 23' );
  200. } );
  201. it( 'subsequent calls provides the same element', () => {
  202. const newContainer = wordCountPlugin.wordCountContainer;
  203. expect( container ).to.equal( newContainer );
  204. } );
  205. describe( 'destroy()', () => {
  206. it( 'html element is removed', done => {
  207. const frag = document.createDocumentFragment();
  208. frag.appendChild( container );
  209. expect( frag.querySelector( '*' ) ).to.be.instanceof( HTMLElement );
  210. editor.destroy()
  211. .then( () => {
  212. expect( frag.querySelector( '*' ) ).to.be.null;
  213. } )
  214. .then( done )
  215. .catch( done );
  216. } );
  217. it( 'method is called', done => {
  218. const spy = sinon.spy( wordCountPlugin, 'destroy' );
  219. editor.destroy()
  220. .then( () => {
  221. sinon.assert.calledOnce( spy );
  222. } )
  223. .then( done )
  224. .catch( done );
  225. } );
  226. it( 'should not throw an error if container is not specified', () => {
  227. return VirtualTestEditor.create( {
  228. plugins: [ WordCount, Paragraph ]
  229. } )
  230. .then( editor => {
  231. return editor.destroy();
  232. } );
  233. } );
  234. } );
  235. } );
  236. describe( '_refreshStats and throttle', () => {
  237. beforeEach( done => {
  238. // We need to flush initial throttle value after editor's initialization
  239. setTimeout( done, DELAY );
  240. } );
  241. it( 'gets update after model data change', done => {
  242. const fake = sinon.fake();
  243. wordCountPlugin.on( 'update', fake );
  244. // Initial change in model should be immediately reflected in word-count
  245. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  246. sinon.assert.calledOnce( fake );
  247. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  248. // Subsequent updates should be throttle and run with last parameters
  249. setTimeout( () => {
  250. sinon.assert.calledTwice( fake );
  251. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  252. done();
  253. }, DELAY );
  254. setModelData( model, '<paragraph>Hello world</paragraph>' );
  255. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  256. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  257. } );
  258. it( 'is not update after selection change', done => {
  259. setModelData( model, '<paragraph>Hello[] world.</paragraph>' );
  260. const fake = sinon.fake();
  261. const fakeSelectionChange = sinon.fake();
  262. wordCountPlugin.on( 'update', fake );
  263. model.document.on( 'change', fakeSelectionChange );
  264. model.change( writer => {
  265. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 1 ] ) );
  266. writer.setSelection( range );
  267. } );
  268. model.change( writer => {
  269. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 10 ] ) );
  270. writer.setSelection( range );
  271. } );
  272. setTimeout( () => {
  273. sinon.assert.notCalled( fake );
  274. sinon.assert.called( fakeSelectionChange );
  275. done();
  276. }, DELAY );
  277. } );
  278. } );
  279. describe( 'custom config options', () => {
  280. it( 'displayWords = false', () => {
  281. return VirtualTestEditor.create( {
  282. plugins: [ WordCount, Paragraph ],
  283. wordCount: {
  284. displayWords: false
  285. }
  286. } )
  287. .then( editor => {
  288. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  289. const container = wordCountPlugin.wordCountContainer;
  290. expect( container.innerText ).to.equal( 'Characters: 0' );
  291. } );
  292. } );
  293. it( 'displayCharacters = false', () => {
  294. return VirtualTestEditor.create( {
  295. plugins: [ WordCount, Paragraph ],
  296. wordCount: {
  297. displayCharacters: false
  298. }
  299. } )
  300. .then( editor => {
  301. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  302. const container = wordCountPlugin.wordCountContainer;
  303. expect( container.innerText ).to.equal( 'Words: 0' );
  304. } );
  305. } );
  306. it( 'should call function registered under config.wordCount.onUpdate', () => {
  307. const fake = sinon.fake();
  308. return VirtualTestEditor.create( {
  309. plugins: [ WordCount, Paragraph ],
  310. wordCount: {
  311. onUpdate: fake
  312. }
  313. } )
  314. .then( editor => {
  315. sinon.assert.calledWithExactly( fake, { words: 0, characters: 0 } );
  316. setModelData( editor.model, '<paragraph>Foo Bar</paragraph>' );
  317. } )
  318. .then( () => new Promise( resolve => {
  319. setTimeout( resolve, DELAY );
  320. } ) )
  321. .then( () => {
  322. sinon.assert.calledWithExactly( fake, { words: 2, characters: 7 } );
  323. } );
  324. } );
  325. it( 'should append word count container in element referenced in config.wordCount.container', () => {
  326. const element = document.createElement( 'div' );
  327. expect( element.children.length ).to.equal( 0 );
  328. return VirtualTestEditor.create( {
  329. plugins: [ WordCount, Paragraph ],
  330. wordCount: {
  331. container: element
  332. }
  333. } )
  334. .then( editor => {
  335. expect( element.children.length ).to.equal( 1 );
  336. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  337. expect( element.firstElementChild ).to.equal( wordCountPlugin.wordCountContainer );
  338. } );
  339. } );
  340. } );
  341. describe( 'translations', () => {
  342. before( () => {
  343. addTranslations( 'pl', {
  344. 'Words: %0': 'Słowa: %0',
  345. 'Characters: %0': 'Znaki: %0'
  346. } );
  347. addTranslations( 'en', {
  348. 'Words: %0': 'Words: %0',
  349. 'Characters: %0': 'Characters: %0'
  350. } );
  351. } );
  352. after( () => {
  353. clearTranslations();
  354. } );
  355. it( 'applies proper language translations', () => {
  356. return VirtualTestEditor.create( {
  357. plugins: [ WordCount, Paragraph ],
  358. language: 'pl'
  359. } )
  360. .then( editor => {
  361. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  362. const container = wordCountPlugin.wordCountContainer;
  363. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  364. } );
  365. } );
  366. } );
  367. } );