emptyelement.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import EmptyElement from '../../src/view/emptyelement';
  6. import Element from '../../src/view/element';
  7. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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( 'view:emptyElement' ) ).to.be.true;
  26. expect( el.is( 'emptyElement', 'p' ) ).to.be.true;
  27. expect( el.is( 'view:emptyElement', 'p' ) ).to.be.true;
  28. expect( el.is( 'element' ) ).to.be.true;
  29. expect( el.is( 'view:element' ) ).to.be.true;
  30. expect( el.is( 'element', 'p' ) ).to.be.true;
  31. expect( el.is( 'view:element', 'p' ) ).to.be.true;
  32. expect( el.is( 'p' ) ).to.be.true;
  33. expect( el.is( 'view:p' ) ).to.be.true;
  34. } );
  35. it( 'should return false for other accept values', () => {
  36. expect( el.is( 'emptyElement', 'span' ) ).to.be.false;
  37. expect( el.is( 'view:emptyElement', 'span' ) ).to.be.false;
  38. expect( el.is( 'element', 'span' ) ).to.be.false;
  39. expect( el.is( 'view:element', 'span' ) ).to.be.false;
  40. expect( el.is( 'span' ) ).to.be.false;
  41. expect( el.is( 'view:span' ) ).to.be.false;
  42. expect( el.is( 'text' ) ).to.be.false;
  43. expect( el.is( 'view:text' ) ).to.be.false;
  44. expect( el.is( 'textProxy' ) ).to.be.false;
  45. expect( el.is( 'containerElement' ) ).to.be.false;
  46. expect( el.is( 'attributeElement' ) ).to.be.false;
  47. expect( el.is( 'uiElement' ) ).to.be.false;
  48. expect( el.is( 'rootElement' ) ).to.be.false;
  49. expect( el.is( 'documentFragment' ) ).to.be.false;
  50. } );
  51. } );
  52. it( 'should throw if child elements are passed to constructor', () => {
  53. const el = new Element( 'i' );
  54. expectToThrowCKEditorError( () => {
  55. new EmptyElement( 'img', null, [ el ] ); // eslint-disable-line no-new
  56. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', el );
  57. } );
  58. describe( '_appendChild', () => {
  59. it( 'should throw when try to append new child element', () => {
  60. expectToThrowCKEditorError( () => {
  61. emptyElement._appendChild( element );
  62. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', element );
  63. } );
  64. } );
  65. describe( '_insertChild', () => {
  66. it( 'should throw when try to insert new child element', () => {
  67. expectToThrowCKEditorError( () => {
  68. emptyElement._insertChild( 0, element );
  69. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', element );
  70. } );
  71. } );
  72. describe( '_clone()', () => {
  73. it( 'should be cloned properly', () => {
  74. const newEmptyElement = emptyElement._clone();
  75. expect( newEmptyElement.name ).to.equal( 'img' );
  76. expect( newEmptyElement.getAttribute( 'alt' ) ).to.equal( 'alternative text' );
  77. expect( newEmptyElement.getStyle( 'border' ) ).to.equal( '1px solid red' );
  78. expect( newEmptyElement.getStyle( 'color' ) ).to.equal( 'white' );
  79. expect( newEmptyElement.hasClass( 'image' ) ).to.be.true;
  80. expect( newEmptyElement.hasClass( 'big' ) ).to.be.true;
  81. expect( newEmptyElement.isSimilar( emptyElement ) ).to.be.true;
  82. } );
  83. } );
  84. describe( 'getFillerOffset', () => {
  85. it( 'should return null', () => {
  86. expect( emptyElement.getFillerOffset() ).to.be.null;
  87. } );
  88. } );
  89. } );