8
0

mentioncommand.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import MentionCommand from '../src/mentioncommand';
  8. describe( 'MentionCommand', () => {
  9. let editor, command, model, doc, selection;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. selection = doc.selection;
  18. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  20. model.schema.extend( '$text', { allowAttributes: [ 'mention' ] } );
  21. command = new MentionCommand( editor );
  22. } );
  23. } );
  24. afterEach( () => {
  25. command.destroy();
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  30. setData( model, '<paragraph>f[]oo</paragraph>' );
  31. expect( command.isEnabled ).to.be.true;
  32. } );
  33. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  34. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  35. // Allow 'bold' on p>$text.
  36. if ( ctx.endsWith( 'x $text' ) && attributeName == 'mention' ) {
  37. return false;
  38. }
  39. } );
  40. setData( model, '<x>fo[]o</x>' );
  41. expect( command.isEnabled ).to.be.false;
  42. } );
  43. } );
  44. describe( 'execute()', () => {
  45. it( 'inserts mention attribute for given range', () => {
  46. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  47. command.execute( {
  48. mention: { name: 'John' },
  49. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  50. } );
  51. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@', 'John' );
  52. } );
  53. it( 'inserts mention object if mention was passed as string', () => {
  54. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  55. command.execute( {
  56. mention: 'John',
  57. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  58. } );
  59. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@', 'John' );
  60. } );
  61. it( 'inserts mention attribute with passed marker for given range', () => {
  62. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  63. const end = model.createPositionAt( selection.focus );
  64. const start = end.getShiftedBy( -3 );
  65. command.execute( {
  66. mention: { name: 'John' },
  67. range: model.createRange( start, end ),
  68. marker: '#'
  69. } );
  70. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '#', 'John' );
  71. } );
  72. it( 'inserts mention attribute at current selection if no range was passed', () => {
  73. setData( model, '<paragraph>foo []bar</paragraph>' );
  74. command.execute( {
  75. mention: { name: 'John' }
  76. } );
  77. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@', 'John' );
  78. } );
  79. it( 'should set also other styles in inserted text', () => {
  80. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  81. setData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
  82. command.execute( {
  83. mention: { name: 'John' },
  84. range: model.createRange( selection.focus.getShiftedBy( -5 ), selection.focus )
  85. } );
  86. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  87. assertMention( textNode, '@', 'John' );
  88. expect( textNode.hasAttribute( 'bold' ) ).to.be.true;
  89. } );
  90. } );
  91. function assertMention( textNode, marker, name ) {
  92. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  93. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
  94. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_marker', marker );
  95. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', name );
  96. }
  97. } );