8
0

mentionediting.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import MentionEditing from '../src/mentionediting';
  11. import MentionCommand from '../src/mentioncommand';
  12. describe( 'MentionEditing', () => {
  13. let editor, model, doc;
  14. testUtils.createSinonSandbox();
  15. afterEach( () => {
  16. if ( editor ) {
  17. return editor.destroy();
  18. }
  19. } );
  20. it( 'should be named', () => {
  21. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  22. } );
  23. it( 'should be loaded', () => {
  24. return createTestEditor()
  25. .then( newEditor => {
  26. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  27. } );
  28. } );
  29. it( 'should set proper schema rules', () => {
  30. return createTestEditor()
  31. .then( newEditor => {
  32. model = newEditor.model;
  33. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  34. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  37. } );
  38. } );
  39. it( 'should register mention command', () => {
  40. return createTestEditor()
  41. .then( newEditor => {
  42. const command = newEditor.commands.get( 'mention' );
  43. expect( command ).to.be.instanceof( MentionCommand );
  44. } );
  45. } );
  46. describe( 'conversion', () => {
  47. beforeEach( () => {
  48. return createTestEditor()
  49. .then( newEditor => {
  50. editor = newEditor;
  51. model = editor.model;
  52. doc = model.document;
  53. } );
  54. } );
  55. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  56. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  57. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  58. expect( textNode ).to.not.be.null;
  59. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  60. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  61. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  62. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  63. const expectedView = '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>';
  64. expect( editor.getData() ).to.equal( expectedView );
  65. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  66. } );
  67. it( 'should convert consecutive mentions spans as two text nodes and two spans in the view', () => {
  68. editor.setData(
  69. '<p>' +
  70. '<span class="mention" data-mention="John">@John</span>' +
  71. '<span class="mention" data-mention="John">@John</span>' +
  72. '</p>'
  73. );
  74. // getModelData() merges text blocks with "same" attributes:
  75. // So expected: <$text mention="{"name":"John"}">@John</$text><$text mention="{"name":"John"}">@John</$text>'
  76. // Is returned as: <$text mention="{"name":"John"}">@John@John</$text>'
  77. const paragraph = doc.getRoot().getChild( 0 );
  78. expect( paragraph.childCount ).to.equal( 2 );
  79. assertTextNode( paragraph.getChild( 0 ) );
  80. assertTextNode( paragraph.getChild( 1 ) );
  81. const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' )._id;
  82. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._id;
  83. expect( firstMentionId ).to.not.equal( secondMentionId );
  84. const expectedView = '<p><span class="mention" data-mention="John">@John</span>' +
  85. '<span class="mention" data-mention="John">@John</span></p>';
  86. expect( editor.getData() ).to.equal( expectedView );
  87. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  88. function assertTextNode( textNode ) {
  89. expect( textNode ).to.not.be.null;
  90. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  91. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  92. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  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. const expectedView = '<p>@Jo</p>';
  100. expect( editor.getData() ).to.equal( expectedView );
  101. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  102. } );
  103. it( 'should not convert empty mentions', () => {
  104. editor.setData( '<p>foo<span class="mention" data-mention="John"></span></p>' );
  105. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  106. const expectedView = '<p>foo</p>';
  107. expect( editor.getData() ).to.equal( expectedView );
  108. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  109. } );
  110. } );
  111. describe( 'selection post-fixer', () => {
  112. beforeEach( () => {
  113. return createTestEditor()
  114. .then( newEditor => {
  115. editor = newEditor;
  116. model = editor.model;
  117. doc = model.document;
  118. } );
  119. } );
  120. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  121. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  122. model.change( writer => {
  123. const paragraph = doc.getRoot().getChild( 0 );
  124. writer.setSelection( paragraph, 9 );
  125. } );
  126. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  127. } );
  128. it( 'should allow to type after a mention', () => {
  129. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  130. model.change( writer => {
  131. const paragraph = doc.getRoot().getChild( 0 );
  132. writer.setSelection( paragraph, 9 );
  133. writer.insertText( ' ', paragraph, 9 );
  134. } );
  135. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  136. } );
  137. } );
  138. describe( 'removing partial mention post-fixer', () => {
  139. beforeEach( () => {
  140. return createTestEditor()
  141. .then( newEditor => {
  142. editor = newEditor;
  143. model = editor.model;
  144. doc = model.document;
  145. } );
  146. } );
  147. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  148. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  149. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  150. expect( textNode ).to.not.be.null;
  151. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  152. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  153. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  154. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  155. model.change( writer => {
  156. const paragraph = doc.getRoot().getChild( 0 );
  157. writer.setSelection( paragraph, 6 );
  158. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  159. } );
  160. expect( getModelData( model, { withoutSelection: true } ) )
  161. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  162. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  163. } );
  164. it( 'should remove mention on removing a text at the beginning of a mention', () => {
  165. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  166. const paragraph = doc.getRoot().getChild( 0 );
  167. model.change( writer => {
  168. writer.setSelection( paragraph, 4 );
  169. } );
  170. model.enqueueChange( () => {
  171. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  172. model.deleteContent( doc.selection );
  173. } );
  174. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  175. } );
  176. it( 'should remove mention on removing a text in the middle a mention', () => {
  177. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  178. const paragraph = doc.getRoot().getChild( 0 );
  179. model.change( writer => {
  180. writer.setSelection( paragraph, 6 );
  181. } );
  182. model.enqueueChange( () => {
  183. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  184. model.deleteContent( doc.selection );
  185. } );
  186. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  187. } );
  188. it( 'should remove mention on removing a text at the and of a mention', () => {
  189. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  190. const paragraph = doc.getRoot().getChild( 0 );
  191. model.change( writer => {
  192. writer.setSelection( paragraph, 9 );
  193. } );
  194. model.enqueueChange( () => {
  195. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  196. model.deleteContent( doc.selection );
  197. } );
  198. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  199. } );
  200. it( 'should not remove mention on removing a text just after a mention', () => {
  201. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  202. const paragraph = doc.getRoot().getChild( 0 );
  203. // Set selection before bar.
  204. model.change( writer => {
  205. writer.setSelection( paragraph, 10 );
  206. } );
  207. model.enqueueChange( () => {
  208. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  209. model.deleteContent( doc.selection );
  210. } );
  211. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  212. } );
  213. it( 'should remove mention on inserting inline element inside a mention', () => {
  214. model.schema.register( 'inline', {
  215. allowWhere: '$text',
  216. isInline: true
  217. } );
  218. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  219. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  220. const paragraph = doc.getRoot().getChild( 0 );
  221. model.change( writer => {
  222. writer.insertElement( 'inline', paragraph, 7 );
  223. } );
  224. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  225. } );
  226. it( 'should remove mention when splitting paragraph with a mention', () => {
  227. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  228. const paragraph = doc.getRoot().getChild( 0 );
  229. model.change( writer => {
  230. writer.split( writer.createPositionAt( paragraph, 7 ) );
  231. } );
  232. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  233. } );
  234. } );
  235. describe( 'extend attribute on mention post-fixer', () => {
  236. beforeEach( () => {
  237. return createTestEditor()
  238. .then( newEditor => {
  239. editor = newEditor;
  240. model = editor.model;
  241. doc = model.document;
  242. } );
  243. } );
  244. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  245. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  246. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  247. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  248. const paragraph = doc.getRoot().getChild( 0 );
  249. model.change( writer => {
  250. const start = writer.createPositionAt( paragraph, 0 );
  251. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  252. writer.setSelection( range );
  253. writer.setAttribute( 'bold', true, range );
  254. } );
  255. expect( editor.getData() )
  256. .to.equal( '<p><strong>foo </strong><span class="mention" data-mention="John"><strong>@John</strong></span> bar</p>' );
  257. } );
  258. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  259. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  260. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  261. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  262. const paragraph = doc.getRoot().getChild( 0 );
  263. model.change( writer => {
  264. const start = writer.createPositionAt( paragraph, 6 );
  265. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  266. writer.setSelection( range );
  267. writer.setAttribute( 'bold', true, range );
  268. } );
  269. expect( editor.getData() )
  270. .to.equal( '<p>foo <span class="mention" data-mention="John"><strong>@John</strong></span><strong> ba</strong>r</p>' );
  271. } );
  272. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  273. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  274. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  275. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  276. const paragraph = doc.getRoot().getChild( 0 );
  277. model.change( writer => {
  278. const start = writer.createPositionAt( paragraph, 6 );
  279. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  280. writer.setSelection( range );
  281. writer.setAttribute( 'bold', true, range );
  282. } );
  283. expect( editor.getData() )
  284. .to.equal( '<p>foo <span class="mention" data-mention="John"><strong>@John</strong></span> bar</p>' );
  285. } );
  286. } );
  287. function createTestEditor( mentionConfig ) {
  288. return VirtualTestEditor
  289. .create( {
  290. plugins: [ Paragraph, MentionEditing ],
  291. mention: mentionConfig
  292. } );
  293. }
  294. } );