editableelement.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import createDocumentMock from 'ckeditor5-engine/tests/view/_utils/createdocumentmock';
  6. import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
  7. import RootEditableElement from 'ckeditor5-engine/src/view/rooteditableelement';
  8. import Range from 'ckeditor5-engine/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 isFocused when document.isFocus changes', () => {
  69. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  70. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  71. docMock.selection.addRange( rangeMain );
  72. docMock.isFocused = true;
  73. expect( viewMain.isFocused ).to.be.true;
  74. expect( viewHeader.isFocused ).to.be.false;
  75. docMock.isFocused = false;
  76. expect( viewMain.isFocused ).to.be.false;
  77. expect( viewHeader.isFocused ).to.be.false;
  78. docMock.selection.setRanges( [ rangeHeader ] );
  79. expect( viewMain.isFocused ).to.be.false;
  80. expect( viewHeader.isFocused ).to.be.false;
  81. } );
  82. } );
  83. describe( 'isReadOnly', () => {
  84. it( 'should be observable', () => {
  85. const root = new RootEditableElement( 'div' );
  86. root.document = createDocumentMock();
  87. expect( root.isReadOnly ).to.be.false;
  88. const isReadOnlySpy = sinon.spy();
  89. root.on( 'change:isReadOnly', isReadOnlySpy );
  90. root.isReadOnly = true;
  91. expect( root.isReadOnly ).to.be.true;
  92. expect( isReadOnlySpy.calledOnce ).to.be.true;
  93. } );
  94. } );
  95. describe( 'getDocument', ()=> {
  96. it( 'should return document', () => {
  97. const docMock = createDocumentMock();
  98. const root = new RootEditableElement( 'div' );
  99. root.document = docMock;
  100. expect( root.document ).to.equal( docMock );
  101. } );
  102. } );
  103. } );