mentionediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import MentionEditing from '../src/mentionediting';
  11. import MentionCommand from '../src/mentioncommand';
  12. describe( 'MentionEditing', () => {
  13. let editor, model, doc;
  14. testUtils.createSinonSandbox();
  15. afterEach( () => {
  16. if ( editor ) {
  17. return editor.destroy();
  18. }
  19. } );
  20. it( 'should be named', () => {
  21. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  22. } );
  23. it( 'should be loaded', () => {
  24. return createTestEditor()
  25. .then( newEditor => {
  26. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  27. } );
  28. } );
  29. it( 'should set proper schema rules', () => {
  30. return createTestEditor()
  31. .then( newEditor => {
  32. model = newEditor.model;
  33. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  34. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  37. } );
  38. } );
  39. it( 'should register mention command', () => {
  40. return createTestEditor()
  41. .then( newEditor => {
  42. const command = newEditor.commands.get( 'mention' );
  43. expect( command ).to.be.instanceof( MentionCommand );
  44. } );
  45. } );
  46. describe( 'conversion', () => {
  47. beforeEach( () => {
  48. return createTestEditor()
  49. .then( newEditor => {
  50. editor = newEditor;
  51. model = editor.model;
  52. doc = model.document;
  53. } );
  54. } );
  55. it( 'should convert <span class="mention" data-mention="@John"> to mention attribute', () => {
  56. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  57. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  58. expect( textNode ).to.not.be.null;
  59. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  60. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  61. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  62. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  63. const expectedView = '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>';
  64. expect( editor.getData() ).to.equal( expectedView );
  65. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  66. } );
  67. it( 'should convert consecutive mentions spans as two text nodes and two spans in the view', () => {
  68. editor.setData(
  69. '<p>' +
  70. '<span class="mention" data-mention="@John">@John</span>' +
  71. '<span class="mention" data-mention="@John">@John</span>' +
  72. '</p>'
  73. );
  74. // getModelData() merges text blocks with "same" attributes:
  75. // So expected: <$text mention="{"name":"John"}">@John</$text><$text mention="{"name":"John"}">@John</$text>'
  76. // Is returned as: <$text mention="{"name":"John"}">@John@John</$text>'
  77. const paragraph = doc.getRoot().getChild( 0 );
  78. expect( paragraph.childCount ).to.equal( 2 );
  79. assertTextNode( paragraph.getChild( 0 ) );
  80. assertTextNode( paragraph.getChild( 1 ) );
  81. const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' )._uid;
  82. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._uid;
  83. expect( firstMentionId ).to.not.equal( secondMentionId );
  84. const expectedView = '<p><span class="mention" data-mention="@John">@John</span>' +
  85. '<span class="mention" data-mention="@John">@John</span></p>';
  86. expect( editor.getData() ).to.equal( expectedView );
  87. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  88. function assertTextNode( textNode ) {
  89. expect( textNode ).to.not.be.null;
  90. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  91. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  92. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  93. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  94. }
  95. } );
  96. it( 'should not convert partial mentions', () => {
  97. editor.setData( '<p><span class="mention" data-mention="@John">@Jo</span></p>' );
  98. const textNode = doc.getRoot().getChild( 0 ).getChild( 0 );
  99. expect( textNode ).to.not.be.null;
  100. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  101. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  102. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@Jo' );
  103. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  104. const expectedView = '<p><span class="mention" data-mention="@John">@Jo</span></p>';
  105. expect( editor.getData() ).to.equal( expectedView );
  106. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  107. } );
  108. it( 'should not convert empty mentions', () => {
  109. editor.setData( '<p>foo<span class="mention" data-mention="@John"></span></p>' );
  110. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  111. const expectedView = '<p>foo</p>';
  112. expect( editor.getData() ).to.equal( expectedView );
  113. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  114. } );
  115. } );
  116. describe( 'selection post-fixer', () => {
  117. beforeEach( () => {
  118. return createTestEditor()
  119. .then( newEditor => {
  120. editor = newEditor;
  121. model = editor.model;
  122. doc = model.document;
  123. } );
  124. } );
  125. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  126. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  127. model.change( writer => {
  128. const paragraph = doc.getRoot().getChild( 0 );
  129. writer.setSelection( paragraph, 9 );
  130. } );
  131. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  132. } );
  133. it( 'should allow to type after a mention', () => {
  134. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  135. model.change( writer => {
  136. const paragraph = doc.getRoot().getChild( 0 );
  137. writer.setSelection( paragraph, 9 );
  138. writer.insertText( ' ', paragraph, 9 );
  139. } );
  140. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  141. } );
  142. } );
  143. describe( 'removing partial mention post-fixer', () => {
  144. beforeEach( () => {
  145. return createTestEditor()
  146. .then( newEditor => {
  147. editor = newEditor;
  148. model = editor.model;
  149. doc = model.document;
  150. } );
  151. } );
  152. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  153. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  154. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  155. expect( textNode ).to.not.be.null;
  156. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  157. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', '@John' );
  158. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', '@John' );
  159. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  160. model.change( writer => {
  161. const paragraph = doc.getRoot().getChild( 0 );
  162. writer.setSelection( paragraph, 6 );
  163. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  164. } );
  165. expect( getModelData( model, { withoutSelection: true } ) )
  166. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  167. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  168. } );
  169. it( 'should remove mention on typing in mention node with selection attributes set', () => {
  170. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  171. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  172. expect( textNode ).to.not.be.null;
  173. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  174. model.change( writer => {
  175. const paragraph = doc.getRoot().getChild( 0 );
  176. writer.setSelection( paragraph, 6 );
  177. writer.setSelectionAttribute( 'bold', true );
  178. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  179. } );
  180. expect( getModelData( model, { withoutSelection: true } ) )
  181. .to.equal( '<paragraph>foo @J<$text bold="true">a</$text>ohn bar</paragraph>' );
  182. } );
  183. it( 'should remove mention on removing a text at the beginning of a mention', () => {
  184. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  185. const paragraph = doc.getRoot().getChild( 0 );
  186. model.change( writer => {
  187. writer.setSelection( paragraph, 4 );
  188. } );
  189. model.enqueueChange( () => {
  190. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  191. model.deleteContent( doc.selection );
  192. } );
  193. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  194. } );
  195. it( 'should remove mention on removing a text in the middle a mention', () => {
  196. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  197. const paragraph = doc.getRoot().getChild( 0 );
  198. model.change( writer => {
  199. writer.setSelection( paragraph, 6 );
  200. } );
  201. model.enqueueChange( () => {
  202. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  203. model.deleteContent( doc.selection );
  204. } );
  205. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  206. } );
  207. it( 'should remove mention on removing a text at the and of a mention', () => {
  208. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  209. const paragraph = doc.getRoot().getChild( 0 );
  210. model.change( writer => {
  211. writer.setSelection( paragraph, 9 );
  212. } );
  213. model.enqueueChange( () => {
  214. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  215. model.deleteContent( doc.selection );
  216. } );
  217. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  218. } );
  219. it( 'should not remove mention on removing a text just after a mention', () => {
  220. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  221. const paragraph = doc.getRoot().getChild( 0 );
  222. // Set selection before bar.
  223. model.change( writer => {
  224. writer.setSelection( paragraph, 10 );
  225. } );
  226. model.enqueueChange( () => {
  227. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  228. model.deleteContent( doc.selection );
  229. } );
  230. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span>bar</p>' );
  231. } );
  232. it( 'should remove mention on inserting text node inside a mention', () => {
  233. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  234. const paragraph = doc.getRoot().getChild( 0 );
  235. model.change( writer => {
  236. writer.insertText( 'baz', paragraph, 7 );
  237. } );
  238. expect( editor.getData() ).to.equal( '<p>foo @Jobazhn bar</p>' );
  239. } );
  240. it( 'should remove mention on inserting inline element inside a mention', () => {
  241. model.schema.register( 'inline', {
  242. allowWhere: '$text',
  243. isInline: true
  244. } );
  245. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  246. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  247. const paragraph = doc.getRoot().getChild( 0 );
  248. model.change( writer => {
  249. writer.insertElement( 'inline', paragraph, 7 );
  250. } );
  251. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  252. } );
  253. it( 'should remove mention when splitting paragraph with a mention', () => {
  254. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  255. const paragraph = doc.getRoot().getChild( 0 );
  256. model.change( writer => {
  257. writer.split( writer.createPositionAt( paragraph, 7 ) );
  258. } );
  259. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  260. } );
  261. it( 'should remove mention when deep splitting elements', () => {
  262. model.schema.register( 'blockQuote', {
  263. allowWhere: '$block',
  264. allowContentOf: '$root'
  265. } );
  266. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  267. editor.setData( '<blockquote><p>foo <span class="mention" data-mention="@John">@John</span> bar</p></blockquote>' );
  268. model.change( writer => {
  269. const paragraph = doc.getRoot().getChild( 0 ).getChild( 0 );
  270. writer.split( writer.createPositionAt( paragraph, 7 ), doc.getRoot() );
  271. } );
  272. expect( editor.getData() ).to.equal( '<blockquote><p>foo @Jo</p></blockquote><blockquote><p>hn bar</p></blockquote>' );
  273. } );
  274. } );
  275. describe( 'extend attribute on mention post-fixer', () => {
  276. beforeEach( () => {
  277. return createTestEditor()
  278. .then( newEditor => {
  279. editor = newEditor;
  280. model = editor.model;
  281. doc = model.document;
  282. } );
  283. } );
  284. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  285. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  286. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  287. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  288. const paragraph = doc.getRoot().getChild( 0 );
  289. model.change( writer => {
  290. const start = writer.createPositionAt( paragraph, 0 );
  291. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  292. writer.setSelection( range );
  293. writer.setAttribute( 'bold', true, range );
  294. } );
  295. expect( editor.getData() )
  296. .to.equal( '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  297. } );
  298. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  299. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  300. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  301. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  302. const paragraph = doc.getRoot().getChild( 0 );
  303. model.change( writer => {
  304. const start = writer.createPositionAt( paragraph, 6 );
  305. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  306. writer.setSelection( range );
  307. writer.setAttribute( 'bold', true, range );
  308. } );
  309. expect( editor.getData() )
  310. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>' );
  311. } );
  312. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  313. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  314. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  315. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  316. const paragraph = doc.getRoot().getChild( 0 );
  317. model.change( writer => {
  318. const start = writer.createPositionAt( paragraph, 6 );
  319. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  320. writer.setSelection( range );
  321. writer.setAttribute( 'bold', true, range );
  322. } );
  323. expect( editor.getData() )
  324. .to.equal( '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>' );
  325. } );
  326. it( 'should set attribute on whole mention when formatting part of two mentions', () => {
  327. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  328. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  329. editor.setData(
  330. '<p><span class="mention" data-mention="@John">@John</span><span class="mention" data-mention="@John">@John</span></p>'
  331. );
  332. const paragraph = doc.getRoot().getChild( 0 );
  333. model.change( writer => {
  334. const start = writer.createPositionAt( paragraph, 4 );
  335. const range = writer.createRange( start, start.getShiftedBy( 4 ) );
  336. writer.setSelection( range );
  337. writer.setAttribute( 'bold', true, range );
  338. } );
  339. expect( editor.getData() ).to.equal(
  340. '<p>' +
  341. '<strong>' +
  342. '<span class="mention" data-mention="@John">@John</span>' +
  343. '<span class="mention" data-mention="@John">@John</span>' +
  344. '</strong>' +
  345. '</p>'
  346. );
  347. } );
  348. it( 'should work with multiple ranges in change set', () => {
  349. model.schema.extend( '$text', { allowAttributes: [ 'foo' ] } );
  350. editor.conversion.attributeToElement( {
  351. model: {
  352. key: 'foo',
  353. values: [ 'a', 'b' ]
  354. },
  355. view: {
  356. a: {
  357. name: 'span',
  358. classes: 'mark-a'
  359. },
  360. b: {
  361. name: 'span',
  362. classes: 'mark-b'
  363. }
  364. },
  365. converterPriority: 'high'
  366. } );
  367. editor.setData(
  368. '<p>' +
  369. '<span class="mark-a">foo <span class="mention" data-mention="@John">@John</span></span>' +
  370. '<span class="mention" data-mention="@John">@John</span> bar' +
  371. '</p>'
  372. );
  373. model.change( writer => {
  374. const paragraph = doc.getRoot().getChild( 0 );
  375. const start = writer.createPositionAt( paragraph, 7 );
  376. const range = writer.createRange( start, start.getShiftedBy( 5 ) );
  377. writer.setAttribute( 'foo', 'b', range );
  378. } );
  379. expect( editor.getData() ).to.equal(
  380. '<p>' +
  381. '<span class="mark-a">foo </span>' +
  382. '<span class="mark-b">' +
  383. '<span class="mention" data-mention="@John">@John</span>' +
  384. '<span class="mention" data-mention="@John">@John</span>' +
  385. '</span> bar' +
  386. '</p>'
  387. );
  388. } );
  389. } );
  390. function createTestEditor( mentionConfig ) {
  391. return VirtualTestEditor
  392. .create( {
  393. plugins: [ Paragraph, MentionEditing ],
  394. mention: mentionConfig
  395. } );
  396. }
  397. } );