spellchecking.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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;
  18. before( () => {
  19. const 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. beforeEach( () => {
  29. if ( onChangesDone ) {
  30. editor.document.off( 'changesDone', onChangesDone );
  31. onChangesDone = null;
  32. }
  33. editor.setData(
  34. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  35. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  36. } );
  37. describe( 'Plain text spellchecking (mutations)', () => {
  38. // This tests emulates spellchecker correction on non-styled text by firing proper mutations.
  39. it( 'should replace with longer word', () => {
  40. emulateSpellcheckerMutation( editor, 0, 13,
  41. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  42. 'The Foo house a is a Foo hous e. A Foo athat and Foo xhat. This is an istane' );
  43. expectContent( editor,
  44. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  45. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  46. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  47. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  48. } );
  49. it( 'should replace with shorter word (merging letter after)', () => {
  50. emulateSpellcheckerMutation( editor, 0, 29,
  51. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  52. 'The Foo hous a is a Foo house. A Foo athat and Foo xhat. This is an istane' );
  53. expectContent( editor,
  54. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  55. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  56. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  57. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  58. } );
  59. it( 'should replace with same length text', () => {
  60. emulateSpellcheckerMutation( editor, 0, 43,
  61. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  62. 'The Foo hous a is a Foo hous e. A Food that and Foo xhat. This is an istane' );
  63. expectContent( editor,
  64. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  65. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  66. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  67. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  68. } );
  69. it( 'should replace with longer word on the paragraph end', () => {
  70. emulateSpellcheckerMutation( editor, 0, 77,
  71. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  72. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance' );
  73. expectContent( editor,
  74. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  75. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  76. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  77. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  78. } );
  79. it( 'should replace with shorter word on the paragraph end', () => {
  80. emulateSpellcheckerMutation( editor, 1, 43,
  81. 'Banana, orenge, appfle and the new comppputer',
  82. 'Banana, orenge, appfle and the new computer' );
  83. expectContent( editor,
  84. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  85. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>',
  86. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  87. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  88. } );
  89. } );
  90. describe( 'Plain text spellchecking (insertText)', () => {
  91. // This tests emulates spellchecker correction on non-styled text by inserting correction text via native 'insertText' command.
  92. it( 'should replace with longer word (collapsed)', ( done ) => {
  93. onChangesDone = () => {
  94. expectContent( editor,
  95. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  96. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  97. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  98. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  99. done();
  100. };
  101. emulateSpellcheckerInsertText( editor, 0, 12, 12, 'e', onChangesDone );
  102. } );
  103. it( 'should replace with longer word (non-collapsed)', ( done ) => {
  104. onChangesDone = () => {
  105. expectContent( editor,
  106. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  107. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  108. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  109. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  110. done();
  111. };
  112. emulateSpellcheckerInsertText( editor, 0, 8, 12, 'house', onChangesDone );
  113. } );
  114. it( 'should replace with shorter word (merging letter after - collapsed)', ( done ) => {
  115. onChangesDone = () => {
  116. expectContent( editor,
  117. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  118. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  119. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  120. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  121. done();
  122. };
  123. emulateSpellcheckerInsertText( editor, 0, 28, 30, 'e', onChangesDone );
  124. } );
  125. it( 'should replace with shorter word (merging letter after - non-collapsed)', ( done ) => {
  126. onChangesDone = () => {
  127. expectContent( editor,
  128. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  129. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  130. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  131. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  132. done();
  133. };
  134. emulateSpellcheckerInsertText( editor, 0, 24, 30, 'house', onChangesDone );
  135. } );
  136. it( 'should replace with same length text', ( done ) => {
  137. onChangesDone = () => {
  138. expectContent( editor,
  139. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  140. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  141. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  142. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  143. done();
  144. };
  145. emulateSpellcheckerInsertText( editor, 0, 37, 43, 'd that', onChangesDone );
  146. } );
  147. it( 'should replace with longer word on the paragraph end', ( done ) => {
  148. onChangesDone = () => {
  149. expectContent( editor,
  150. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  151. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  152. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  153. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  154. done();
  155. };
  156. emulateSpellcheckerInsertText( editor, 0, 69, 75, 'instance', onChangesDone );
  157. } );
  158. it( 'should replace with shorter word on the paragraph end', ( done ) => {
  159. onChangesDone = () => {
  160. expectContent( editor,
  161. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  162. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>',
  163. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  164. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  165. done();
  166. };
  167. emulateSpellcheckerInsertText( editor, 1, 35, 45, 'computer', onChangesDone );
  168. } );
  169. } );
  170. } );
  171. function emulateSpellcheckerMutation( editor, nodeIndex, resultPositionIndex, oldText, newText ) {
  172. const view = editor.editing.view;
  173. const viewRoot = view.getRoot();
  174. const viewSelection = new ViewSelection();
  175. viewSelection.collapse( viewRoot.getChild( nodeIndex ).getChild( 0 ), resultPositionIndex );
  176. view.fire( 'mutations',
  177. [ {
  178. type: 'text',
  179. oldText: oldText,
  180. newText: newText,
  181. node: viewRoot.getChild( nodeIndex ).getChild( 0 )
  182. } ],
  183. viewSelection
  184. );
  185. }
  186. function emulateSpellcheckerInsertText( editor, nodeIndex, rangeStart, rangeEnd, text, onChangesDoneCallback ) {
  187. const model = editor.editing.model;
  188. const modelRoot = model.getRoot();
  189. editor.editing.view.focus();
  190. model.enqueueChanges( () => {
  191. model.selection.setRanges( [
  192. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( nodeIndex ), rangeStart, modelRoot.getChild( nodeIndex ), rangeEnd )
  193. ] );
  194. } );
  195. editor.document.once( 'changesDone', onChangesDoneCallback, { priority: 'low' } );
  196. window.document.execCommand( 'insertText', false, text );
  197. }
  198. function expectContent( editor, expectedModel, expectedView ) {
  199. expect( getModelData( editor.editing.model ) ).to.equal( expectedModel );
  200. expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
  201. }