8
0

mentionediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 MentionEditing from '../src/mentionediting';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  12. import MentionCommand from '../src/mentioncommand';
  13. describe( 'MentionEditing', () => {
  14. testUtils.createSinonSandbox();
  15. let editor, model, doc;
  16. testUtils.createSinonSandbox();
  17. afterEach( () => {
  18. if ( editor ) {
  19. return editor.destroy();
  20. }
  21. } );
  22. it( 'should be named', () => {
  23. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  24. } );
  25. it( 'should be loaded', () => {
  26. return createTestEditor()
  27. .then( newEditor => {
  28. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  29. } );
  30. } );
  31. it( 'should set proper schema rules', () => {
  32. return createTestEditor()
  33. .then( newEditor => {
  34. model = newEditor.model;
  35. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  37. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  38. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  39. } );
  40. } );
  41. it( 'should register mention command', () => {
  42. return createTestEditor()
  43. .then( newEditor => {
  44. const command = newEditor.commands.get( 'mention' );
  45. expect( command ).to.be.instanceof( MentionCommand );
  46. } );
  47. } );
  48. describe( 'conversion in the data pipeline', () => {
  49. beforeEach( () => {
  50. return createTestEditor()
  51. .then( newEditor => {
  52. editor = newEditor;
  53. model = editor.model;
  54. doc = model.document;
  55. } );
  56. } );
  57. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  58. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  59. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  60. expect( textNode ).to.not.be.null;
  61. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  62. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  63. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  64. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  65. } );
  66. it( 'should convert consecutive mentions spans as two text nodes and two spans in the view', () => {
  67. editor.setData(
  68. '<p>' +
  69. '<span class="mention" data-mention="John">@John</span>' +
  70. '<span class="mention" data-mention="John">@John</span>' +
  71. '</p>'
  72. );
  73. // getModelData() merges text blocks with "same" attributes:
  74. // So expected: <$text mention="{"name":"John"}">@John</$text><$text mention="{"name":"John"}">@John</$text>'
  75. // Is returned as: <$text mention="{"name":"John"}">@John@John</$text>'
  76. const paragraph = doc.getRoot().getChild( 0 );
  77. expect( paragraph.childCount ).to.equal( 2 );
  78. assertTextNode( paragraph.getChild( 0 ) );
  79. assertTextNode( paragraph.getChild( 1 ) );
  80. const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' )._id;
  81. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._id;
  82. expect( firstMentionId ).to.not.equal( secondMentionId );
  83. expect( editor.getData() ).to.equal(
  84. '<p>' +
  85. '<span class="mention" data-mention="John">@John</span>' +
  86. '<span class="mention" data-mention="John">@John</span>' +
  87. '</p>'
  88. );
  89. function assertTextNode( textNode ) {
  90. expect( textNode ).to.not.be.null;
  91. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  92. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  93. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  94. }
  95. } );
  96. it( 'should not convert partial mentions', () => {
  97. editor.setData( '<p><span class="mention" data-mention="John">@Jo</span></p>' );
  98. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>@Jo</paragraph>' );
  99. expect( editor.getData() ).to.equal( '<p>@Jo</p>' );
  100. } );
  101. } );
  102. describe( 'selection post fixer', () => {
  103. beforeEach( () => {
  104. return createTestEditor()
  105. .then( newEditor => {
  106. editor = newEditor;
  107. model = editor.model;
  108. doc = model.document;
  109. } );
  110. } );
  111. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  112. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  113. model.change( writer => {
  114. const paragraph = doc.getRoot().getChild( 0 );
  115. writer.setSelection( paragraph, 9 );
  116. } );
  117. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  118. } );
  119. it( 'should allow to type after a mention', () => {
  120. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  121. model.change( writer => {
  122. const paragraph = doc.getRoot().getChild( 0 );
  123. writer.setSelection( paragraph, 9 );
  124. writer.insertText( ' ', paragraph, 9 );
  125. } );
  126. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  127. } );
  128. } );
  129. describe( 'removing partial mention post fixer', () => {
  130. beforeEach( () => {
  131. return createTestEditor()
  132. .then( newEditor => {
  133. editor = newEditor;
  134. model = editor.model;
  135. doc = model.document;
  136. } );
  137. } );
  138. it( 'should remove mention on adding a text inside mention', () => {
  139. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  140. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  141. expect( textNode ).to.not.be.null;
  142. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  143. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  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. describe( 'integration', () => {
  194. describe( 'undo', () => {
  195. beforeEach( () => {
  196. return VirtualTestEditor
  197. .create( {
  198. plugins: [ Paragraph, MentionEditing, UndoEditing ]
  199. } ).then( newEditor => {
  200. editor = newEditor;
  201. model = editor.model;
  202. doc = model.document;
  203. } );
  204. } );
  205. // Failing test. See ckeditor/ckeditor5#1645.
  206. it( 'should restore removed mention on adding a text inside mention', () => {
  207. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  208. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  209. model.change( writer => {
  210. const paragraph = doc.getRoot().getChild( 0 );
  211. writer.setSelection( paragraph, 6 );
  212. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  213. } );
  214. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  215. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jaohn bar</p>' );
  216. editor.execute( 'undo' );
  217. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  218. expect( getViewData( editor.editing.view ) )
  219. .to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  220. } );
  221. // Failing test. See ckeditor/ckeditor5#1645.
  222. it( 'should restore removed mention on removing a text inside mention', () => {
  223. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  224. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  225. model.change( writer => {
  226. const paragraph = doc.getRoot().getChild( 0 );
  227. writer.setSelection( paragraph, 7 );
  228. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  229. model.deleteContent( doc.selection );
  230. } );
  231. expect( editor.getData() ).to.equal( '<p>foo @Jhn bar</p>' );
  232. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jhn bar</p>' );
  233. editor.execute( 'undo' );
  234. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  235. expect( getViewData( editor.editing.view ) )
  236. .to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  237. } );
  238. } );
  239. } );
  240. function createTestEditor( mentionConfig ) {
  241. return VirtualTestEditor
  242. .create( {
  243. plugins: [ Paragraph, MentionEditing ],
  244. mention: mentionConfig
  245. } );
  246. }
  247. } );