wordcount.js 12 KB

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