8
0

editableelement.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import RootEditableElement from '../../src/view/rooteditableelement';
  8. import Range from '../../src/view/range';
  9. describe( 'EditableElement', () => {
  10. describe( 'document', () => {
  11. let element, docMock;
  12. beforeEach( () => {
  13. element = new RootEditableElement( 'div' );
  14. docMock = createDocumentMock();
  15. } );
  16. it( 'should allow to set document', () => {
  17. element.document = docMock;
  18. expect( element.document ).to.equal( docMock );
  19. } );
  20. it( 'should return undefined if document is not set', () => {
  21. expect( element.document ).to.be.undefined;
  22. } );
  23. it( 'should throw if trying to set document again', () => {
  24. element.document = docMock;
  25. const newDoc = createDocumentMock();
  26. expect( () => {
  27. element.document = newDoc;
  28. } ).to.throw( CKEditorError, 'view-editableelement-document-already-set: View document is already set.' );
  29. } );
  30. it( 'should be cloned properly', () => {
  31. element.document = docMock;
  32. const newElement = element.clone();
  33. expect( newElement.document ).to.equal( docMock );
  34. } );
  35. } );
  36. describe( 'isFocused', () => {
  37. let docMock, viewMain, viewHeader;
  38. beforeEach( () => {
  39. docMock = createDocumentMock();
  40. viewMain = new RootEditableElement( 'div' );
  41. viewMain.document = docMock;
  42. viewHeader = new RootEditableElement( 'h1' );
  43. viewHeader.document = docMock;
  44. viewHeader.rootName = 'header';
  45. } );
  46. it( 'should be observable', () => {
  47. const root = new RootEditableElement( 'div' );
  48. root.document = createDocumentMock();
  49. expect( root.isFocused ).to.be.false;
  50. const isFocusedSpy = sinon.spy();
  51. root.on( 'change:isFocused', isFocusedSpy );
  52. root.isFocused = true;
  53. expect( root.isFocused ).to.be.true;
  54. expect( isFocusedSpy.calledOnce ).to.be.true;
  55. } );
  56. it( 'should change isFocused on document render event', () => {
  57. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  58. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  59. docMock.selection.addRange( rangeMain );
  60. docMock.isFocused = true;
  61. expect( viewMain.isFocused ).to.be.true;
  62. expect( viewHeader.isFocused ).to.be.false;
  63. docMock.selection.setRanges( [ rangeHeader ] );
  64. docMock.fire( 'render' );
  65. expect( viewMain.isFocused ).to.be.false;
  66. expect( viewHeader.isFocused ).to.be.true;
  67. } );
  68. it( 'should change isFocus before actual rendering', done => {
  69. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  70. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  71. docMock.render = sinon.spy();
  72. docMock.selection.addRange( rangeMain );
  73. docMock.isFocused = true;
  74. expect( viewMain.isFocused ).to.be.true;
  75. expect( viewHeader.isFocused ).to.be.false;
  76. docMock.selection.setRanges( [ rangeHeader ] );
  77. viewHeader.on( 'change:isFocused', ( evt, propertyName, value ) => {
  78. expect( value ).to.be.true;
  79. sinon.assert.notCalled( docMock.render );
  80. done();
  81. } );
  82. docMock.fire( 'render' );
  83. } );
  84. it( 'should change isFocused when document.isFocus changes', () => {
  85. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  86. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  87. docMock.selection.addRange( rangeMain );
  88. docMock.isFocused = true;
  89. expect( viewMain.isFocused ).to.be.true;
  90. expect( viewHeader.isFocused ).to.be.false;
  91. docMock.isFocused = false;
  92. expect( viewMain.isFocused ).to.be.false;
  93. expect( viewHeader.isFocused ).to.be.false;
  94. docMock.selection.setRanges( [ rangeHeader ] );
  95. expect( viewMain.isFocused ).to.be.false;
  96. expect( viewHeader.isFocused ).to.be.false;
  97. } );
  98. } );
  99. describe( 'isReadOnly', () => {
  100. it( 'should be observable', () => {
  101. const root = new RootEditableElement( 'div' );
  102. root.document = createDocumentMock();
  103. expect( root.isReadOnly ).to.be.false;
  104. const isReadOnlySpy = sinon.spy();
  105. root.on( 'change:isReadOnly', isReadOnlySpy );
  106. root.isReadOnly = true;
  107. expect( root.isReadOnly ).to.be.true;
  108. expect( isReadOnlySpy.calledOnce ).to.be.true;
  109. } );
  110. it( 'should be bound to the document#isReadOnly', () => {
  111. const root = new RootEditableElement( 'div' );
  112. root.document = createDocumentMock();
  113. root.document.isReadOnly = false;
  114. expect( root.isReadOnly ).to.false;
  115. root.document.isReadOnly = true;
  116. expect( root.isReadOnly ).to.true;
  117. } );
  118. } );
  119. describe( 'getDocument', () => {
  120. it( 'should return document', () => {
  121. const docMock = createDocumentMock();
  122. const root = new RootEditableElement( 'div' );
  123. root.document = docMock;
  124. expect( root.document ).to.equal( docMock );
  125. } );
  126. } );
  127. } );