wordcount.js 11 KB

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