mentionediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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' );
  61. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  62. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  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' )._id;
  82. const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._id;
  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' );
  92. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  93. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  94. }
  95. } );
  96. it( 'should not convert partial mentions', () => {
  97. editor.setData( '<p><span class="mention" data-mention="John">@Jo</span></p>' );
  98. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>@Jo</paragraph>' );
  99. const expectedView = '<p>@Jo</p>';
  100. expect( editor.getData() ).to.equal( expectedView );
  101. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  102. } );
  103. it( 'should not convert empty mentions', () => {
  104. editor.setData( '<p>foo<span class="mention" data-mention="John"></span></p>' );
  105. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  106. const expectedView = '<p>foo</p>';
  107. expect( editor.getData() ).to.equal( expectedView );
  108. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  109. } );
  110. } );
  111. describe( 'selection post-fixer', () => {
  112. beforeEach( () => {
  113. return createTestEditor()
  114. .then( newEditor => {
  115. editor = newEditor;
  116. model = editor.model;
  117. doc = model.document;
  118. } );
  119. } );
  120. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  121. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  122. model.change( writer => {
  123. const paragraph = doc.getRoot().getChild( 0 );
  124. writer.setSelection( paragraph, 9 );
  125. } );
  126. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  127. } );
  128. it( 'should allow to type after a mention', () => {
  129. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  130. model.change( writer => {
  131. const paragraph = doc.getRoot().getChild( 0 );
  132. writer.setSelection( paragraph, 9 );
  133. writer.insertText( ' ', paragraph, 9 );
  134. } );
  135. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  136. } );
  137. } );
  138. describe( 'removing partial mention post-fixer', () => {
  139. beforeEach( () => {
  140. return createTestEditor()
  141. .then( newEditor => {
  142. editor = newEditor;
  143. model = editor.model;
  144. doc = model.document;
  145. } );
  146. } );
  147. it( 'should remove mention on adding a text inside mention (in the middle)', () => {
  148. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  149. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  150. expect( textNode ).to.not.be.null;
  151. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  152. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  153. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', '@' );
  154. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
  155. model.change( writer => {
  156. const paragraph = doc.getRoot().getChild( 0 );
  157. writer.setSelection( paragraph, 6 );
  158. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  159. } );
  160. expect( getModelData( model, { withoutSelection: true } ) )
  161. .to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
  162. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  163. } );
  164. it( 'should remove mention on typing in mention node with selection attributes set', () => {
  165. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  166. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  167. expect( textNode ).to.not.be.null;
  168. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  169. model.change( writer => {
  170. const paragraph = doc.getRoot().getChild( 0 );
  171. writer.setSelection( paragraph, 6 );
  172. writer.setSelectionAttribute( 'bold', true );
  173. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  174. } );
  175. expect( getModelData( model, { withoutSelection: true } ) )
  176. .to.equal( '<paragraph>foo @J<$text bold="true">a</$text>ohn bar</paragraph>' );
  177. } );
  178. it( 'should remove mention on removing a text at the beginning of a mention', () => {
  179. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  180. const paragraph = doc.getRoot().getChild( 0 );
  181. model.change( writer => {
  182. writer.setSelection( paragraph, 4 );
  183. } );
  184. model.enqueueChange( () => {
  185. model.modifySelection( doc.selection, { direction: 'forward', unit: 'codepoint' } );
  186. model.deleteContent( doc.selection );
  187. } );
  188. expect( editor.getData() ).to.equal( '<p>foo John bar</p>' );
  189. } );
  190. it( 'should remove mention on removing a text in the middle a mention', () => {
  191. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  192. const paragraph = doc.getRoot().getChild( 0 );
  193. model.change( writer => {
  194. writer.setSelection( paragraph, 6 );
  195. } );
  196. model.enqueueChange( () => {
  197. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  198. model.deleteContent( doc.selection );
  199. } );
  200. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  201. } );
  202. it( 'should remove mention on removing a text at the and of a mention', () => {
  203. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  204. const paragraph = doc.getRoot().getChild( 0 );
  205. model.change( writer => {
  206. writer.setSelection( paragraph, 9 );
  207. } );
  208. model.enqueueChange( () => {
  209. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  210. model.deleteContent( doc.selection );
  211. } );
  212. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  213. } );
  214. it( 'should not remove mention on removing a text just after a mention', () => {
  215. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  216. const paragraph = doc.getRoot().getChild( 0 );
  217. // Set selection before bar.
  218. model.change( writer => {
  219. writer.setSelection( paragraph, 10 );
  220. } );
  221. model.enqueueChange( () => {
  222. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  223. model.deleteContent( doc.selection );
  224. } );
  225. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  226. } );
  227. it( 'should remove mention on inserting text node inside a mention', () => {
  228. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  229. const paragraph = doc.getRoot().getChild( 0 );
  230. model.change( writer => {
  231. writer.insertText( 'baz', paragraph, 7 );
  232. } );
  233. expect( editor.getData() ).to.equal( '<p>foo @Jobazhn bar</p>' );
  234. } );
  235. it( 'should remove mention on inserting inline element inside a mention', () => {
  236. model.schema.register( 'inline', {
  237. allowWhere: '$text',
  238. isInline: true
  239. } );
  240. editor.conversion.elementToElement( { model: 'inline', view: 'br' } );
  241. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  242. const paragraph = doc.getRoot().getChild( 0 );
  243. model.change( writer => {
  244. writer.insertElement( 'inline', paragraph, 7 );
  245. } );
  246. expect( editor.getData() ).to.equal( '<p>foo @Jo<br>hn bar</p>' );
  247. } );
  248. it( 'should remove mention when splitting paragraph with 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.split( writer.createPositionAt( paragraph, 7 ) );
  253. } );
  254. expect( editor.getData() ).to.equal( '<p>foo @Jo</p><p>hn bar</p>' );
  255. } );
  256. it( 'should remove mention when deep splitting elements', () => {
  257. model.schema.register( 'blockQuote', {
  258. allowWhere: '$block',
  259. allowContentOf: '$root'
  260. } );
  261. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  262. editor.setData( '<blockquote><p>foo <span class="mention" data-mention="John">@John</span> bar</p></blockquote>' );
  263. model.change( writer => {
  264. const paragraph = doc.getRoot().getChild( 0 ).getChild( 0 );
  265. writer.split( writer.createPositionAt( paragraph, 7 ), doc.getRoot() );
  266. } );
  267. expect( editor.getData() ).to.equal( '<blockquote><p>foo @Jo</p></blockquote><blockquote><p>hn bar</p></blockquote>' );
  268. } );
  269. } );
  270. describe( 'extend attribute on mention post-fixer', () => {
  271. beforeEach( () => {
  272. return createTestEditor()
  273. .then( newEditor => {
  274. editor = newEditor;
  275. model = editor.model;
  276. doc = model.document;
  277. } );
  278. } );
  279. it( 'should set attribute on whole mention when formatting part of a mention (beginning formatted)', () => {
  280. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  281. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  282. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  283. const paragraph = doc.getRoot().getChild( 0 );
  284. model.change( writer => {
  285. const start = writer.createPositionAt( paragraph, 0 );
  286. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  287. writer.setSelection( range );
  288. writer.setAttribute( 'bold', true, range );
  289. } );
  290. expect( editor.getData() )
  291. .to.equal( '<p><strong>foo <span class="mention" data-mention="John">@John</span></strong> bar</p>' );
  292. } );
  293. it( 'should set attribute on whole mention when formatting part of a mention (end formatted)', () => {
  294. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  295. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  296. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  297. const paragraph = doc.getRoot().getChild( 0 );
  298. model.change( writer => {
  299. const start = writer.createPositionAt( paragraph, 6 );
  300. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  301. writer.setSelection( range );
  302. writer.setAttribute( 'bold', true, range );
  303. } );
  304. expect( editor.getData() )
  305. .to.equal( '<p>foo <strong><span class="mention" data-mention="John">@John</span> ba</strong>r</p>' );
  306. } );
  307. it( 'should set attribute on whole mention when formatting part of a mention (middle of mention formatted)', () => {
  308. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  309. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  310. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  311. const paragraph = doc.getRoot().getChild( 0 );
  312. model.change( writer => {
  313. const start = writer.createPositionAt( paragraph, 6 );
  314. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  315. writer.setSelection( range );
  316. writer.setAttribute( 'bold', true, range );
  317. } );
  318. expect( editor.getData() )
  319. .to.equal( '<p>foo <strong><span class="mention" data-mention="John">@John</span></strong> bar</p>' );
  320. } );
  321. it( 'should set attribute on whole mention when formatting part of two mentions', () => {
  322. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  323. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  324. editor.setData(
  325. '<p><span class="mention" data-mention="John">@John</span><span class="mention" data-mention="John">@John</span></p>'
  326. );
  327. const paragraph = doc.getRoot().getChild( 0 );
  328. model.change( writer => {
  329. const start = writer.createPositionAt( paragraph, 4 );
  330. const range = writer.createRange( start, start.getShiftedBy( 4 ) );
  331. writer.setSelection( range );
  332. writer.setAttribute( 'bold', true, range );
  333. } );
  334. expect( editor.getData() ).to.equal(
  335. '<p>' +
  336. '<strong>' +
  337. '<span class="mention" data-mention="John">@John</span>' +
  338. '<span class="mention" data-mention="John">@John</span>' +
  339. '</strong>' +
  340. '</p>'
  341. );
  342. } );
  343. it( 'should work with multiple ranges in change set', () => {
  344. model.schema.extend( '$text', { allowAttributes: [ 'foo' ] } );
  345. editor.conversion.attributeToElement( {
  346. model: {
  347. key: 'foo',
  348. values: [ 'a', 'b' ]
  349. },
  350. view: {
  351. a: {
  352. name: 'span',
  353. classes: 'mark-a'
  354. },
  355. b: {
  356. name: 'span',
  357. classes: 'mark-b'
  358. }
  359. },
  360. converterPriority: 'high'
  361. } );
  362. editor.setData(
  363. '<p>' +
  364. '<span class="mark-a">foo <span class="mention" data-mention="John">@John</span></span>' +
  365. '<span class="mention" data-mention="John">@John</span> bar' +
  366. '</p>'
  367. );
  368. model.change( writer => {
  369. const paragraph = doc.getRoot().getChild( 0 );
  370. const start = writer.createPositionAt( paragraph, 7 );
  371. const range = writer.createRange( start, start.getShiftedBy( 5 ) );
  372. writer.setAttribute( 'foo', 'b', range );
  373. } );
  374. expect( editor.getData() ).to.equal(
  375. '<p>' +
  376. '<span class="mark-a">foo </span>' +
  377. '<span class="mark-b">' +
  378. '<span class="mention" data-mention="John">@John</span>' +
  379. '<span class="mention" data-mention="John">@John</span>' +
  380. '</span> bar' +
  381. '</p>'
  382. );
  383. } );
  384. } );
  385. function createTestEditor( mentionConfig ) {
  386. return VirtualTestEditor
  387. .create( {
  388. plugins: [ Paragraph, MentionEditing ],
  389. mention: mentionConfig
  390. } );
  391. }
  392. } );