mentionediting.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import MentionEditing from '../src/mentionediting';
  10. import MentionCommand from '../src/mentioncommand';
  11. describe( 'MentionEditing', () => {
  12. let editor, model, doc;
  13. testUtils.createSinonSandbox();
  14. afterEach( () => {
  15. if ( editor ) {
  16. return editor.destroy();
  17. }
  18. } );
  19. it( 'should be named', () => {
  20. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  21. } );
  22. it( 'should be loaded', () => {
  23. return createTestEditor()
  24. .then( newEditor => {
  25. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  26. } );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. return createTestEditor()
  30. .then( newEditor => {
  31. model = newEditor.model;
  32. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  33. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  34. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  36. } );
  37. } );
  38. it( 'should register mention command', () => {
  39. return createTestEditor()
  40. .then( newEditor => {
  41. const command = newEditor.commands.get( 'mention' );
  42. expect( command ).to.be.instanceof( MentionCommand );
  43. } );
  44. } );
  45. describe( 'conversion in the data pipeline', () => {
  46. beforeEach( () => {
  47. return createTestEditor()
  48. .then( newEditor => {
  49. editor = newEditor;
  50. model = editor.model;
  51. doc = model.document;
  52. } );
  53. } );
  54. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  55. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  56. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  57. expect( textNode ).to.not.be.null;
  58. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  59. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  60. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  61. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  62. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  63. } );
  64. it( 'should convert consecutive mentions spans as two text nodes and two spans in the view', () => {
  65. editor.setData(
  66. '<p>' +
  67. '<span class="mention" data-mention="John">@John</span>' +
  68. '<span class="mention" data-mention="John">@John</span>' +
  69. '</p>'
  70. );
  71. // getModelData() merges text blocks with "same" attributes:
  72. // So expected: <$text mention="{"name":"John"}">@John</$text><$text mention="{"name":"John"}">@John</$text>'
  73. // Is returned as: <$text mention="{"name":"John"}">@John@John</$text>'
  74. const paragraph = doc.getRoot().getChild( 0 );
  75. expect( paragraph.childCount ).to.equal( 2 );
  76. assertTextNode( paragraph.getChild( 0 ) );
  77. assertTextNode( paragraph.getChild( 1 ) );
  78. const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' )._id;
  79. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._id;
  80. expect( firstMentionId ).to.not.equal( secondMentionId );
  81. expect( editor.getData() ).to.equal(
  82. '<p>' +
  83. '<span class="mention" data-mention="John">@John</span>' +
  84. '<span class="mention" data-mention="John">@John</span>' +
  85. '</p>'
  86. );
  87. function assertTextNode( textNode ) {
  88. expect( textNode ).to.not.be.null;
  89. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  90. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  91. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  92. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  93. }
  94. } );
  95. it( 'should not convert partial mentions', () => {
  96. editor.setData( '<p><span class="mention" data-mention="John">@Jo</span></p>' );
  97. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>@Jo</paragraph>' );
  98. expect( editor.getData() ).to.equal( '<p>@Jo</p>' );
  99. } );
  100. } );
  101. describe( 'selection post fixer', () => {
  102. beforeEach( () => {
  103. return createTestEditor()
  104. .then( newEditor => {
  105. editor = newEditor;
  106. model = editor.model;
  107. doc = model.document;
  108. } );
  109. } );
  110. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  111. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  112. model.change( writer => {
  113. const paragraph = doc.getRoot().getChild( 0 );
  114. writer.setSelection( paragraph, 9 );
  115. } );
  116. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  117. } );
  118. it( 'should allow to type after a mention', () => {
  119. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  120. model.change( writer => {
  121. const paragraph = doc.getRoot().getChild( 0 );
  122. writer.setSelection( paragraph, 9 );
  123. writer.insertText( ' ', paragraph, 9 );
  124. } );
  125. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  126. } );
  127. } );
  128. describe( 'removing partial mention post fixer', () => {
  129. beforeEach( () => {
  130. return createTestEditor()
  131. .then( newEditor => {
  132. editor = newEditor;
  133. model = editor.model;
  134. doc = model.document;
  135. } );
  136. } );
  137. it( 'should remove mention on adding a text inside mention', () => {
  138. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  139. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  140. expect( textNode ).to.not.be.null;
  141. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  142. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  143. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  144. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  145. model.change( writer => {
  146. const paragraph = doc.getRoot().getChild( 0 );
  147. writer.setSelection( paragraph, 6 );
  148. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  149. } );
  150. expect( getModelData( model, { withoutSelection: true } ) )
  151. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  152. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  153. } );
  154. it( 'should remove mention on removing a text inside mention', () => {
  155. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  156. const paragraph = doc.getRoot().getChild( 0 );
  157. model.change( writer => {
  158. writer.setSelection( paragraph, 6 );
  159. } );
  160. model.enqueueChange( () => {
  161. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  162. model.deleteContent( doc.selection );
  163. } );
  164. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  165. } );
  166. it( 'should remove mention on removing a text at the and of a mention', () => {
  167. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  168. const paragraph = doc.getRoot().getChild( 0 );
  169. // Set selection at the end of a John.
  170. model.change( writer => {
  171. writer.setSelection( paragraph, 9 );
  172. } );
  173. model.enqueueChange( () => {
  174. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  175. model.deleteContent( doc.selection );
  176. } );
  177. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  178. } );
  179. it( 'should not remove mention on removing a text just after a mention', () => {
  180. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  181. const paragraph = doc.getRoot().getChild( 0 );
  182. // Set selection before bar.
  183. model.change( writer => {
  184. writer.setSelection( paragraph, 10 );
  185. } );
  186. model.enqueueChange( () => {
  187. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  188. model.deleteContent( doc.selection );
  189. } );
  190. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  191. } );
  192. } );
  193. function createTestEditor( mentionConfig ) {
  194. return VirtualTestEditor
  195. .create( {
  196. plugins: [ Paragraph, MentionEditing ],
  197. mention: mentionConfig
  198. } );
  199. }
  200. } );