mentionediting.js 22 KB

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