8
0

mentionediting.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { stringify as stringifyView, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import MentionEditing, { _toMentionAttribute } from '../src/mentionediting';
  12. import MentionCommand from '../src/mentioncommand';
  13. describe( 'MentionEditing', () => {
  14. let editor, model, doc;
  15. testUtils.createSinonSandbox();
  16. afterEach( () => {
  17. if ( editor ) {
  18. return editor.destroy();
  19. }
  20. } );
  21. it( 'should be named', () => {
  22. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  23. } );
  24. it( 'should be loaded', () => {
  25. return createTestEditor()
  26. .then( newEditor => {
  27. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  28. } );
  29. } );
  30. it( 'should set proper schema rules', () => {
  31. return createTestEditor()
  32. .then( newEditor => {
  33. model = newEditor.model;
  34. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  37. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  38. } );
  39. } );
  40. it( 'should register mention command', () => {
  41. return createTestEditor()
  42. .then( newEditor => {
  43. const command = newEditor.commands.get( 'mention' );
  44. expect( command ).to.be.instanceof( MentionCommand );
  45. } );
  46. } );
  47. describe( 'conversion', () => {
  48. beforeEach( () => {
  49. return createTestEditor()
  50. .then( newEditor => {
  51. editor = newEditor;
  52. model = editor.model;
  53. doc = model.document;
  54. } );
  55. } );
  56. it( 'should convert <span class="mention" data-mention="@John"> to mention attribute', () => {
  57. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  58. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  59. expect( textNode ).to.not.be.null;
  60. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  61. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  62. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  63. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  64. const expectedView = '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>';
  65. expect( editor.getData() ).to.equal( expectedView );
  66. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  67. } );
  68. it( 'should be overridable', () => {
  69. addCustomMentionConverters( editor );
  70. editor.setData( '<p>Hello <b class="mention" data-mention="@Ted Mosby">Ted Mosby</b></p>' );
  71. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  72. expect( textNode ).to.not.be.null;
  73. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  74. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@Ted Mosby' );
  75. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', 'Ted Mosby' );
  76. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  77. const expectedView = '<p>Hello <b class="mention" data-mention="@Ted Mosby">Ted Mosby</b></p>';
  78. expect( editor.getData() ).to.equal( expectedView );
  79. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  80. } );
  81. it( 'should convert consecutive mentions spans as two text nodes and two spans in the view', () => {
  82. editor.setData(
  83. '<p>' +
  84. '<span class="mention" data-mention="@John">@John</span>' +
  85. '<span class="mention" data-mention="@John">@John</span>' +
  86. '</p>'
  87. );
  88. // getModelData() merges text blocks with "same" attributes:
  89. // So expected: <$text mention="{"name":"John"}">@John</$text><$text mention="{"name":"John"}">@John</$text>'
  90. // Is returned as: <$text mention="{"name":"John"}">@John@John</$text>'
  91. const paragraph = doc.getRoot().getChild( 0 );
  92. expect( paragraph.childCount ).to.equal( 2 );
  93. assertTextNode( paragraph.getChild( 0 ) );
  94. assertTextNode( paragraph.getChild( 1 ) );
  95. const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' ).uid;
  96. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' ).uid;
  97. expect( firstMentionId ).to.not.equal( secondMentionId );
  98. const expectedView = '<p><span class="mention" data-mention="@John">@John</span>' +
  99. '<span class="mention" data-mention="@John">@John</span></p>';
  100. expect( editor.getData() ).to.equal( expectedView );
  101. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  102. function assertTextNode( textNode ) {
  103. expect( textNode ).to.not.be.null;
  104. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  105. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  106. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  107. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  108. }
  109. } );
  110. it( 'should upcast partial mention', () => {
  111. editor.setData( '<p><span class="mention" data-mention="@John">@Jo</span></p>' );
  112. const textNode = doc.getRoot().getChild( 0 ).getChild( 0 );
  113. expect( textNode ).to.not.be.null;
  114. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  115. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  116. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@Jo' );
  117. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  118. const expectedView = '<p><span class="mention" data-mention="@John">@Jo</span></p>';
  119. expect( editor.getData() ).to.equal( expectedView );
  120. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  121. } );
  122. it( 'should not downcast partial mention (default converter)', done => {
  123. editor.setData( '<p>Hello <span class="mention" data-mention="@John">@John</span></p>' );
  124. model.change( writer => {
  125. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  126. const end = writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 );
  127. writer.setSelection( writer.createRange( start, end ) );
  128. } );
  129. const dataTransferMock = createDataTransfer();
  130. const preventDefaultSpy = sinon.spy();
  131. editor.editing.view.document.on( 'clipboardOutput', ( evt, data ) => {
  132. expect( stringifyView( data.content ) ).to.equal( 'Hello @Jo' );
  133. done();
  134. } );
  135. editor.editing.view.document.fire( 'copy', {
  136. dataTransfer: dataTransferMock,
  137. preventDefault: preventDefaultSpy
  138. } );
  139. } );
  140. it( 'should not downcast partial mention (custom converter)', done => {
  141. addCustomMentionConverters( editor );
  142. editor.conversion.for( 'downcast' ).attributeToElement( {
  143. model: 'mention',
  144. view: ( modelAttributeValue, { writer } ) => {
  145. if ( !modelAttributeValue ) {
  146. return;
  147. }
  148. return writer.createAttributeElement( 'a', {
  149. class: 'mention',
  150. 'data-mention': modelAttributeValue.id,
  151. 'href': modelAttributeValue.link
  152. }, {
  153. priority: 20,
  154. id: modelAttributeValue.uid
  155. } );
  156. },
  157. converterPriority: 'high'
  158. } );
  159. editor.setData( '<p>Hello <b class="mention" data-mention="@Ted Mosby">Ted Mosby</b></p>' );
  160. model.change( writer => {
  161. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  162. const end = writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 );
  163. writer.setSelection( writer.createRange( start, end ) );
  164. } );
  165. const dataTransferMock = createDataTransfer();
  166. const preventDefaultSpy = sinon.spy();
  167. editor.editing.view.document.on( 'clipboardOutput', ( evt, data ) => {
  168. expect( stringifyView( data.content ) ).to.equal( 'Hello Ted' );
  169. done();
  170. } );
  171. editor.editing.view.document.fire( 'copy', {
  172. dataTransfer: dataTransferMock,
  173. preventDefault: preventDefaultSpy
  174. } );
  175. } );
  176. it( 'should not convert empty mentions', () => {
  177. editor.setData( '<p>foo<span class="mention" data-mention="@John"></span></p>' );
  178. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  179. const expectedView = '<p>foo</p>';
  180. expect( editor.getData() ).to.equal( expectedView );
  181. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  182. } );
  183. } );
  184. describe( 'selection post-fixer', () => {
  185. beforeEach( () => {
  186. return createTestEditor()
  187. .then( newEditor => {
  188. editor = newEditor;
  189. model = editor.model;
  190. doc = model.document;
  191. } );
  192. } );
  193. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  194. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  195. model.change( writer => {
  196. const paragraph = doc.getRoot().getChild( 0 );
  197. writer.setSelection( paragraph, 9 );
  198. } );
  199. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  200. } );
  201. it( 'should allow to type after a mention', () => {
  202. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  203. model.change( writer => {
  204. const paragraph = doc.getRoot().getChild( 0 );
  205. writer.setSelection( paragraph, 9 );
  206. writer.insertText( ' ', paragraph, 9 );
  207. } );
  208. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  209. } );
  210. it( 'should not allow to type with mention attribute before mention', () => {
  211. editor.setData( '<p><span class="mention" data-mention="@John">@John</span> bar</p>' );
  212. const paragraph = doc.getRoot().getChild( 0 );
  213. // Set selection before mention.
  214. model.change( writer => {
  215. writer.setSelection( paragraph, 0 );
  216. } );
  217. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  218. model.change( writer => {
  219. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 0 ) );
  220. } );
  221. expect( editor.getData() ).to.equal( '<p>a<span class="mention" data-mention="@John">@John</span> bar</p>' );
  222. } );
  223. } );
  224. describe( 'removing partial mention post-fixer', () => {
  225. beforeEach( () => {
  226. return createTestEditor()
  227. .then( newEditor => {
  228. editor = newEditor;
  229. model = editor.model;
  230. doc = model.document;
  231. } );
  232. } );
  233. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  234. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  235. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  236. expect( textNode ).to.not.be.null;
  237. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  238. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  239. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  240. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  241. model.change( writer => {
  242. const paragraph = doc.getRoot().getChild( 0 );
  243. writer.setSelection( paragraph, 6 );
  244. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  245. } );
  246. expect( getModelData( model, { withoutSelection: true } ) )
  247. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  248. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  249. } );
  250. it( 'should remove mention on typing in mention node with selection attributes set', () => {
  251. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  252. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  253. expect( textNode ).to.not.be.null;
  254. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  255. model.change( writer => {
  256. const paragraph = doc.getRoot().getChild( 0 );
  257. writer.setSelection( paragraph, 6 );
  258. writer.setSelectionAttribute( 'bold', true );
  259. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  260. } );
  261. expect( getModelData( model, { withoutSelection: true } ) )
  262. .to.equal( '<paragraph>foo @J<$text bold="true">a</$text>ohn bar</paragraph>' );
  263. } );
  264. it( 'should remove mention on removing a text at the beginning of a mention', () => {
  265. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  266. const paragraph = doc.getRoot().getChild( 0 );
  267. model.change( writer => {
  268. writer.setSelection( paragraph, 4 );
  269. } );
  270. model.enqueueChange( () => {
  271. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  272. model.deleteContent( doc.selection );
  273. } );
  274. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  275. } );
  276. it( 'should remove mention on removing a text in the middle a mention', () => {
  277. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  278. const paragraph = doc.getRoot().getChild( 0 );
  279. model.change( writer => {
  280. writer.setSelection( paragraph, 6 );
  281. } );
  282. model.enqueueChange( () => {
  283. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  284. model.deleteContent( doc.selection );
  285. } );
  286. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  287. } );
  288. it( 'should remove mention on removing a text at the and of a mention', () => {
  289. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  290. const paragraph = doc.getRoot().getChild( 0 );
  291. model.change( writer => {
  292. writer.setSelection( paragraph, 9 );
  293. } );
  294. model.enqueueChange( () => {
  295. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  296. model.deleteContent( doc.selection );
  297. } );
  298. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  299. } );
  300. it( 'should not remove mention on removing a text just after a mention', () => {
  301. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  302. const paragraph = doc.getRoot().getChild( 0 );
  303. // Set selection before bar.
  304. model.change( writer => {
  305. writer.setSelection( paragraph, 10 );
  306. } );
  307. model.enqueueChange( () => {
  308. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  309. model.deleteContent( doc.selection );
  310. } );
  311. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  312. } );
  313. it( 'should remove mention on inserting text node inside a mention', () => {
  314. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  315. const paragraph = doc.getRoot().getChild( 0 );
  316. model.change( writer => {
  317. writer.insertText( 'baz', paragraph, 7 );
  318. } );
  319. expect( editor.getData() ).to.equal( '<p>foo @Jobazhn bar</p>' );
  320. } );
  321. it( 'should remove mention on inserting inline element inside a mention', () => {
  322. model.schema.register( 'inline', {
  323. allowWhere: '$text',
  324. isInline: true
  325. } );
  326. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  327. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  328. const paragraph = doc.getRoot().getChild( 0 );
  329. model.change( writer => {
  330. writer.insertElement( 'inline', paragraph, 7 );
  331. } );
  332. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  333. } );
  334. it( 'should remove mention when splitting paragraph with a mention', () => {
  335. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  336. const paragraph = doc.getRoot().getChild( 0 );
  337. model.change( writer => {
  338. writer.split( writer.createPositionAt( paragraph, 7 ) );
  339. } );
  340. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  341. } );
  342. it( 'should remove mention when deep splitting elements', () => {
  343. model.schema.register( 'blockQuote', {
  344. allowWhere: '$block',
  345. allowContentOf: '$root'
  346. } );
  347. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  348. editor.setData( '<blockquote><p>foo <span class="mention" data-mention="@John">@John</span> bar</p></blockquote>' );
  349. model.change( writer => {
  350. const paragraph = doc.getRoot().getChild( 0 ).getChild( 0 );
  351. writer.split( writer.createPositionAt( paragraph, 7 ), doc.getRoot() );
  352. } );
  353. expect( editor.getData() ).to.equal( '<blockquote><p>foo @Jo</p></blockquote><blockquote><p>hn bar</p></blockquote>' );
  354. } );
  355. } );
  356. describe( 'extend attribute on mention post-fixer', () => {
  357. beforeEach( () => {
  358. return createTestEditor()
  359. .then( newEditor => {
  360. editor = newEditor;
  361. model = editor.model;
  362. doc = model.document;
  363. } );
  364. } );
  365. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  366. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  367. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  368. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  369. const paragraph = doc.getRoot().getChild( 0 );
  370. model.change( writer => {
  371. const start = writer.createPositionAt( paragraph, 0 );
  372. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  373. writer.setSelection( range );
  374. writer.setAttribute( 'bold', true, range );
  375. } );
  376. expect( editor.getData() )
  377. .to.equal( '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  378. } );
  379. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  380. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  381. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  382. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  383. const paragraph = doc.getRoot().getChild( 0 );
  384. model.change( writer => {
  385. const start = writer.createPositionAt( paragraph, 6 );
  386. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  387. writer.setSelection( range );
  388. writer.setAttribute( 'bold', true, range );
  389. } );
  390. expect( editor.getData() )
  391. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>' );
  392. } );
  393. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  394. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  395. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  396. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  397. const paragraph = doc.getRoot().getChild( 0 );
  398. model.change( writer => {
  399. const start = writer.createPositionAt( paragraph, 6 );
  400. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  401. writer.setSelection( range );
  402. writer.setAttribute( 'bold', true, range );
  403. } );
  404. expect( editor.getData() )
  405. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  406. } );
  407. it( 'should set attribute on whole mention when formatting part of two mentions', () => {
  408. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  409. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  410. editor.setData(
  411. '<p><span class="mention" data-mention="@John">@John</span><span class="mention" data-mention="@John">@John</span></p>'
  412. );
  413. const paragraph = doc.getRoot().getChild( 0 );
  414. model.change( writer => {
  415. const start = writer.createPositionAt( paragraph, 4 );
  416. const range = writer.createRange( start, start.getShiftedBy( 4 ) );
  417. writer.setSelection( range );
  418. writer.setAttribute( 'bold', true, range );
  419. } );
  420. expect( editor.getData() ).to.equal(
  421. '<p>' +
  422. '<strong>' +
  423. '<span class="mention" data-mention="@John">@John</span>' +
  424. '<span class="mention" data-mention="@John">@John</span>' +
  425. '</strong>' +
  426. '</p>'
  427. );
  428. } );
  429. it( 'should work with multiple ranges in change set', () => {
  430. model.schema.extend( '$text', { allowAttributes: [ 'foo' ] } );
  431. editor.conversion.attributeToElement( {
  432. model: {
  433. key: 'foo',
  434. values: [ 'a', 'b' ]
  435. },
  436. view: {
  437. a: {
  438. name: 'span',
  439. classes: 'mark-a'
  440. },
  441. b: {
  442. name: 'span',
  443. classes: 'mark-b'
  444. }
  445. },
  446. converterPriority: 'high'
  447. } );
  448. editor.setData(
  449. '<p>' +
  450. '<span class="mark-a">foo <span class="mention" data-mention="@John">@John</span></span>' +
  451. '<span class="mention" data-mention="@John">@John</span> bar' +
  452. '</p>'
  453. );
  454. model.change( writer => {
  455. const paragraph = doc.getRoot().getChild( 0 );
  456. const start = writer.createPositionAt( paragraph, 7 );
  457. const range = writer.createRange( start, start.getShiftedBy( 5 ) );
  458. writer.setAttribute( 'foo', 'b', range );
  459. } );
  460. expect( editor.getData() ).to.equal(
  461. '<p>' +
  462. '<span class="mark-a">foo </span>' +
  463. '<span class="mark-b">' +
  464. '<span class="mention" data-mention="@John">@John</span>' +
  465. '<span class="mention" data-mention="@John">@John</span>' +
  466. '</span> bar' +
  467. '</p>'
  468. );
  469. } );
  470. } );
  471. function createTestEditor( mentionConfig ) {
  472. return VirtualTestEditor
  473. .create( {
  474. plugins: [ Paragraph, MentionEditing, Clipboard ],
  475. mention: mentionConfig
  476. } );
  477. }
  478. function createDataTransfer() {
  479. const store = new Map();
  480. return {
  481. setData( type, data ) {
  482. store.set( type, data );
  483. },
  484. getData( type ) {
  485. return store.get( type );
  486. }
  487. };
  488. }
  489. } );
  490. function addCustomMentionConverters( editor ) {
  491. editor.conversion.for( 'upcast' ).elementToAttribute( {
  492. view: {
  493. name: 'b',
  494. key: 'data-mention',
  495. classes: 'mention'
  496. },
  497. model: {
  498. key: 'mention',
  499. value: viewItem => {
  500. return _toMentionAttribute( viewItem );
  501. }
  502. },
  503. converterPriority: 'high'
  504. } );
  505. editor.conversion.for( 'downcast' ).attributeToElement( {
  506. model: 'mention',
  507. view: ( modelAttributeValue, { writer } ) => {
  508. if ( !modelAttributeValue ) {
  509. return;
  510. }
  511. return writer.createAttributeElement( 'b', {
  512. class: 'mention',
  513. 'data-mention': modelAttributeValue.id
  514. }, {
  515. priority: 20,
  516. id: modelAttributeValue.uid
  517. } );
  518. },
  519. converterPriority: 'high'
  520. } );
  521. }