8
0

uielement.js 5.5 KB

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