editableelement.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. } );
  31. describe( 'isFocused', () => {
  32. let docMock, viewMain, viewHeader;
  33. beforeEach( () => {
  34. docMock = createDocumentMock();
  35. viewMain = new RootEditableElement( 'div' );
  36. viewMain.document = docMock;
  37. viewHeader = new RootEditableElement( 'h1' );
  38. viewHeader.document = docMock;
  39. viewHeader.rootName = 'header';
  40. } );
  41. it( 'should be observable', () => {
  42. const root = new RootEditableElement( 'div' );
  43. root.document = createDocumentMock();
  44. expect( root.isFocused ).to.be.false;
  45. const isFocusedSpy = sinon.spy();
  46. root.on( 'change:isFocused', isFocusedSpy );
  47. root.isFocused = true;
  48. expect( root.isFocused ).to.be.true;
  49. expect( isFocusedSpy.calledOnce ).to.be.true;
  50. } );
  51. it( 'should change isFocused on document render event', () => {
  52. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  53. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  54. docMock.selection.addRange( rangeMain );
  55. docMock.isFocused = true;
  56. expect( viewMain.isFocused ).to.be.true;
  57. expect( viewHeader.isFocused ).to.be.false;
  58. docMock.selection.setRanges( [ rangeHeader ] );
  59. docMock.fire( 'render' );
  60. expect( viewMain.isFocused ).to.be.false;
  61. expect( viewHeader.isFocused ).to.be.true;
  62. } );
  63. it( 'should change isFocused when document.isFocus changes', () => {
  64. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  65. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  66. docMock.selection.addRange( rangeMain );
  67. docMock.isFocused = true;
  68. expect( viewMain.isFocused ).to.be.true;
  69. expect( viewHeader.isFocused ).to.be.false;
  70. docMock.isFocused = false;
  71. expect( viewMain.isFocused ).to.be.false;
  72. expect( viewHeader.isFocused ).to.be.false;
  73. docMock.selection.setRanges( [ rangeHeader ] );
  74. expect( viewMain.isFocused ).to.be.false;
  75. expect( viewHeader.isFocused ).to.be.false;
  76. } );
  77. } );
  78. describe( 'isReadOnly', () => {
  79. it( 'should be observable', () => {
  80. const root = new RootEditableElement( 'div' );
  81. root.document = createDocumentMock();
  82. expect( root.isReadOnly ).to.be.false;
  83. const isReadOnlySpy = sinon.spy();
  84. root.on( 'change:isReadOnly', isReadOnlySpy );
  85. root.isReadOnly = true;
  86. expect( root.isReadOnly ).to.be.true;
  87. expect( isReadOnlySpy.calledOnce ).to.be.true;
  88. } );
  89. } );
  90. describe( 'getDocument', ()=> {
  91. it( 'should return document', () => {
  92. const docMock = createDocumentMock();
  93. const root = new RootEditableElement( 'div' );
  94. root.document = docMock;
  95. expect( root.document ).to.equal( docMock );
  96. } );
  97. } );
  98. } );