spellchecking.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Input from '../src/input';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. // import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  9. // import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  12. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  13. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  15. describe( 'Spellchecking integration', () => {
  16. let editor, model, modelRoot, view, viewRoot;
  17. before( () => {
  18. return VirtualTestEditor.create( {
  19. plugins: [ Input, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. // Mock image feature.
  23. newEditor.document.schema.registerItem( 'image', '$inline' );
  24. buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
  25. .fromElement( 'image' )
  26. .toElement( 'img' );
  27. buildViewConverter().for( newEditor.data.viewToModel )
  28. .fromElement( 'img' )
  29. .toElement( 'image' );
  30. editor = newEditor;
  31. model = editor.editing.model;
  32. modelRoot = model.getRoot();
  33. view = editor.editing.view;
  34. viewRoot = view.getRoot();
  35. } );
  36. } );
  37. beforeEach( () => {
  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', () => {
  43. // This tests emulates spellchecker correction on non-styled text.
  44. it( 'should replace with longer word', () => {
  45. emulateSpellchecker(
  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. viewRoot, view, 0, 13
  49. );
  50. expect( getModelData( model ) ).to.equal(
  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. expect( getViewData( view ) ).to.equal(
  54. '<p>The Foo house{} a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  55. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  56. } );
  57. it( 'should replace with shorter word (merging letter after)', () => {
  58. emulateSpellchecker(
  59. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  60. 'The Foo hous a is a Foo house. A Foo athat and Foo xhat. This is an istane',
  61. viewRoot, view, 0, 29
  62. );
  63. expect( getModelData( model ) ).to.equal(
  64. '<paragraph>The Foo hous a is a Foo house[]. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  65. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>' );
  66. expect( getViewData( view ) ).to.equal(
  67. '<p>The Foo hous a is a Foo house{}. A Foo athat and Foo xhat. This is an istane</p>' +
  68. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  69. } );
  70. it( 'should replace with same length text', () => {
  71. emulateSpellchecker(
  72. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  73. 'The Foo hous a is a Foo hous e. A Food that and Foo xhat. This is an istane',
  74. viewRoot, view, 0, 43
  75. );
  76. expect( getModelData( model ) ).to.equal(
  77. '<paragraph>The Foo hous a is a Foo hous e. A Food that[] and Foo xhat. This is an istane</paragraph>' +
  78. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>' );
  79. expect( getViewData( view ) ).to.equal(
  80. '<p>The Foo hous a is a Foo hous e. A Food that{} and Foo xhat. This is an istane</p>' +
  81. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  82. } );
  83. it( 'should replace with longer word on the paragraph end', () => {
  84. emulateSpellchecker(
  85. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane',
  86. 'The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance',
  87. viewRoot, view, 0, 77
  88. );
  89. expect( getModelData( model ) ).to.equal(
  90. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance[]</paragraph>' +
  91. '<paragraph>Banana, orenge, appfle and the new comppputer</paragraph>' );
  92. expect( getViewData( view ) ).to.equal(
  93. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an instance{}</p>' +
  94. '<p>Banana, orenge, appfle and the new comppputer</p>' );
  95. } );
  96. it( 'should replace with shorter word on the paragraph end', () => {
  97. emulateSpellchecker(
  98. 'Banana, orenge, appfle and the new comppputer',
  99. 'Banana, orenge, appfle and the new computer',
  100. viewRoot, view, 1, 43
  101. );
  102. expect( getModelData( model ) ).to.equal(
  103. '<paragraph>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</paragraph>' +
  104. '<paragraph>Banana, orenge, appfle and the new computer[]</paragraph>' );
  105. expect( getViewData( view ) ).to.equal(
  106. '<p>The Foo hous a is a Foo hous e. A Foo athat and Foo xhat. This is an istane</p>' +
  107. '<p>Banana, orenge, appfle and the new computer{}</p>' );
  108. } );
  109. } );
  110. } );
  111. function emulateSpellchecker( oldText, newText, viewRoot, view, nodeIndex, resultPositionIndex ) {
  112. const viewSelection = new ViewSelection();
  113. viewSelection.collapse( viewRoot.getChild( nodeIndex ).getChild( 0 ), resultPositionIndex );
  114. view.fire( 'mutations',
  115. [ {
  116. type: 'text',
  117. oldText: oldText,
  118. newText: newText,
  119. node: viewRoot.getChild( nodeIndex ).getChild( 0 )
  120. } ],
  121. viewSelection
  122. );
  123. }