wordcount.js 12 KB

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