editableelement.js 5.1 KB

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