wordcount.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 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. } );
  162. } );
  163. describe( 'self-updating element', () => {
  164. let container;
  165. beforeEach( () => {
  166. container = wordCountPlugin.wordCountContainer;
  167. } );
  168. it( 'provides html element', () => {
  169. expect( container ).to.be.instanceof( HTMLElement );
  170. } );
  171. it( 'provided element has proper structure', () => {
  172. expect( container.tagName ).to.equal( 'DIV' );
  173. expect( container.classList.contains( 'ck' ) ).to.be.true;
  174. expect( container.classList.contains( 'ck-word-count' ) ).to.be.true;
  175. const children = Array.from( container.children );
  176. expect( children.length ).to.equal( 2 );
  177. expect( children[ 0 ].tagName ).to.equal( 'DIV' );
  178. expect( children[ 0 ].innerHTML ).to.equal( 'Words: 0' );
  179. expect( children[ 1 ].tagName ).to.equal( 'DIV' );
  180. expect( children[ 1 ].innerHTML ).to.equal( 'Characters: 0' );
  181. } );
  182. it( 'updates container content', () => {
  183. expect( container.innerText ).to.equal( 'Words: 0Characters: 0' );
  184. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  185. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  186. wordCountPlugin._refreshStats();
  187. expect( container.innerText ).to.equal( 'Words: 5Characters: 23' );
  188. } );
  189. it( 'subsequent calls provides the same element', () => {
  190. const newContainer = wordCountPlugin.wordCountContainer;
  191. expect( container ).to.equal( newContainer );
  192. } );
  193. describe( 'destroy()', () => {
  194. it( 'html element is removed', done => {
  195. const frag = document.createDocumentFragment();
  196. frag.appendChild( container );
  197. expect( frag.querySelector( '*' ) ).to.be.instanceof( HTMLElement );
  198. editor.destroy()
  199. .then( () => {
  200. expect( frag.querySelector( '*' ) ).to.be.null;
  201. } )
  202. .then( done )
  203. .catch( done );
  204. } );
  205. it( 'method is called', done => {
  206. const spy = sinon.spy( wordCountPlugin, 'destroy' );
  207. editor.destroy()
  208. .then( () => {
  209. sinon.assert.calledOnce( spy );
  210. } )
  211. .then( done )
  212. .catch( done );
  213. } );
  214. it( 'should not throw an error if container is not specified', () => {
  215. return VirtualTestEditor.create( {
  216. plugins: [ WordCount, Paragraph ]
  217. } )
  218. .then( editor => {
  219. return editor.destroy();
  220. } );
  221. } );
  222. } );
  223. } );
  224. describe( '_refreshStats and throttle', () => {
  225. beforeEach( done => {
  226. // We need to flush initial throttle value after editor's initialization
  227. setTimeout( done, DELAY );
  228. } );
  229. it( 'gets update after model data change', done => {
  230. const fake = sinon.fake();
  231. wordCountPlugin.on( 'update', fake );
  232. // Initial change in model should be immediately reflected in word-count
  233. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  234. sinon.assert.calledOnce( fake );
  235. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  236. // Subsequent updates should be throttle and run with last parameters
  237. setTimeout( () => {
  238. sinon.assert.calledTwice( fake );
  239. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  240. done();
  241. }, DELAY );
  242. setModelData( model, '<paragraph>Hello world</paragraph>' );
  243. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  244. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  245. } );
  246. it( 'is not update after selection change', done => {
  247. setModelData( model, '<paragraph>Hello[] world.</paragraph>' );
  248. const fake = sinon.fake();
  249. const fakeSelectionChange = sinon.fake();
  250. wordCountPlugin.on( 'update', fake );
  251. model.document.on( 'change', fakeSelectionChange );
  252. model.change( writer => {
  253. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 1 ] ) );
  254. writer.setSelection( range );
  255. } );
  256. model.change( writer => {
  257. const range = writer.createRange( new Position( model.document.getRoot(), [ 0, 10 ] ) );
  258. writer.setSelection( range );
  259. } );
  260. setTimeout( () => {
  261. sinon.assert.notCalled( fake );
  262. sinon.assert.called( fakeSelectionChange );
  263. done();
  264. }, DELAY );
  265. } );
  266. } );
  267. describe( 'custom config options', () => {
  268. it( 'displayWords = false', done => {
  269. VirtualTestEditor.create( {
  270. plugins: [ WordCount, Paragraph ],
  271. wordCount: {
  272. displayWords: false
  273. }
  274. } )
  275. .then( editor => {
  276. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  277. const container = wordCountPlugin.wordCountContainer;
  278. expect( container.innerText ).to.equal( 'Characters: 0' );
  279. } )
  280. .then( done )
  281. .catch( done );
  282. } );
  283. it( 'displayCharacters = false', done => {
  284. VirtualTestEditor.create( {
  285. plugins: [ WordCount, Paragraph ],
  286. wordCount: {
  287. displayCharacters: false
  288. }
  289. } )
  290. .then( editor => {
  291. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  292. const container = wordCountPlugin.wordCountContainer;
  293. expect( container.innerText ).to.equal( 'Words: 0' );
  294. } )
  295. .then( done )
  296. .catch( done );
  297. } );
  298. it( 'should call function registered under config.wordCount.onUpdate', () => {
  299. const fake = sinon.fake();
  300. return VirtualTestEditor.create( {
  301. plugins: [ WordCount, Paragraph ],
  302. wordCount: {
  303. onUpdate: fake
  304. }
  305. } )
  306. .then( editor => {
  307. sinon.assert.calledWithExactly( fake, { words: 0, characters: 0 } );
  308. setModelData( editor.model, '<paragraph>Foo Bar</paragraph>' );
  309. } )
  310. .then( () => new Promise( resolve => {
  311. setTimeout( resolve, DELAY );
  312. } ) )
  313. .then( () => {
  314. sinon.assert.calledWithExactly( fake, { words: 2, characters: 7 } );
  315. } );
  316. } );
  317. it( 'should append word count container in element referenced in config.wordCount.container', () => {
  318. const element = document.createElement( 'div' );
  319. expect( element.children.length ).to.equal( 0 );
  320. return VirtualTestEditor.create( {
  321. plugins: [ WordCount, Paragraph ],
  322. wordCount: {
  323. container: element
  324. }
  325. } )
  326. .then( editor => {
  327. expect( element.children.length ).to.equal( 1 );
  328. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  329. expect( element.firstElementChild ).to.equal( wordCountPlugin.wordCountContainer );
  330. } );
  331. } );
  332. } );
  333. describe( 'translations', () => {
  334. before( () => {
  335. addTranslations( 'pl', {
  336. 'Words: %0': 'Słowa: %0',
  337. 'Characters: %0': 'Znaki: %0'
  338. } );
  339. addTranslations( 'en', {
  340. 'Words: %0': 'Words: %0',
  341. 'Characters: %0': 'Characters: %0'
  342. } );
  343. } );
  344. after( () => {
  345. clearTranslations();
  346. } );
  347. it( 'applies proper language translations', done => {
  348. VirtualTestEditor.create( {
  349. plugins: [ WordCount, Paragraph ],
  350. language: 'pl'
  351. } )
  352. .then( editor => {
  353. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  354. const container = wordCountPlugin.wordCountContainer;
  355. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  356. } )
  357. .then( done )
  358. .catch( done );
  359. } );
  360. } );
  361. } );