spellchecking.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
  7. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  8. import Typing from '../src/typing';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  12. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  13. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  14. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  16. describe( 'Spellchecking integration', () => {
  17. let editor, onChangesDone, container;
  18. before( () => {
  19. container = document.createElement( 'div' );
  20. document.body.appendChild( container );
  21. return ClassicEditor.create( container, {
  22. plugins: [ Enter, Typing, Paragraph, Bold, Undo ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. } );
  27. } );
  28. after( () => {
  29. container.remove();
  30. return editor.destroy();
  31. } );
  32. beforeEach( () => {
  33. if ( onChangesDone ) {
  34. editor.document.off( 'changesDone', onChangesDone );
  35. onChangesDone = null;
  36. }
  37. editor.setData(
  38. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  39. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  40. } );
  41. describe( 'Plain text spellchecking (mutations)', () => {
  42. // This tests emulates spellchecker correction on non-styled text by firing proper mutations.
  43. it( 'should replace with longer word', () => {
  44. emulateSpellcheckerMutation( editor, 0, 13,
  45. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  46. 'The Foo house a is a Foo hous e. A Foo athat and Foo xhat. This is an istane' );
  47. expectContent( editor,
  48. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  49. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  50. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  51. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  52. } );
  53. it( 'should replace with shorter word (merging letter after)', () => {
  54. emulateSpellcheckerMutation( editor, 0, 29,
  55. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  56. 'The Foo hous a is a Foo house. A Foo athat and Foo xhat. This is an istane' );
  57. expectContent( editor,
  58. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  59. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  60. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  61. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  62. } );
  63. it( 'should replace with same length text', () => {
  64. emulateSpellcheckerMutation( editor, 0, 43,
  65. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  66. 'The Foo hous a is a Foo hous e. A Food that and Foo xhat. This is an istane' );
  67. expectContent( editor,
  68. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  69. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  70. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  71. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  72. } );
  73. it( 'should replace with longer word on the paragraph end', () => {
  74. emulateSpellcheckerMutation( editor, 0, 77,
  75. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  76. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance' );
  77. expectContent( editor,
  78. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  79. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  80. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  81. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  82. } );
  83. it( 'should replace with shorter word on the paragraph end', () => {
  84. emulateSpellcheckerMutation( editor, 1, 43,
  85. 'Banana, orenge, appfle and the new comppputer',
  86. 'Banana, orenge, appfle and the new computer' );
  87. expectContent( editor,
  88. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  89. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>',
  90. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  91. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  92. } );
  93. } );
  94. describe( 'Plain text spellchecking (insertText)', () => {
  95. // This tests emulates spellchecker correction on non-styled text by inserting correction text via native 'insertText' command.
  96. it( 'should replace with longer word (collapsed)', done => {
  97. onChangesDone = () => {
  98. expectContent( editor,
  99. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  100. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  101. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  102. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  103. done();
  104. };
  105. emulateSpellcheckerInsertText( editor, 0, 12, 12, 'e', onChangesDone );
  106. } );
  107. it( 'should replace with longer word (non-collapsed)', done => {
  108. onChangesDone = () => {
  109. expectContent( editor,
  110. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  111. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  112. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  113. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  114. done();
  115. };
  116. emulateSpellcheckerInsertText( editor, 0, 8, 12, 'house', onChangesDone );
  117. } );
  118. it( 'should replace with shorter word (merging letter after - collapsed)', done => {
  119. onChangesDone = () => {
  120. expectContent( editor,
  121. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  122. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  123. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  124. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  125. done();
  126. };
  127. emulateSpellcheckerInsertText( editor, 0, 28, 30, 'e', onChangesDone );
  128. } );
  129. it( 'should replace with shorter word (merging letter after - non-collapsed)', done => {
  130. onChangesDone = () => {
  131. expectContent( editor,
  132. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  133. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  134. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  135. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  136. done();
  137. };
  138. emulateSpellcheckerInsertText( editor, 0, 24, 30, 'house', onChangesDone );
  139. } );
  140. it( 'should replace with same length text', done => {
  141. onChangesDone = () => {
  142. expectContent( editor,
  143. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  144. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  145. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  146. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  147. done();
  148. };
  149. emulateSpellcheckerInsertText( editor, 0, 37, 43, 'd that', onChangesDone );
  150. } );
  151. it( 'should replace with longer word on the paragraph end', done => {
  152. onChangesDone = () => {
  153. expectContent( editor,
  154. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  155. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  156. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  157. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  158. done();
  159. };
  160. emulateSpellcheckerInsertText( editor, 0, 69, 75, 'instance', onChangesDone );
  161. } );
  162. it( 'should replace with shorter word on the paragraph end', done => {
  163. onChangesDone = () => {
  164. expectContent( editor,
  165. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  166. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>',
  167. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  168. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  169. done();
  170. };
  171. emulateSpellcheckerInsertText( editor, 1, 35, 45, 'computer', onChangesDone );
  172. } );
  173. } );
  174. } );
  175. function emulateSpellcheckerMutation( editor, nodeIndex, resultPositionIndex, oldText, newText ) {
  176. const view = editor.editing.view;
  177. const viewRoot = view.getRoot();
  178. const viewSelection = new ViewSelection();
  179. viewSelection.collapse( viewRoot.getChild( nodeIndex ).getChild( 0 ), resultPositionIndex );
  180. view.fire( 'mutations',
  181. [ {
  182. type: 'text',
  183. oldText,
  184. newText,
  185. node: viewRoot.getChild( nodeIndex ).getChild( 0 )
  186. } ],
  187. viewSelection
  188. );
  189. }
  190. function emulateSpellcheckerInsertText( editor, nodeIndex, rangeStart, rangeEnd, text, onChangesDoneCallback ) {
  191. const model = editor.editing.model;
  192. const modelRoot = model.getRoot();
  193. editor.editing.view.focus();
  194. model.enqueueChanges( () => {
  195. model.selection.setRanges( [
  196. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( nodeIndex ), rangeStart, modelRoot.getChild( nodeIndex ), rangeEnd )
  197. ] );
  198. } );
  199. editor.document.once( 'changesDone', onChangesDoneCallback, { priority: 'low' } );
  200. window.document.execCommand( 'insertText', false, text );
  201. }
  202. function expectContent( editor, expectedModel, expectedView ) {
  203. expect( getModelData( editor.editing.model ) ).to.equal( expectedModel );
  204. expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
  205. }