mention-integration.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import { parse as parseView, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import MentionEditing from '../src/mentionediting';
  14. describe( 'Mention feature - integration', () => {
  15. let div, editor, model, doc;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. div = document.createElement( 'div' );
  19. document.body.appendChild( div );
  20. } );
  21. afterEach( () => {
  22. div.remove();
  23. return editor.destroy();
  24. } );
  25. describe( 'with undo', () => {
  26. beforeEach( () => {
  27. return ClassicTestEditor.create( div, { plugins: [ Paragraph, MentionEditing, UndoEditing ] } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = editor.model;
  31. doc = model.document;
  32. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  33. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  34. } );
  35. } );
  36. // Failing test. See ckeditor/ckeditor5#1645.
  37. it( 'should restore removed mention on adding a text inside mention', () => {
  38. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  39. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  40. model.change( writer => {
  41. const paragraph = doc.getRoot().getChild( 0 );
  42. writer.setSelection( paragraph, 6 );
  43. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  44. } );
  45. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  46. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jaohn bar</p>' );
  47. editor.execute( 'undo' );
  48. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  49. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  50. .to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  51. } );
  52. // Failing test. See ckeditor/ckeditor5#1645.
  53. it( 'should restore removed mention on removing a text inside mention', () => {
  54. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  55. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  56. model.change( writer => {
  57. const paragraph = doc.getRoot().getChild( 0 );
  58. writer.setSelection( paragraph, 7 );
  59. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  60. model.deleteContent( doc.selection );
  61. } );
  62. expect( editor.getData() ).to.equal( '<p>foo @Jhn bar</p>' );
  63. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jhn bar</p>' );
  64. editor.execute( 'undo' );
  65. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  66. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  67. .to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  68. } );
  69. it( 'should work with attribute post-fixer (beginning formatted)', () => {
  70. testAttributePostFixer(
  71. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  72. '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>',
  73. () => {
  74. model.change( writer => {
  75. const paragraph = doc.getRoot().getChild( 0 );
  76. const start = writer.createPositionAt( paragraph, 0 );
  77. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  78. writer.setSelection( range );
  79. writer.setAttribute( 'bold', true, range );
  80. } );
  81. } );
  82. } );
  83. it( 'should work with attribute post-fixer (end formatted)', () => {
  84. testAttributePostFixer(
  85. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  86. '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>',
  87. () => {
  88. model.change( writer => {
  89. const paragraph = doc.getRoot().getChild( 0 );
  90. const start = writer.createPositionAt( paragraph, 6 );
  91. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  92. writer.setSelection( range );
  93. writer.setAttribute( 'bold', true, range );
  94. } );
  95. } );
  96. } );
  97. it( 'should work with attribute post-fixer (middle formatted)', () => {
  98. testAttributePostFixer(
  99. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  100. '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>',
  101. () => {
  102. model.change( writer => {
  103. const paragraph = doc.getRoot().getChild( 0 );
  104. const start = writer.createPositionAt( paragraph, 6 );
  105. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  106. writer.setSelection( range );
  107. writer.setAttribute( 'bold', true, range );
  108. } );
  109. } );
  110. } );
  111. function testAttributePostFixer( initialData, expectedData, testCallback ) {
  112. editor.setData( initialData );
  113. expect( editor.getData() ).to.equal( initialData );
  114. testCallback();
  115. expect( editor.getData() )
  116. .to.equal( expectedData );
  117. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  118. .to.equal( expectedData );
  119. editor.execute( 'undo' );
  120. expect( editor.getData() ).to.equal( initialData );
  121. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  122. .to.equal( initialData );
  123. editor.execute( 'redo' );
  124. expect( editor.getData() )
  125. .to.equal( expectedData );
  126. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  127. .to.equal( expectedData );
  128. }
  129. } );
  130. describe( 'with clipboard', () => {
  131. let clipboard;
  132. beforeEach( () => {
  133. return ClassicTestEditor
  134. .create( div, { plugins: [ Clipboard, Paragraph, BlockQuote, MentionEditing, UndoEditing ] } )
  135. .then( newEditor => {
  136. editor = newEditor;
  137. model = editor.model;
  138. doc = model.document;
  139. clipboard = editor.plugins.get( 'Clipboard' );
  140. } );
  141. } );
  142. it( 'should not fix broken mention inside pasted content', () => {
  143. editor.setData( '<p>foobar</p>' );
  144. model.change( writer => {
  145. writer.setSelection( doc.getRoot().getChild( 0 ), 3 );
  146. } );
  147. clipboard.fire( 'inputTransformation', {
  148. content: parseView( '<blockquote><p>xxx<span class="mention" data-mention="@John">@Joh</span></p></blockquote>' )
  149. } );
  150. const expectedData = '<p>foo</p>' +
  151. '<blockquote><p>xxx<span class="mention" data-mention="@John">@Joh</span></p></blockquote>' +
  152. '<p>bar</p>';
  153. expect( editor.getData() )
  154. .to.equal( expectedData );
  155. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  156. .to.equal( expectedData );
  157. } );
  158. } );
  159. } );