editableelement.js 5.1 KB

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