uielement.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* global document, HTMLElement */
  6. import UIElement from '../../src/view/uielement';
  7. import Element from '../../src/view/element';
  8. import Document from '../../src/view/document';
  9. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'UIElement', () => {
  11. let uiElement, doc;
  12. beforeEach( () => {
  13. doc = new Document();
  14. uiElement = new UIElement( doc, 'span', {
  15. foo: 'bar',
  16. style: 'margin-top: 2em;color: white;',
  17. class: 'foo bar'
  18. } );
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create instance', () => {
  22. expect( uiElement.name ).to.equal( 'span' );
  23. expect( uiElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. expect( uiElement.getStyle( 'margin-top' ) ).to.equal( '2em' );
  25. expect( uiElement.getStyle( 'color' ) ).to.equal( 'white' );
  26. expect( uiElement.hasClass( 'foo' ) ).to.true;
  27. expect( uiElement.hasClass( 'bar' ) ).to.true;
  28. } );
  29. it( 'should throw if child elements are passed to constructor', () => {
  30. expectToThrowCKEditorError( () => {
  31. new UIElement( doc, 'img', null, [ new Element( doc, 'i' ) ] ); // eslint-disable-line no-new
  32. }, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  33. } );
  34. } );
  35. describe( 'is()', () => {
  36. let el;
  37. before( () => {
  38. el = new UIElement( doc, 'span' );
  39. } );
  40. it( 'should return true for uiElement/element, also with correct name and element name', () => {
  41. expect( el.is( 'uiElement' ) ).to.be.true;
  42. expect( el.is( 'view:uiElement' ) ).to.be.true;
  43. expect( el.is( 'uiElement', 'span' ) ).to.be.true;
  44. expect( el.is( 'view:uiElement', 'span' ) ).to.be.true;
  45. expect( el.is( 'element' ) ).to.be.true;
  46. expect( el.is( 'view:element' ) ).to.be.true;
  47. expect( el.is( 'node' ) ).to.be.true;
  48. expect( el.is( 'view:node' ) ).to.be.true;
  49. expect( el.is( 'element', 'span' ) ).to.be.true;
  50. expect( el.is( 'view:element', 'span' ) ).to.be.true;
  51. expect( el.is( 'span' ) ).to.be.true;
  52. expect( el.is( 'view:span' ) ).to.be.true;
  53. } );
  54. it( 'should return false for other accept values', () => {
  55. expect( el.is( 'uiElement', 'p' ) ).to.be.false;
  56. expect( el.is( 'view:uiElement', 'p' ) ).to.be.false;
  57. expect( el.is( 'element', 'p' ) ).to.be.false;
  58. expect( el.is( 'view:element', 'p' ) ).to.be.false;
  59. expect( el.is( 'p' ) ).to.be.false;
  60. expect( el.is( 'view:p' ) ).to.be.false;
  61. expect( el.is( 'text' ) ).to.be.false;
  62. expect( el.is( 'textProxy' ) ).to.be.false;
  63. expect( el.is( 'containerElement' ) ).to.be.false;
  64. expect( el.is( 'attributeElement' ) ).to.be.false;
  65. expect( el.is( 'emptyElement' ) ).to.be.false;
  66. expect( el.is( 'rootElement' ) ).to.be.false;
  67. expect( el.is( 'documentFragment' ) ).to.be.false;
  68. expect( el.is( 'model:element' ) ).to.be.false;
  69. expect( el.is( 'model:span' ) ).to.be.false;
  70. expect( el.is( 'model:node' ) ).to.be.false;
  71. } );
  72. } );
  73. describe( '_appendChild()', () => {
  74. it( 'should throw when try to append new child element', () => {
  75. expectToThrowCKEditorError( () => {
  76. uiElement._appendChild( new Element( doc, 'i' ) );
  77. }, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  78. } );
  79. } );
  80. describe( '_insertChild()', () => {
  81. it( 'should throw when try to insert new child element', () => {
  82. expectToThrowCKEditorError( () => {
  83. uiElement._insertChild( 0, new Element( doc, 'i' ) );
  84. }, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  85. } );
  86. } );
  87. describe( '_clone()', () => {
  88. it( 'should be properly cloned', () => {
  89. const newUIElement = uiElement._clone();
  90. expect( newUIElement.name ).to.equal( 'span' );
  91. expect( newUIElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
  92. expect( newUIElement.getStyle( 'margin-top' ) ).to.equal( '2em' );
  93. expect( newUIElement.getStyle( 'color' ) ).to.equal( 'white' );
  94. expect( newUIElement.hasClass( 'foo' ) ).to.true;
  95. expect( newUIElement.hasClass( 'bar' ) ).to.true;
  96. expect( newUIElement.isSimilar( uiElement ) ).to.true;
  97. } );
  98. } );
  99. describe( 'getFillerOffset()', () => {
  100. it( 'should return null', () => {
  101. expect( uiElement.getFillerOffset() ).to.null;
  102. } );
  103. } );
  104. describe( 'render()', () => {
  105. let domElement;
  106. beforeEach( () => {
  107. domElement = uiElement.render( document );
  108. } );
  109. it( 'should return DOM element', () => {
  110. expect( domElement ).to.be.instanceOf( HTMLElement );
  111. } );
  112. it( 'should use element name', () => {
  113. expect( domElement.tagName.toLowerCase() ).to.equal( uiElement.name );
  114. } );
  115. it( 'should render attributes', () => {
  116. for ( const key of uiElement.getAttributeKeys() ) {
  117. expect( domElement.getAttribute( key ) ).to.equal( uiElement.getAttribute( key ) );
  118. }
  119. } );
  120. it( 'should allow to change render() method', () => {
  121. uiElement.render = function( domDocument ) {
  122. return domDocument.createElement( 'b' );
  123. };
  124. expect( uiElement.render( document ).tagName.toLowerCase() ).to.equal( 'b' );
  125. } );
  126. it( 'should allow to add new elements inside', () => {
  127. uiElement.render = function( domDocument ) {
  128. const element = this.toDomElement( domDocument );
  129. const text = domDocument.createTextNode( 'foo bar' );
  130. element.appendChild( text );
  131. return element;
  132. };
  133. const rendered = uiElement.render( document );
  134. expect( rendered.tagName.toLowerCase() ).to.equal( 'span' );
  135. expect( rendered.textContent ).to.equal( 'foo bar' );
  136. } );
  137. } );
  138. } );