8
0

spellchecking.js 10 KB

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