mentionediting.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 MentionEditing from '../src/mentionediting';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'MentionEditing', () => {
  11. testUtils.createSinonSandbox();
  12. it( 'should be named', () => {
  13. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  14. } );
  15. describe( 'init()', () => {
  16. let editor, model, doc;
  17. it( 'should be loaded', () => {
  18. return createTestEditor()
  19. .then( newEditor => {
  20. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  21. } );
  22. } );
  23. it( 'should set proper schema rules', () => {
  24. return createTestEditor()
  25. .then( newEditor => {
  26. model = newEditor.model;
  27. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  28. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  29. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  30. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  31. } );
  32. } );
  33. describe( 'conversion in the data pipeline', () => {
  34. beforeEach( () => {
  35. return createTestEditor()
  36. .then( newEditor => {
  37. editor = newEditor;
  38. model = editor.model;
  39. doc = model.document;
  40. } );
  41. } );
  42. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  43. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  44. expect( getModelData( model, { withoutSelection: true } ) )
  45. .to.equal( '<paragraph>foo <$text mention="John">John</$text> bar</paragraph>' );
  46. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  47. } );
  48. it( 'should remove mention on adding a text inside mention', () => {
  49. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  50. model.change( writer => {
  51. const paragraph = doc.getRoot().getChild( 0 );
  52. writer.setSelection( paragraph, 5 );
  53. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  54. } );
  55. expect( editor.getData() ).to.equal( '<p>foo Joahn bar</p>' );
  56. } );
  57. it( 'should remove mention on removing a text inside mention', () => {
  58. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  59. model.change( writer => {
  60. const paragraph = doc.getRoot().getChild( 0 );
  61. writer.setSelection( paragraph, 5 );
  62. writer.remove( writer.createRange( writer.createPositionAt( paragraph, 5 ), writer.createPositionAt( paragraph, 6 ) ) );
  63. } );
  64. expect( editor.getData() ).to.equal( '<p>foo Jhn bar</p>' );
  65. } );
  66. it( 'should remove mention on removing a text at the and of a mention', () => {
  67. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  68. const paragraph = doc.getRoot().getChild( 0 );
  69. // Set selection at the end of a John.
  70. model.change( writer => {
  71. writer.setSelection( paragraph, 8 );
  72. } );
  73. model.change( writer => {
  74. writer.remove( writer.createRange( writer.createPositionAt( paragraph, 7 ), writer.createPositionAt( paragraph, 8 ) ) );
  75. } );
  76. expect( editor.getData() ).to.equal( '<p>foo Joh bar</p>' );
  77. } );
  78. it( 'should work on insert text to an empty node', () => {
  79. editor.setData( '<p></p>' );
  80. model.change( writer => {
  81. const paragraph = doc.getRoot().getChild( 0 );
  82. writer.insertText( 'foo', paragraph, 0 );
  83. } );
  84. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  85. } );
  86. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  87. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span>bar</p>' );
  88. model.change( writer => {
  89. const paragraph = doc.getRoot().getChild( 0 );
  90. writer.setSelection( paragraph, 8 );
  91. } );
  92. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  93. } );
  94. it( 'should allow to type after a mention', () => {
  95. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span>bar</p>' );
  96. model.change( writer => {
  97. const paragraph = doc.getRoot().getChild( 0 );
  98. writer.setSelection( paragraph, 8 );
  99. writer.insertText( ' ', paragraph, 8 );
  100. } );
  101. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  102. } );
  103. } );
  104. } );
  105. function createTestEditor( mentionConfig ) {
  106. return VirtualTestEditor
  107. .create( {
  108. plugins: [ Paragraph, MentionEditing ],
  109. mention: mentionConfig
  110. } );
  111. }
  112. } );