editableelement.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  6. import EditableElement from '../../src/view/editableelement';
  7. import Range from '../../src/view/range';
  8. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. describe( 'EditableElement', () => {
  10. describe( 'is', () => {
  11. let el;
  12. before( () => {
  13. el = new EditableElement( 'div' );
  14. } );
  15. it( 'should return true for containerElement/editable/element, also with correct name and element name', () => {
  16. expect( el.is( 'containerElement' ) ).to.be.true;
  17. expect( el.is( 'view:containerElement' ) ).to.be.true;
  18. expect( el.is( 'containerElement', 'div' ) ).to.be.true;
  19. expect( el.is( 'view:containerElement', 'div' ) ).to.be.true;
  20. expect( el.is( 'editableElement' ) ).to.be.true;
  21. expect( el.is( 'view:editableElement' ) ).to.be.true;
  22. expect( el.is( 'editableElement', 'div' ) ).to.be.true;
  23. expect( el.is( 'view:editableElement', 'div' ) ).to.be.true;
  24. expect( el.is( 'element' ) ).to.be.true;
  25. expect( el.is( 'view:element' ) ).to.be.true;
  26. expect( el.is( 'element', 'div' ) ).to.be.true;
  27. expect( el.is( 'view:element', 'div' ) ).to.be.true;
  28. expect( el.is( 'div' ) ).to.be.true;
  29. expect( el.is( 'view:div' ) ).to.be.true;
  30. } );
  31. it( 'should return false for other accept values', () => {
  32. expect( el.is( 'rootElement', 'p' ) ).to.be.false;
  33. expect( el.is( 'view:rootElement', 'p' ) ).to.be.false;
  34. expect( el.is( 'containerElement', 'p' ) ).to.be.false;
  35. expect( el.is( 'view:containerElement', 'p' ) ).to.be.false;
  36. expect( el.is( 'element', 'p' ) ).to.be.false;
  37. expect( el.is( 'view:element', 'p' ) ).to.be.false;
  38. expect( el.is( 'p' ) ).to.be.false;
  39. expect( el.is( 'view:p' ) ).to.be.false;
  40. expect( el.is( 'text' ) ).to.be.false;
  41. expect( el.is( 'textProxy' ) ).to.be.false;
  42. expect( el.is( 'attributeElement' ) ).to.be.false;
  43. expect( el.is( 'uiElement' ) ).to.be.false;
  44. expect( el.is( 'emptyElement' ) ).to.be.false;
  45. expect( el.is( 'documentFragment' ) ).to.be.false;
  46. } );
  47. } );
  48. describe( 'document', () => {
  49. let element, docMock;
  50. beforeEach( () => {
  51. element = new EditableElement( 'div' );
  52. docMock = createDocumentMock();
  53. } );
  54. it( 'should allow to set document', () => {
  55. element._document = docMock;
  56. expect( element.document ).to.equal( docMock );
  57. } );
  58. it( 'should return undefined if document is not set', () => {
  59. expect( element.document ).to.be.undefined;
  60. } );
  61. it( 'should throw if trying to set document again', () => {
  62. element._document = docMock;
  63. const newDoc = createDocumentMock();
  64. expectToThrowCKEditorError( () => {
  65. element._document = newDoc;
  66. }, 'view-editableelement-document-already-set: View document is already set.', docMock );
  67. } );
  68. it( 'should be cloned properly', () => {
  69. element._document = docMock;
  70. const newElement = element._clone();
  71. expect( newElement.document ).to.equal( docMock );
  72. } );
  73. } );
  74. describe( 'isFocused', () => {
  75. let docMock, viewMain, viewHeader;
  76. beforeEach( () => {
  77. docMock = createDocumentMock();
  78. viewMain = new EditableElement( 'div' );
  79. viewMain._document = docMock;
  80. viewHeader = new EditableElement( 'h1' );
  81. viewHeader._document = docMock;
  82. viewHeader.rootName = 'header';
  83. } );
  84. it( 'should be observable', () => {
  85. const root = new EditableElement( 'div' );
  86. root._document = createDocumentMock();
  87. expect( root.isFocused ).to.be.false;
  88. const isFocusedSpy = sinon.spy();
  89. root.on( 'change:isFocused', isFocusedSpy );
  90. root.isFocused = true;
  91. expect( root.isFocused ).to.be.true;
  92. expect( isFocusedSpy.calledOnce ).to.be.true;
  93. } );
  94. it( 'should change isFocused when selection changes', () => {
  95. const rangeMain = Range._createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  96. const rangeHeader = Range._createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  97. docMock.selection._setTo( rangeMain );
  98. docMock.isFocused = true;
  99. expect( viewMain.isFocused ).to.be.true;
  100. expect( viewHeader.isFocused ).to.be.false;
  101. docMock.selection._setTo( [ rangeHeader ] );
  102. expect( viewMain.isFocused ).to.be.false;
  103. expect( viewHeader.isFocused ).to.be.true;
  104. } );
  105. it( 'should change isFocused when document.isFocus changes', () => {
  106. const rangeMain = Range._createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  107. const rangeHeader = Range._createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  108. docMock.selection._setTo( rangeMain );
  109. docMock.isFocused = true;
  110. expect( viewMain.isFocused ).to.be.true;
  111. expect( viewHeader.isFocused ).to.be.false;
  112. docMock.isFocused = false;
  113. expect( viewMain.isFocused ).to.be.false;
  114. expect( viewHeader.isFocused ).to.be.false;
  115. docMock.selection._setTo( [ rangeHeader ] );
  116. expect( viewMain.isFocused ).to.be.false;
  117. expect( viewHeader.isFocused ).to.be.false;
  118. } );
  119. } );
  120. describe( 'isReadOnly', () => {
  121. it( 'should be observable', () => {
  122. const root = new EditableElement( 'div' );
  123. root._document = createDocumentMock();
  124. expect( root.isReadOnly ).to.be.false;
  125. const isReadOnlySpy = sinon.spy();
  126. root.on( 'change:isReadOnly', isReadOnlySpy );
  127. root.isReadOnly = true;
  128. expect( root.isReadOnly ).to.be.true;
  129. expect( isReadOnlySpy.calledOnce ).to.be.true;
  130. } );
  131. it( 'should be bound to the document#isReadOnly', () => {
  132. const root = new EditableElement( 'div' );
  133. root._document = createDocumentMock();
  134. root.document.isReadOnly = false;
  135. expect( root.isReadOnly ).to.false;
  136. root.document.isReadOnly = true;
  137. expect( root.isReadOnly ).to.true;
  138. } );
  139. } );
  140. describe( 'getDocument', () => {
  141. it( 'should return document', () => {
  142. const docMock = createDocumentMock();
  143. const root = new EditableElement( 'div' );
  144. root._document = docMock;
  145. expect( root.document ).to.equal( docMock );
  146. } );
  147. } );
  148. } );