spellchecking.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  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 { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. describe( 'Typing – spellchecking integration', () => {
  15. let editor, container;
  16. beforeEach( () => {
  17. container = document.createElement( 'div' );
  18. document.body.appendChild( container );
  19. return ClassicEditor
  20. .create( container, {
  21. plugins: [ Enter, Typing, Paragraph, Bold, Undo ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. editor.setData(
  26. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  27. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  28. } );
  29. } );
  30. afterEach( () => {
  31. container.remove();
  32. return editor.destroy();
  33. } );
  34. describe( 'Plain text spellchecking (mutations)', () => {
  35. // This tests emulates spellchecker correction on non-styled text by firing proper mutations.
  36. it( 'should replace with longer word (collapsed)', () => {
  37. emulateSpellcheckerMutation( editor, 0, 13, 13,
  38. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  39. 'The Foo house a is a Foo hous e. A Foo athat and Foo xhat. This is an istane' );
  40. expectContent( editor,
  41. '<paragraph>The Foo house[] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  42. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  43. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  44. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  45. } );
  46. it( 'should replace with longer word (non-collapsed)', () => {
  47. emulateSpellcheckerMutation( editor, 0, 8, 13,
  48. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  49. 'The Foo house a is a Foo hous e. A Foo athat and Foo xhat. This is an istane' );
  50. expectContent( editor,
  51. '<paragraph>The Foo [house] a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  52. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  53. '<p>The Foo {house} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  54. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  55. } );
  56. it( 'should replace with shorter word (merging letter after - collapsed)', () => {
  57. emulateSpellcheckerMutation( editor, 0, 29, 29,
  58. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  59. 'The Foo hous a is a Foo house. A Foo athat and Foo xhat. This is an istane' );
  60. expectContent( editor,
  61. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  62. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  63. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  64. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  65. } );
  66. it( 'should replace with shorter word (merging letter after - non-collapsed)', () => {
  67. emulateSpellcheckerMutation( editor, 0, 24, 29,
  68. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  69. 'The Foo hous a is a Foo house. A Foo athat and Foo xhat. This is an istane' );
  70. expectContent( editor,
  71. '<paragraph>The Foo hous a is a Foo [house]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  72. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  73. '<p>The Foo hous a is a Foo {house}. A Foo athat and Foo xhat. This is an istane</p>' +
  74. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  75. } );
  76. it( 'should replace with same length text (collapsed)', () => {
  77. emulateSpellcheckerMutation( editor, 0, 43, 43,
  78. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  79. 'The Foo hous a is a Foo hous e. A Food that and Foo xhat. This is an istane' );
  80. expectContent( editor,
  81. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  82. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  83. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  84. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  85. } );
  86. it( 'should replace with same length text (non-collapsed)', () => {
  87. emulateSpellcheckerMutation( editor, 0, 37, 43,
  88. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  89. 'The Foo hous a is a Foo hous e. A Food that and Foo xhat. This is an istane' );
  90. expectContent( editor,
  91. '<paragraph>The Foo hous a is a Foo hous e. A Foo[d that] and Foo xhat. This is an istane</paragraph>' +
  92. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  93. '<p>The Foo hous a is a Foo hous e. A Foo{d that} and Foo xhat. This is an istane</p>' +
  94. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  95. } );
  96. it( 'should replace with longer word on the paragraph end (non-collapsed)', () => {
  97. emulateSpellcheckerMutation( editor, 0, 77, 77,
  98. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  99. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance' );
  100. expectContent( editor,
  101. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  102. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  103. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  104. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  105. } );
  106. it( 'should replace with longer word on the paragraph end (collapsed)', () => {
  107. emulateSpellcheckerMutation( editor, 0, 69, 77,
  108. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  109. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance' );
  110. expectContent( editor,
  111. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an [instance]</paragraph>' +
  112. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>',
  113. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an {instance}</p>' +
  114. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  115. } );
  116. it( 'should replace with shorter word on the paragraph end (non-collapsed)', () => {
  117. emulateSpellcheckerMutation( editor, 1, 43, 43,
  118. 'Banana, orenge, appfle and the new comppputer',
  119. 'Banana, orenge, appfle and the new computer' );
  120. expectContent( editor,
  121. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  122. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>',
  123. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  124. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  125. } );
  126. it( 'should replace with shorter word on the paragraph end (collapsed)', () => {
  127. emulateSpellcheckerMutation( editor, 1, 35, 43,
  128. 'Banana, orenge, appfle and the new comppputer',
  129. 'Banana, orenge, appfle and the new computer' );
  130. expectContent( editor,
  131. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  132. '<paragraph>Banana, orenge, appfle and the new [computer]</paragraph>',
  133. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  134. '<p>Banana, orenge, appfle and the new {computer}</p>' );
  135. } );
  136. } );
  137. } );
  138. function emulateSpellcheckerMutation( editor, nodeIndex, rangeStart, rangeEnd, oldText, newText ) {
  139. const view = editor.editing.view;
  140. const viewRoot = view.document.getRoot();
  141. const viewSelection = view.createSelection();
  142. const node = viewRoot.getChild( nodeIndex ).getChild( 0 );
  143. viewSelection.setTo( view.createRange(
  144. view.createPositionAt( node, rangeStart ),
  145. view.createPositionAt( node, rangeEnd )
  146. ) );
  147. view.document.fire( 'mutations',
  148. [
  149. {
  150. type: 'text',
  151. oldText,
  152. newText,
  153. node
  154. }
  155. ],
  156. viewSelection
  157. );
  158. }
  159. function expectContent( editor, expectedModel, expectedView ) {
  160. expect( getModelData( editor.model ) ).to.equal( expectedModel );
  161. expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
  162. }