8
0

editableelement.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 when selection changes', () => {
  57. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  58. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  59. docMock.selection._setTo( rangeMain );
  60. docMock.isFocused = true;
  61. expect( viewMain.isFocused ).to.be.true;
  62. expect( viewHeader.isFocused ).to.be.false;
  63. docMock.selection._setTo( [ rangeHeader ] );
  64. expect( viewMain.isFocused ).to.be.false;
  65. expect( viewHeader.isFocused ).to.be.true;
  66. } );
  67. it( 'should change isFocused when document.isFocus changes', () => {
  68. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  69. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  70. docMock.selection._setTo( rangeMain );
  71. docMock.isFocused = true;
  72. expect( viewMain.isFocused ).to.be.true;
  73. expect( viewHeader.isFocused ).to.be.false;
  74. docMock.isFocused = false;
  75. expect( viewMain.isFocused ).to.be.false;
  76. expect( viewHeader.isFocused ).to.be.false;
  77. docMock.selection._setTo( [ rangeHeader ] );
  78. expect( viewMain.isFocused ).to.be.false;
  79. expect( viewHeader.isFocused ).to.be.false;
  80. } );
  81. } );
  82. describe( 'isReadOnly', () => {
  83. it( 'should be observable', () => {
  84. const root = new RootEditableElement( 'div' );
  85. root._document = createDocumentMock();
  86. expect( root.isReadOnly ).to.be.false;
  87. const isReadOnlySpy = sinon.spy();
  88. root.on( 'change:isReadOnly', isReadOnlySpy );
  89. root.isReadOnly = true;
  90. expect( root.isReadOnly ).to.be.true;
  91. expect( isReadOnlySpy.calledOnce ).to.be.true;
  92. } );
  93. it( 'should be bound to the document#isReadOnly', () => {
  94. const root = new RootEditableElement( 'div' );
  95. root._document = createDocumentMock();
  96. root.document.isReadOnly = false;
  97. expect( root.isReadOnly ).to.false;
  98. root.document.isReadOnly = true;
  99. expect( root.isReadOnly ).to.true;
  100. } );
  101. } );
  102. describe( 'getDocument', () => {
  103. it( 'should return document', () => {
  104. const docMock = createDocumentMock();
  105. const root = new RootEditableElement( 'div' );
  106. root._document = docMock;
  107. expect( root.document ).to.equal( docMock );
  108. } );
  109. } );
  110. } );