uielement.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import UIElement from '../../src/view/uielement';
  6. import Element from '../../src/view/element';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. describe( 'UIElement', () => {
  9. let uiElement;
  10. beforeEach( () => {
  11. uiElement = new UIElement( 'span', {
  12. foo: 'bar',
  13. style: 'border: 1px solid red;color: white;',
  14. class: 'foo bar'
  15. } );
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'should create instance', () => {
  19. expect( uiElement.name ).to.equal( 'span' );
  20. expect( uiElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
  21. expect( uiElement.getStyle( 'border' ) ).to.equal( '1px solid red' );
  22. expect( uiElement.getStyle( 'color' ) ).to.equal( 'white' );
  23. expect( uiElement.hasClass( 'foo' ) ).to.true;
  24. expect( uiElement.hasClass( 'bar' ) ).to.true;
  25. } );
  26. it( 'should throw if child elements are passed to constructor', () => {
  27. expect( () => {
  28. new UIElement( 'img', null, [ new Element( 'i' ) ] );
  29. } ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  30. } );
  31. } );
  32. describe( 'is()', () => {
  33. let el;
  34. before( () => {
  35. el = new UIElement( 'span' );
  36. } );
  37. it( 'should return true for uiElement/element, also with correct name and element name', () => {
  38. expect( el.is( 'uiElement' ) ).to.be.true;
  39. expect( el.is( 'uiElement', 'span' ) ).to.be.true;
  40. expect( el.is( 'element' ) ).to.be.true;
  41. expect( el.is( 'element', 'span' ) ).to.be.true;
  42. expect( el.is( 'span' ) ).to.be.true;
  43. } );
  44. it( 'should return false for other accept values', () => {
  45. expect( el.is( 'uiElement', 'p' ) ).to.be.false;
  46. expect( el.is( 'element', 'p' ) ).to.be.false;
  47. expect( el.is( 'p' ) ).to.be.false;
  48. expect( el.is( 'text' ) ).to.be.false;
  49. expect( el.is( 'textProxy' ) ).to.be.false;
  50. expect( el.is( 'containerElement' ) ).to.be.false;
  51. expect( el.is( 'attributeElement' ) ).to.be.false;
  52. expect( el.is( 'emptyElement' ) ).to.be.false;
  53. expect( el.is( 'rootElement' ) ).to.be.false;
  54. expect( el.is( 'documentFragment' ) ).to.be.false;
  55. } );
  56. } );
  57. describe( 'appendChildren()', () => {
  58. it( 'should throw when try to append new child element', () => {
  59. expect( () => {
  60. uiElement.appendChildren( new Element( 'i' ) );
  61. } ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  62. } );
  63. } );
  64. describe( 'insertChildren()', () => {
  65. it( 'should throw when try to insert new child element', () => {
  66. expect( () => {
  67. uiElement.insertChildren( 0, new Element( 'i' ) );
  68. } ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
  69. } );
  70. } );
  71. describe( 'clone()', () => {
  72. it( 'should be properly cloned', () => {
  73. const newUIElement = uiElement.clone();
  74. expect( newUIElement.name ).to.equal( 'span' );
  75. expect( newUIElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
  76. expect( newUIElement.getStyle( 'border' ) ).to.equal( '1px solid red' );
  77. expect( newUIElement.getStyle( 'color' ) ).to.equal( 'white' );
  78. expect( newUIElement.hasClass( 'foo' ) ).to.true;
  79. expect( newUIElement.hasClass( 'bar' ) ).to.true;
  80. expect( newUIElement.isSimilar( uiElement ) ).to.true;
  81. } );
  82. } );
  83. describe( 'getFillerOffset()', () => {
  84. it( 'should return null', () => {
  85. expect( uiElement.getFillerOffset() ).to.null;
  86. } );
  87. } );
  88. } );