uielement.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. import UIElement from '../../src/view/uielement';
  7. import Element from '../../src/view/element';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. describe( 'UIElement', () => {
  10. let uiElement;
  11. beforeEach( () => {
  12. uiElement = new UIElement( 'span', {
  13. foo: 'bar',
  14. style: 'border: 1px solid red;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( 'border' ) ).to.equal( '1px solid red' );
  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. expect( () => {
  29. new UIElement( 'img', null, [ new Element( 'i' ) ] ); // eslint-disable-line no-new
  30. } ).to.throw( CKEditorError, '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( 'uiElement', 'span' ) ).to.be.true;
  41. expect( el.is( 'element' ) ).to.be.true;
  42. expect( el.is( 'element', 'span' ) ).to.be.true;
  43. expect( el.is( 'span' ) ).to.be.true;
  44. } );
  45. it( 'should return false for other accept values', () => {
  46. expect( el.is( 'uiElement', 'p' ) ).to.be.false;
  47. expect( el.is( 'element', 'p' ) ).to.be.false;
  48. expect( el.is( 'p' ) ).to.be.false;
  49. expect( el.is( 'text' ) ).to.be.false;
  50. expect( el.is( 'textProxy' ) ).to.be.false;
  51. expect( el.is( 'containerElement' ) ).to.be.false;
  52. expect( el.is( 'attributeElement' ) ).to.be.false;
  53. expect( el.is( 'emptyElement' ) ).to.be.false;
  54. expect( el.is( 'rootElement' ) ).to.be.false;
  55. expect( el.is( 'documentFragment' ) ).to.be.false;
  56. } );
  57. } );
  58. describe( 'appendChildren()', () => {
  59. it( 'should throw when try to append new child element', () => {
  60. expect( () => {
  61. uiElement.appendChildren( new Element( 'i' ) );
  62. } ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  63. } );
  64. } );
  65. describe( 'insertChildren()', () => {
  66. it( 'should throw when try to insert new child element', () => {
  67. expect( () => {
  68. uiElement.insertChildren( 0, new Element( 'i' ) );
  69. } ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  70. } );
  71. } );
  72. describe( 'clone()', () => {
  73. it( 'should be properly cloned', () => {
  74. const newUIElement = uiElement.clone();
  75. expect( newUIElement.name ).to.equal( 'span' );
  76. expect( newUIElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
  77. expect( newUIElement.getStyle( 'border' ) ).to.equal( '1px solid red' );
  78. expect( newUIElement.getStyle( 'color' ) ).to.equal( 'white' );
  79. expect( newUIElement.hasClass( 'foo' ) ).to.true;
  80. expect( newUIElement.hasClass( 'bar' ) ).to.true;
  81. expect( newUIElement.isSimilar( uiElement ) ).to.true;
  82. } );
  83. } );
  84. describe( 'getFillerOffset()', () => {
  85. it( 'should return null', () => {
  86. expect( uiElement.getFillerOffset() ).to.null;
  87. } );
  88. } );
  89. describe( 'render()', () => {
  90. let domElement;
  91. beforeEach( () => {
  92. domElement = uiElement.render( document );
  93. } );
  94. it( 'should return DOM element', () => {
  95. expect( domElement ).to.be.instanceOf( HTMLElement );
  96. } );
  97. it( 'should use element name', () => {
  98. expect( domElement.tagName.toLowerCase() ).to.equal( uiElement.name );
  99. } );
  100. it( 'should render attributes', () => {
  101. for ( const key of uiElement.getAttributeKeys() ) {
  102. expect( domElement.getAttribute( key ) ).to.equal( uiElement.getAttribute( key ) );
  103. }
  104. } );
  105. } );
  106. } );