8
0

mentionediting.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. 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, viewWriter ) => {
  145. if ( !modelAttributeValue ) {
  146. return;
  147. }
  148. return viewWriter.createAttributeElement( 'a', {
  149. class: 'mention',
  150. 'data-mention': modelAttributeValue.id,
  151. 'href': modelAttributeValue.link
  152. }, { id: modelAttributeValue._uid } );
  153. },
  154. converterPriority: 'high'
  155. } );
  156. editor.setData( '<p>Hello <b class="mention" data-mention="@Ted Mosby">Ted Mosby</b></p>' );
  157. model.change( writer => {
  158. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  159. const end = writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 );
  160. writer.setSelection( writer.createRange( start, end ) );
  161. } );
  162. const dataTransferMock = createDataTransfer();
  163. const preventDefaultSpy = sinon.spy();
  164. editor.editing.view.document.on( 'clipboardOutput', ( evt, data ) => {
  165. expect( stringifyView( data.content ) ).to.equal( 'Hello Ted' );
  166. done();
  167. } );
  168. editor.editing.view.document.fire( 'copy', {
  169. dataTransfer: dataTransferMock,
  170. preventDefault: preventDefaultSpy
  171. } );
  172. } );
  173. it( 'should not convert empty mentions', () => {
  174. editor.setData( '<p>foo<span class="mention" data-mention="@John"></span></p>' );
  175. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  176. const expectedView = '<p>foo</p>';
  177. expect( editor.getData() ).to.equal( expectedView );
  178. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  179. } );
  180. } );
  181. describe( 'selection post-fixer', () => {
  182. beforeEach( () => {
  183. return createTestEditor()
  184. .then( newEditor => {
  185. editor = newEditor;
  186. model = editor.model;
  187. doc = model.document;
  188. } );
  189. } );
  190. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  191. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  192. model.change( writer => {
  193. const paragraph = doc.getRoot().getChild( 0 );
  194. writer.setSelection( paragraph, 9 );
  195. } );
  196. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  197. } );
  198. it( 'should allow to type after a mention', () => {
  199. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  200. model.change( writer => {
  201. const paragraph = doc.getRoot().getChild( 0 );
  202. writer.setSelection( paragraph, 9 );
  203. writer.insertText( ' ', paragraph, 9 );
  204. } );
  205. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  206. } );
  207. } );
  208. describe( 'removing partial mention post-fixer', () => {
  209. beforeEach( () => {
  210. return createTestEditor()
  211. .then( newEditor => {
  212. editor = newEditor;
  213. model = editor.model;
  214. doc = model.document;
  215. } );
  216. } );
  217. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  218. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  219. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  220. expect( textNode ).to.not.be.null;
  221. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  222. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  223. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  224. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  225. model.change( writer => {
  226. const paragraph = doc.getRoot().getChild( 0 );
  227. writer.setSelection( paragraph, 6 );
  228. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  229. } );
  230. expect( getModelData( model, { withoutSelection: true } ) )
  231. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  232. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  233. } );
  234. it( 'should remove mention on typing in mention node with selection attributes set', () => {
  235. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  236. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  237. expect( textNode ).to.not.be.null;
  238. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  239. model.change( writer => {
  240. const paragraph = doc.getRoot().getChild( 0 );
  241. writer.setSelection( paragraph, 6 );
  242. writer.setSelectionAttribute( 'bold', true );
  243. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  244. } );
  245. expect( getModelData( model, { withoutSelection: true } ) )
  246. .to.equal( '<paragraph>foo @J<$text bold="true">a</$text>ohn bar</paragraph>' );
  247. } );
  248. it( 'should remove mention on removing a text at the beginning of a mention', () => {
  249. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  250. const paragraph = doc.getRoot().getChild( 0 );
  251. model.change( writer => {
  252. writer.setSelection( paragraph, 4 );
  253. } );
  254. model.enqueueChange( () => {
  255. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  256. model.deleteContent( doc.selection );
  257. } );
  258. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  259. } );
  260. it( 'should remove mention on removing a text in the middle a mention', () => {
  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. writer.setSelection( paragraph, 6 );
  265. } );
  266. model.enqueueChange( () => {
  267. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  268. model.deleteContent( doc.selection );
  269. } );
  270. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  271. } );
  272. it( 'should remove mention on removing a text at the and of a mention', () => {
  273. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  274. const paragraph = doc.getRoot().getChild( 0 );
  275. model.change( writer => {
  276. writer.setSelection( paragraph, 9 );
  277. } );
  278. model.enqueueChange( () => {
  279. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  280. model.deleteContent( doc.selection );
  281. } );
  282. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  283. } );
  284. it( 'should not remove mention on removing a text just after a mention', () => {
  285. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  286. const paragraph = doc.getRoot().getChild( 0 );
  287. // Set selection before bar.
  288. model.change( writer => {
  289. writer.setSelection( paragraph, 10 );
  290. } );
  291. model.enqueueChange( () => {
  292. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  293. model.deleteContent( doc.selection );
  294. } );
  295. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  296. } );
  297. it( 'should remove mention on inserting text node inside a mention', () => {
  298. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  299. const paragraph = doc.getRoot().getChild( 0 );
  300. model.change( writer => {
  301. writer.insertText( 'baz', paragraph, 7 );
  302. } );
  303. expect( editor.getData() ).to.equal( '<p>foo @Jobazhn bar</p>' );
  304. } );
  305. it( 'should remove mention on inserting inline element inside a mention', () => {
  306. model.schema.register( 'inline', {
  307. allowWhere: '$text',
  308. isInline: true
  309. } );
  310. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  311. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  312. const paragraph = doc.getRoot().getChild( 0 );
  313. model.change( writer => {
  314. writer.insertElement( 'inline', paragraph, 7 );
  315. } );
  316. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  317. } );
  318. it( 'should remove mention when splitting paragraph with a mention', () => {
  319. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  320. const paragraph = doc.getRoot().getChild( 0 );
  321. model.change( writer => {
  322. writer.split( writer.createPositionAt( paragraph, 7 ) );
  323. } );
  324. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  325. } );
  326. it( 'should remove mention when deep splitting elements', () => {
  327. model.schema.register( 'blockQuote', {
  328. allowWhere: '$block',
  329. allowContentOf: '$root'
  330. } );
  331. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  332. editor.setData( '<blockquote><p>foo <span class="mention" data-mention="@John">@John</span> bar</p></blockquote>' );
  333. model.change( writer => {
  334. const paragraph = doc.getRoot().getChild( 0 ).getChild( 0 );
  335. writer.split( writer.createPositionAt( paragraph, 7 ), doc.getRoot() );
  336. } );
  337. expect( editor.getData() ).to.equal( '<blockquote><p>foo @Jo</p></blockquote><blockquote><p>hn bar</p></blockquote>' );
  338. } );
  339. } );
  340. describe( 'extend attribute on mention post-fixer', () => {
  341. beforeEach( () => {
  342. return createTestEditor()
  343. .then( newEditor => {
  344. editor = newEditor;
  345. model = editor.model;
  346. doc = model.document;
  347. } );
  348. } );
  349. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  350. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  351. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  352. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  353. const paragraph = doc.getRoot().getChild( 0 );
  354. model.change( writer => {
  355. const start = writer.createPositionAt( paragraph, 0 );
  356. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  357. writer.setSelection( range );
  358. writer.setAttribute( 'bold', true, range );
  359. } );
  360. expect( editor.getData() )
  361. .to.equal( '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  362. } );
  363. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  364. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  365. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  366. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  367. const paragraph = doc.getRoot().getChild( 0 );
  368. model.change( writer => {
  369. const start = writer.createPositionAt( paragraph, 6 );
  370. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  371. writer.setSelection( range );
  372. writer.setAttribute( 'bold', true, range );
  373. } );
  374. expect( editor.getData() )
  375. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>' );
  376. } );
  377. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  378. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  379. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  380. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  381. const paragraph = doc.getRoot().getChild( 0 );
  382. model.change( writer => {
  383. const start = writer.createPositionAt( paragraph, 6 );
  384. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  385. writer.setSelection( range );
  386. writer.setAttribute( 'bold', true, range );
  387. } );
  388. expect( editor.getData() )
  389. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  390. } );
  391. it( 'should set attribute on whole mention when formatting part of two mentions', () => {
  392. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  393. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  394. editor.setData(
  395. '<p><span class="mention" data-mention="@John">@John</span><span class="mention" data-mention="@John">@John</span></p>'
  396. );
  397. const paragraph = doc.getRoot().getChild( 0 );
  398. model.change( writer => {
  399. const start = writer.createPositionAt( paragraph, 4 );
  400. const range = writer.createRange( start, start.getShiftedBy( 4 ) );
  401. writer.setSelection( range );
  402. writer.setAttribute( 'bold', true, range );
  403. } );
  404. expect( editor.getData() ).to.equal(
  405. '<p>' +
  406. '<strong>' +
  407. '<span class="mention" data-mention="@John">@John</span>' +
  408. '<span class="mention" data-mention="@John">@John</span>' +
  409. '</strong>' +
  410. '</p>'
  411. );
  412. } );
  413. it( 'should work with multiple ranges in change set', () => {
  414. model.schema.extend( '$text', { allowAttributes: [ 'foo' ] } );
  415. editor.conversion.attributeToElement( {
  416. model: {
  417. key: 'foo',
  418. values: [ 'a', 'b' ]
  419. },
  420. view: {
  421. a: {
  422. name: 'span',
  423. classes: 'mark-a'
  424. },
  425. b: {
  426. name: 'span',
  427. classes: 'mark-b'
  428. }
  429. },
  430. converterPriority: 'high'
  431. } );
  432. editor.setData(
  433. '<p>' +
  434. '<span class="mark-a">foo <span class="mention" data-mention="@John">@John</span></span>' +
  435. '<span class="mention" data-mention="@John">@John</span> bar' +
  436. '</p>'
  437. );
  438. model.change( writer => {
  439. const paragraph = doc.getRoot().getChild( 0 );
  440. const start = writer.createPositionAt( paragraph, 7 );
  441. const range = writer.createRange( start, start.getShiftedBy( 5 ) );
  442. writer.setAttribute( 'foo', 'b', range );
  443. } );
  444. expect( editor.getData() ).to.equal(
  445. '<p>' +
  446. '<span class="mark-a">foo </span>' +
  447. '<span class="mark-b">' +
  448. '<span class="mention" data-mention="@John">@John</span>' +
  449. '<span class="mention" data-mention="@John">@John</span>' +
  450. '</span> bar' +
  451. '</p>'
  452. );
  453. } );
  454. } );
  455. function createTestEditor( mentionConfig ) {
  456. return VirtualTestEditor
  457. .create( {
  458. plugins: [ Paragraph, MentionEditing, Clipboard ],
  459. mention: mentionConfig
  460. } );
  461. }
  462. function createDataTransfer() {
  463. const store = new Map();
  464. return {
  465. setData( type, data ) {
  466. store.set( type, data );
  467. },
  468. getData( type ) {
  469. return store.get( type );
  470. }
  471. };
  472. }
  473. } );
  474. function addCustomMentionConverters( editor ) {
  475. editor.conversion.for( 'upcast' ).elementToAttribute( {
  476. view: {
  477. name: 'b',
  478. key: 'data-mention',
  479. classes: 'mention'
  480. },
  481. model: {
  482. key: 'mention',
  483. value: viewItem => {
  484. return _toMentionAttribute( viewItem );
  485. }
  486. },
  487. converterPriority: 'high'
  488. } );
  489. editor.conversion.for( 'downcast' ).attributeToElement( {
  490. model: 'mention',
  491. view: ( modelAttributeValue, viewWriter ) => {
  492. if ( !modelAttributeValue ) {
  493. return;
  494. }
  495. return viewWriter.createAttributeElement( 'b', {
  496. class: 'mention',
  497. 'data-mention': modelAttributeValue.id
  498. }, { id: modelAttributeValue._uid } );
  499. },
  500. converterPriority: 'high'
  501. } );
  502. }