8
0

mentionediting.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. it( 'should not allow to type with mention attribute before mention', () => {
  208. editor.setData( '<p><span class="mention" data-mention="@John">@John</span> bar</p>' );
  209. const paragraph = doc.getRoot().getChild( 0 );
  210. // Set selection before mention.
  211. model.change( writer => {
  212. writer.setSelection( paragraph, 0 );
  213. } );
  214. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  215. model.change( writer => {
  216. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 0 ) );
  217. } );
  218. expect( editor.getData() ).to.equal( '<p>a<span class="mention" data-mention="@John">@John</span> bar</p>' );
  219. } );
  220. } );
  221. describe( 'removing partial mention post-fixer', () => {
  222. beforeEach( () => {
  223. return createTestEditor()
  224. .then( newEditor => {
  225. editor = newEditor;
  226. model = editor.model;
  227. doc = model.document;
  228. } );
  229. } );
  230. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  231. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  232. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  233. expect( textNode ).to.not.be.null;
  234. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  235. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  236. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  237. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  238. model.change( writer => {
  239. const paragraph = doc.getRoot().getChild( 0 );
  240. writer.setSelection( paragraph, 6 );
  241. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  242. } );
  243. expect( getModelData( model, { withoutSelection: true } ) )
  244. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  245. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  246. } );
  247. it( 'should remove mention on typing in mention node with selection attributes set', () => {
  248. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  249. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  250. expect( textNode ).to.not.be.null;
  251. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  252. model.change( writer => {
  253. const paragraph = doc.getRoot().getChild( 0 );
  254. writer.setSelection( paragraph, 6 );
  255. writer.setSelectionAttribute( 'bold', true );
  256. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  257. } );
  258. expect( getModelData( model, { withoutSelection: true } ) )
  259. .to.equal( '<paragraph>foo @J<$text bold="true">a</$text>ohn bar</paragraph>' );
  260. } );
  261. it( 'should remove mention on removing a text at the beginning of 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, 4 );
  266. } );
  267. model.enqueueChange( () => {
  268. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  269. model.deleteContent( doc.selection );
  270. } );
  271. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  272. } );
  273. it( 'should remove mention on removing a text in the middle 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, 6 );
  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 @ohn bar</p>' );
  284. } );
  285. it( 'should remove mention on removing a text at the and of 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. model.change( writer => {
  289. writer.setSelection( paragraph, 9 );
  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 @Joh bar</p>' );
  296. } );
  297. it( 'should not remove mention on removing a text just after 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. // Set selection before bar.
  301. model.change( writer => {
  302. writer.setSelection( paragraph, 10 );
  303. } );
  304. model.enqueueChange( () => {
  305. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  306. model.deleteContent( doc.selection );
  307. } );
  308. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  309. } );
  310. it( 'should remove mention on inserting text node inside a mention', () => {
  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.insertText( 'baz', paragraph, 7 );
  315. } );
  316. expect( editor.getData() ).to.equal( '<p>foo @Jobazhn bar</p>' );
  317. } );
  318. it( 'should remove mention on inserting inline element inside a mention', () => {
  319. model.schema.register( 'inline', {
  320. allowWhere: '$text',
  321. isInline: true
  322. } );
  323. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  324. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  325. const paragraph = doc.getRoot().getChild( 0 );
  326. model.change( writer => {
  327. writer.insertElement( 'inline', paragraph, 7 );
  328. } );
  329. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  330. } );
  331. it( 'should remove mention when splitting paragraph with a mention', () => {
  332. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  333. const paragraph = doc.getRoot().getChild( 0 );
  334. model.change( writer => {
  335. writer.split( writer.createPositionAt( paragraph, 7 ) );
  336. } );
  337. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  338. } );
  339. it( 'should remove mention when deep splitting elements', () => {
  340. model.schema.register( 'blockQuote', {
  341. allowWhere: '$block',
  342. allowContentOf: '$root'
  343. } );
  344. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  345. editor.setData( '<blockquote><p>foo <span class="mention" data-mention="@John">@John</span> bar</p></blockquote>' );
  346. model.change( writer => {
  347. const paragraph = doc.getRoot().getChild( 0 ).getChild( 0 );
  348. writer.split( writer.createPositionAt( paragraph, 7 ), doc.getRoot() );
  349. } );
  350. expect( editor.getData() ).to.equal( '<blockquote><p>foo @Jo</p></blockquote><blockquote><p>hn bar</p></blockquote>' );
  351. } );
  352. } );
  353. describe( 'extend attribute on mention post-fixer', () => {
  354. beforeEach( () => {
  355. return createTestEditor()
  356. .then( newEditor => {
  357. editor = newEditor;
  358. model = editor.model;
  359. doc = model.document;
  360. } );
  361. } );
  362. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  363. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  364. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  365. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  366. const paragraph = doc.getRoot().getChild( 0 );
  367. model.change( writer => {
  368. const start = writer.createPositionAt( paragraph, 0 );
  369. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  370. writer.setSelection( range );
  371. writer.setAttribute( 'bold', true, range );
  372. } );
  373. expect( editor.getData() )
  374. .to.equal( '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  375. } );
  376. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  377. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  378. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  379. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  380. const paragraph = doc.getRoot().getChild( 0 );
  381. model.change( writer => {
  382. const start = writer.createPositionAt( paragraph, 6 );
  383. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  384. writer.setSelection( range );
  385. writer.setAttribute( 'bold', true, range );
  386. } );
  387. expect( editor.getData() )
  388. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>' );
  389. } );
  390. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  391. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  392. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  393. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  394. const paragraph = doc.getRoot().getChild( 0 );
  395. model.change( writer => {
  396. const start = writer.createPositionAt( paragraph, 6 );
  397. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  398. writer.setSelection( range );
  399. writer.setAttribute( 'bold', true, range );
  400. } );
  401. expect( editor.getData() )
  402. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  403. } );
  404. it( 'should set attribute on whole mention when formatting part of two mentions', () => {
  405. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  406. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  407. editor.setData(
  408. '<p><span class="mention" data-mention="@John">@John</span><span class="mention" data-mention="@John">@John</span></p>'
  409. );
  410. const paragraph = doc.getRoot().getChild( 0 );
  411. model.change( writer => {
  412. const start = writer.createPositionAt( paragraph, 4 );
  413. const range = writer.createRange( start, start.getShiftedBy( 4 ) );
  414. writer.setSelection( range );
  415. writer.setAttribute( 'bold', true, range );
  416. } );
  417. expect( editor.getData() ).to.equal(
  418. '<p>' +
  419. '<strong>' +
  420. '<span class="mention" data-mention="@John">@John</span>' +
  421. '<span class="mention" data-mention="@John">@John</span>' +
  422. '</strong>' +
  423. '</p>'
  424. );
  425. } );
  426. it( 'should work with multiple ranges in change set', () => {
  427. model.schema.extend( '$text', { allowAttributes: [ 'foo' ] } );
  428. editor.conversion.attributeToElement( {
  429. model: {
  430. key: 'foo',
  431. values: [ 'a', 'b' ]
  432. },
  433. view: {
  434. a: {
  435. name: 'span',
  436. classes: 'mark-a'
  437. },
  438. b: {
  439. name: 'span',
  440. classes: 'mark-b'
  441. }
  442. },
  443. converterPriority: 'high'
  444. } );
  445. editor.setData(
  446. '<p>' +
  447. '<span class="mark-a">foo <span class="mention" data-mention="@John">@John</span></span>' +
  448. '<span class="mention" data-mention="@John">@John</span> bar' +
  449. '</p>'
  450. );
  451. model.change( writer => {
  452. const paragraph = doc.getRoot().getChild( 0 );
  453. const start = writer.createPositionAt( paragraph, 7 );
  454. const range = writer.createRange( start, start.getShiftedBy( 5 ) );
  455. writer.setAttribute( 'foo', 'b', range );
  456. } );
  457. expect( editor.getData() ).to.equal(
  458. '<p>' +
  459. '<span class="mark-a">foo </span>' +
  460. '<span class="mark-b">' +
  461. '<span class="mention" data-mention="@John">@John</span>' +
  462. '<span class="mention" data-mention="@John">@John</span>' +
  463. '</span> bar' +
  464. '</p>'
  465. );
  466. } );
  467. } );
  468. function createTestEditor( mentionConfig ) {
  469. return VirtualTestEditor
  470. .create( {
  471. plugins: [ Paragraph, MentionEditing, Clipboard ],
  472. mention: mentionConfig
  473. } );
  474. }
  475. function createDataTransfer() {
  476. const store = new Map();
  477. return {
  478. setData( type, data ) {
  479. store.set( type, data );
  480. },
  481. getData( type ) {
  482. return store.get( type );
  483. }
  484. };
  485. }
  486. } );
  487. function addCustomMentionConverters( editor ) {
  488. editor.conversion.for( 'upcast' ).elementToAttribute( {
  489. view: {
  490. name: 'b',
  491. key: 'data-mention',
  492. classes: 'mention'
  493. },
  494. model: {
  495. key: 'mention',
  496. value: viewItem => {
  497. return _toMentionAttribute( viewItem );
  498. }
  499. },
  500. converterPriority: 'high'
  501. } );
  502. editor.conversion.for( 'downcast' ).attributeToElement( {
  503. model: 'mention',
  504. view: ( modelAttributeValue, viewWriter ) => {
  505. if ( !modelAttributeValue ) {
  506. return;
  507. }
  508. return viewWriter.createAttributeElement( 'b', {
  509. class: 'mention',
  510. 'data-mention': modelAttributeValue.id
  511. }, { id: modelAttributeValue._uid } );
  512. },
  513. converterPriority: 'high'
  514. } );
  515. }