emptyelement.js 3.2 KB

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