linkactionsview.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import LinkActionsView from '../../src/ui/linkactionsview';
  6. import View from '@ckeditor/ckeditor5-ui/src/view';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  10. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  11. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'LinkActionsView', () => {
  14. let view;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. view = new LinkActionsView( { t: val => val } );
  18. view.render();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create element from template', () => {
  22. expect( view.element.classList.contains( 'ck' ) ).to.true;
  23. expect( view.element.classList.contains( 'ck-link-actions' ) ).to.true;
  24. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  25. } );
  26. it( 'should create child views', () => {
  27. expect( view.previewButtonView ).to.be.instanceOf( View );
  28. expect( view.unlinkButtonView ).to.be.instanceOf( View );
  29. expect( view.editButtonView ).to.be.instanceOf( View );
  30. expect( view._unboundChildren.get( 0 ) ).to.equal( view.previewButtonView );
  31. expect( view._unboundChildren.get( 1 ) ).to.equal( view.editButtonView );
  32. expect( view._unboundChildren.get( 2 ) ).to.equal( view.unlinkButtonView );
  33. } );
  34. it( 'should create #focusTracker instance', () => {
  35. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  36. } );
  37. it( 'should create #keystrokes instance', () => {
  38. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  39. } );
  40. it( 'should create #_focusCycler instance', () => {
  41. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  42. } );
  43. it( 'should create #_focusables view collection', () => {
  44. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  45. } );
  46. it( 'should fire `edit` event on editButtonView#execute', () => {
  47. const spy = sinon.spy();
  48. view.on( 'edit', spy );
  49. view.editButtonView.fire( 'execute' );
  50. expect( spy.calledOnce ).to.true;
  51. } );
  52. it( 'should fire `unlink` event on unlinkButtonView#execute', () => {
  53. const spy = sinon.spy();
  54. view.on( 'unlink', spy );
  55. view.unlinkButtonView.fire( 'execute' );
  56. expect( spy.calledOnce ).to.true;
  57. } );
  58. describe( 'preview button view', () => {
  59. it( 'is an anchor', () => {
  60. expect( view.previewButtonView.element.tagName.toLowerCase() ).to.equal( 'a' );
  61. } );
  62. it( 'has a CSS class', () => {
  63. expect( view.previewButtonView.element.classList.contains( 'ck-link-actions__preview' ) ).to.be.true;
  64. } );
  65. it( 'has a "target" attribute', () => {
  66. expect( view.previewButtonView.element.getAttribute( 'target' ) ).to.equal( '_blank' );
  67. } );
  68. it( 'has a "rel" attribute', () => {
  69. expect( view.previewButtonView.element.getAttribute( 'rel' ) ).to.equal( 'noopener noreferrer' );
  70. } );
  71. describe( '<a> bindings', () => {
  72. it( 'binds href DOM attribute to view#href', () => {
  73. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.be.null;
  74. view.href = 'foo';
  75. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( 'foo' );
  76. } );
  77. it( 'does not render unsafe view#href', () => {
  78. view.href = 'javascript:alert(1)';
  79. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( '#' );
  80. } );
  81. it( 'binds #isEnabled to view#href', () => {
  82. expect( view.previewButtonView.isEnabled ).to.be.false;
  83. view.href = 'foo';
  84. expect( view.previewButtonView.isEnabled ).to.be.true;
  85. } );
  86. } );
  87. } );
  88. describe( 'template', () => {
  89. it( 'has child views', () => {
  90. expect( view.template.children[ 0 ] ).to.equal( view.previewButtonView );
  91. expect( view.template.children[ 1 ] ).to.equal( view.editButtonView );
  92. expect( view.template.children[ 2 ] ).to.equal( view.unlinkButtonView );
  93. } );
  94. } );
  95. } );
  96. describe( 'render()', () => {
  97. it( 'should register child views in #_focusables', () => {
  98. expect( view._focusables.map( f => f ) ).to.have.members( [
  99. view.previewButtonView,
  100. view.editButtonView,
  101. view.unlinkButtonView
  102. ] );
  103. } );
  104. it( 'should register child views\' #element in #focusTracker', () => {
  105. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  106. view = new LinkActionsView( { t: () => {} } );
  107. view.render();
  108. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.previewButtonView.element );
  109. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.editButtonView.element );
  110. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.unlinkButtonView.element );
  111. } );
  112. it( 'starts listening for #keystrokes coming from #element', () => {
  113. view = new LinkActionsView( { t: () => {} } );
  114. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  115. view.render();
  116. sinon.assert.calledOnce( spy );
  117. sinon.assert.calledWithExactly( spy, view.element );
  118. } );
  119. describe( 'activates keyboard navigation for the toolbar', () => {
  120. it( 'so "tab" focuses the next focusable item', () => {
  121. const keyEvtData = {
  122. keyCode: keyCodes.tab,
  123. preventDefault: sinon.spy(),
  124. stopPropagation: sinon.spy()
  125. };
  126. // Mock the preview button is focused.
  127. view.focusTracker.isFocused = true;
  128. view.focusTracker.focusedElement = view.previewButtonView.element;
  129. const spy = sinon.spy( view.editButtonView, 'focus' );
  130. view.keystrokes.press( keyEvtData );
  131. sinon.assert.calledOnce( keyEvtData.preventDefault );
  132. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  133. sinon.assert.calledOnce( spy );
  134. } );
  135. it( 'so "shift + tab" focuses the previous focusable item', () => {
  136. const keyEvtData = {
  137. keyCode: keyCodes.tab,
  138. shiftKey: true,
  139. preventDefault: sinon.spy(),
  140. stopPropagation: sinon.spy()
  141. };
  142. // Mock the edit button is focused.
  143. view.focusTracker.isFocused = true;
  144. view.focusTracker.focusedElement = view.editButtonView.element;
  145. const spy = sinon.spy( view.previewButtonView, 'focus' );
  146. view.keystrokes.press( keyEvtData );
  147. sinon.assert.calledOnce( keyEvtData.preventDefault );
  148. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. } );
  152. } );
  153. describe( 'focus()', () => {
  154. it( 'focuses the #previewButtonView', () => {
  155. const spy = sinon.spy( view.previewButtonView, 'focus' );
  156. view.focus();
  157. sinon.assert.calledOnce( spy );
  158. } );
  159. } );
  160. } );