8
0

uielement.js 5.4 KB

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