rooteditableelement.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ContainerElement from '../../src/view/containerelement';
  6. import EditableElement from '../../src/view/editableelement';
  7. import RootEditableElement from '../../src/view/rooteditableelement';
  8. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  9. describe( 'RootEditableElement', () => {
  10. let document;
  11. beforeEach( () => {
  12. document = createDocumentMock();
  13. } );
  14. describe( 'constructor()', () => {
  15. it( 'should create an element with default root name', () => {
  16. const root = new RootEditableElement( document, 'div' );
  17. expect( root ).to.be.instanceof( EditableElement );
  18. expect( root ).to.be.instanceof( ContainerElement );
  19. expect( root.rootName ).to.equal( 'main' );
  20. expect( root.name ).to.equal( 'div' );
  21. expect( root.isFocused ).to.be.false;
  22. expect( root.isReadOnly ).to.be.false;
  23. } );
  24. it( 'should create an element with custom root name', () => {
  25. const root = new RootEditableElement( document, 'h1' );
  26. root.rootName = 'header';
  27. expect( root.rootName ).to.equal( 'header' );
  28. expect( root.name ).to.equal( 'h1' );
  29. expect( root.isFocused ).to.be.false;
  30. expect( root.isReadOnly ).to.be.false;
  31. } );
  32. } );
  33. describe( 'is()', () => {
  34. let el;
  35. before( () => {
  36. el = new RootEditableElement( document, 'div' );
  37. } );
  38. it( 'should return true for rootElement/containerElement/editable/element, also with correct name and element name', () => {
  39. expect( el.is( 'rootElement' ) ).to.be.true;
  40. expect( el.is( 'view:rootElement' ) ).to.be.true;
  41. expect( el.is( 'rootElement', 'div' ) ).to.be.true;
  42. expect( el.is( 'view:rootElement', 'div' ) ).to.be.true;
  43. expect( el.is( 'containerElement' ) ).to.be.true;
  44. expect( el.is( 'view:containerElement' ) ).to.be.true;
  45. expect( el.is( 'containerElement', 'div' ) ).to.be.true;
  46. expect( el.is( 'view:containerElement', 'div' ) ).to.be.true;
  47. expect( el.is( 'editableElement' ) ).to.be.true;
  48. expect( el.is( 'view:editableElement' ) ).to.be.true;
  49. expect( el.is( 'editableElement', 'div' ) ).to.be.true;
  50. expect( el.is( 'view:editableElement', 'div' ) ).to.be.true;
  51. expect( el.is( 'element' ) ).to.be.true;
  52. expect( el.is( 'view:element' ) ).to.be.true;
  53. expect( el.is( 'element', 'div' ) ).to.be.true;
  54. expect( el.is( 'view:element', 'div' ) ).to.be.true;
  55. expect( el.is( 'div' ) ).to.be.true;
  56. expect( el.is( 'view:div' ) ).to.be.true;
  57. } );
  58. it( 'should return false for other accept values', () => {
  59. expect( el.is( 'rootElement', 'p' ) ).to.be.false;
  60. expect( el.is( 'view:rootElement', 'p' ) ).to.be.false;
  61. expect( el.is( 'containerElement', 'p' ) ).to.be.false;
  62. expect( el.is( 'view:containerElement', 'p' ) ).to.be.false;
  63. expect( el.is( 'element', 'p' ) ).to.be.false;
  64. expect( el.is( 'view:element', 'p' ) ).to.be.false;
  65. expect( el.is( 'p' ) ).to.be.false;
  66. expect( el.is( 'view:p' ) ).to.be.false;
  67. expect( el.is( 'text' ) ).to.be.false;
  68. expect( el.is( 'view:text' ) ).to.be.false;
  69. expect( el.is( 'textProxy' ) ).to.be.false;
  70. expect( el.is( 'attributeElement' ) ).to.be.false;
  71. expect( el.is( 'uiElement' ) ).to.be.false;
  72. expect( el.is( 'emptyElement' ) ).to.be.false;
  73. expect( el.is( 'documentFragment' ) ).to.be.false;
  74. expect( el.is( 'model:rootElement' ) ).to.be.false;
  75. } );
  76. } );
  77. describe( '_name', () => {
  78. it( 'should set new name to element', () => {
  79. const el = new RootEditableElement( document, '$root' );
  80. expect( el.name ).to.equal( '$root' );
  81. el._name = 'div';
  82. expect( el.name ).to.equal( 'div' );
  83. } );
  84. } );
  85. it( 'should be cloned properly', () => {
  86. const root = new RootEditableElement( document, 'h1' );
  87. root.rootName = 'header';
  88. const newRoot = root._clone();
  89. expect( newRoot.document ).to.equal( root.document );
  90. expect( newRoot.rootName ).to.equal( root.rootName );
  91. } );
  92. } );