8
0

spellchecking.js 5.7 KB

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