editableelement.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 Document from '../../src/view/document';
  9. import { StylesProcessor } from '../../src/view/stylesmap';
  10. describe( 'EditableElement', () => {
  11. let stylesProcessor;
  12. before( () => {
  13. stylesProcessor = new StylesProcessor();
  14. } );
  15. describe( 'is', () => {
  16. let el;
  17. before( () => {
  18. el = new EditableElement( new Document( stylesProcessor ), 'div' );
  19. } );
  20. it( 'should return true for containerElement/editable/element, also with correct name and element name', () => {
  21. expect( el.is( 'containerElement' ) ).to.be.true;
  22. expect( el.is( 'view:containerElement' ) ).to.be.true;
  23. expect( el.is( 'containerElement', 'div' ) ).to.be.true;
  24. expect( el.is( 'view:containerElement', 'div' ) ).to.be.true;
  25. expect( el.is( 'editableElement' ) ).to.be.true;
  26. expect( el.is( 'view:editableElement' ) ).to.be.true;
  27. expect( el.is( 'editableElement', 'div' ) ).to.be.true;
  28. expect( el.is( 'view:editableElement', 'div' ) ).to.be.true;
  29. expect( el.is( 'element' ) ).to.be.true;
  30. expect( el.is( 'view:element' ) ).to.be.true;
  31. expect( el.is( 'element', 'div' ) ).to.be.true;
  32. expect( el.is( 'view:element', 'div' ) ).to.be.true;
  33. expect( el.is( 'div' ) ).to.be.true;
  34. expect( el.is( 'view:div' ) ).to.be.true;
  35. } );
  36. it( 'should return false for other accept values', () => {
  37. expect( el.is( 'rootElement', 'p' ) ).to.be.false;
  38. expect( el.is( 'view:rootElement', 'p' ) ).to.be.false;
  39. expect( el.is( 'containerElement', 'p' ) ).to.be.false;
  40. expect( el.is( 'view:containerElement', 'p' ) ).to.be.false;
  41. expect( el.is( 'element', 'p' ) ).to.be.false;
  42. expect( el.is( 'view:element', 'p' ) ).to.be.false;
  43. expect( el.is( 'p' ) ).to.be.false;
  44. expect( el.is( 'view:p' ) ).to.be.false;
  45. expect( el.is( 'text' ) ).to.be.false;
  46. expect( el.is( 'textProxy' ) ).to.be.false;
  47. expect( el.is( 'attributeElement' ) ).to.be.false;
  48. expect( el.is( 'uiElement' ) ).to.be.false;
  49. expect( el.is( 'emptyElement' ) ).to.be.false;
  50. expect( el.is( 'documentFragment' ) ).to.be.false;
  51. } );
  52. } );
  53. describe( 'isFocused', () => {
  54. let docMock, viewMain, viewHeader;
  55. beforeEach( () => {
  56. docMock = createDocumentMock();
  57. viewMain = new EditableElement( docMock, 'div' );
  58. viewHeader = new EditableElement( docMock, 'h1' );
  59. viewHeader.rootName = 'header';
  60. } );
  61. it( 'should be observable', () => {
  62. const root = new EditableElement( docMock, 'div' );
  63. expect( root.isFocused ).to.be.false;
  64. const isFocusedSpy = sinon.spy();
  65. root.on( 'change:isFocused', isFocusedSpy );
  66. root.isFocused = true;
  67. expect( root.isFocused ).to.be.true;
  68. expect( isFocusedSpy.calledOnce ).to.be.true;
  69. } );
  70. it( 'should change isFocused when selection changes', () => {
  71. const rangeMain = Range._createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  72. const rangeHeader = Range._createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  73. docMock.selection._setTo( rangeMain );
  74. docMock.isFocused = true;
  75. expect( viewMain.isFocused ).to.be.true;
  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.true;
  80. } );
  81. it( 'should change isFocused when document.isFocus changes', () => {
  82. const rangeMain = Range._createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  83. const rangeHeader = Range._createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  84. docMock.selection._setTo( rangeMain );
  85. docMock.isFocused = true;
  86. expect( viewMain.isFocused ).to.be.true;
  87. expect( viewHeader.isFocused ).to.be.false;
  88. docMock.isFocused = false;
  89. expect( viewMain.isFocused ).to.be.false;
  90. expect( viewHeader.isFocused ).to.be.false;
  91. docMock.selection._setTo( [ rangeHeader ] );
  92. expect( viewMain.isFocused ).to.be.false;
  93. expect( viewHeader.isFocused ).to.be.false;
  94. } );
  95. } );
  96. describe( 'isReadOnly', () => {
  97. let docMock;
  98. beforeEach( () => {
  99. docMock = createDocumentMock();
  100. } );
  101. it( 'should be observable', () => {
  102. const root = new EditableElement( docMock, 'div' );
  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 EditableElement( docMock, 'div' );
  112. root.document.isReadOnly = false;
  113. expect( root.isReadOnly ).to.false;
  114. root.document.isReadOnly = true;
  115. expect( root.isReadOnly ).to.true;
  116. } );
  117. } );
  118. describe( 'document', () => {
  119. let element, docMock;
  120. beforeEach( () => {
  121. docMock = createDocumentMock();
  122. element = new EditableElement( docMock, 'div' );
  123. } );
  124. it( 'should be cloned properly', () => {
  125. const newElement = element._clone();
  126. expect( newElement.document ).to.equal( docMock );
  127. } );
  128. } );
  129. } );