emptyelement.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import EmptyElement from '../../src/view/emptyelement';
  6. import Element from '../../src/view/element';
  7. import Document from '../../src/view/document';
  8. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. import { StylesProcessor } from '../../src/view/stylesmap';
  10. describe( 'EmptyElement', () => {
  11. let element, emptyElement, document;
  12. beforeEach( () => {
  13. document = new Document( new StylesProcessor() );
  14. element = new Element( document, 'b' );
  15. emptyElement = new EmptyElement( document, 'img', {
  16. alt: 'alternative text',
  17. style: 'margin-top: 2em;color: white;',
  18. class: 'image big'
  19. } );
  20. } );
  21. describe( 'is()', () => {
  22. let el;
  23. before( () => {
  24. el = new EmptyElement( document, 'p' );
  25. } );
  26. it( 'should return true for emptyElement/element, also with correct name and element name', () => {
  27. expect( el.is( 'emptyElement' ) ).to.be.true;
  28. expect( el.is( 'view:emptyElement' ) ).to.be.true;
  29. expect( el.is( 'emptyElement', 'p' ) ).to.be.true;
  30. expect( el.is( 'view:emptyElement', 'p' ) ).to.be.true;
  31. expect( el.is( 'element' ) ).to.be.true;
  32. expect( el.is( 'view:element' ) ).to.be.true;
  33. expect( el.is( 'element', 'p' ) ).to.be.true;
  34. expect( el.is( 'view:element', 'p' ) ).to.be.true;
  35. expect( el.is( 'element', 'p' ) ).to.be.true;
  36. } );
  37. it( 'should return false for other accept values', () => {
  38. expect( el.is( 'emptyElement', 'span' ) ).to.be.false;
  39. expect( el.is( 'view:emptyElement', 'span' ) ).to.be.false;
  40. expect( el.is( 'element', 'span' ) ).to.be.false;
  41. expect( el.is( 'view:element', 'span' ) ).to.be.false;
  42. expect( el.is( 'element', 'span' ) ).to.be.false;
  43. expect( el.is( 'view:span' ) ).to.be.false;
  44. expect( el.is( '$text' ) ).to.be.false;
  45. expect( el.is( 'view:$text' ) ).to.be.false;
  46. expect( el.is( '$textProxy' ) ).to.be.false;
  47. expect( el.is( 'containerElement' ) ).to.be.false;
  48. expect( el.is( 'attributeElement' ) ).to.be.false;
  49. expect( el.is( 'uiElement' ) ).to.be.false;
  50. expect( el.is( 'rootElement' ) ).to.be.false;
  51. expect( el.is( 'documentFragment' ) ).to.be.false;
  52. } );
  53. } );
  54. it( 'should throw if child elements are passed to constructor', () => {
  55. const el = new Element( document, 'i' );
  56. expectToThrowCKEditorError( () => {
  57. new EmptyElement( document, 'img', null, [ el ] ); // eslint-disable-line no-new
  58. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', el );
  59. } );
  60. describe( '_appendChild', () => {
  61. it( 'should throw when try to append new child element', () => {
  62. expectToThrowCKEditorError( () => {
  63. emptyElement._appendChild( element );
  64. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', element );
  65. } );
  66. } );
  67. describe( '_insertChild', () => {
  68. it( 'should throw when try to insert new child element', () => {
  69. expectToThrowCKEditorError( () => {
  70. emptyElement._insertChild( 0, element );
  71. }, 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.', element );
  72. } );
  73. } );
  74. describe( '_clone()', () => {
  75. it( 'should be cloned properly', () => {
  76. const newEmptyElement = emptyElement._clone();
  77. expect( newEmptyElement.name ).to.equal( 'img' );
  78. expect( newEmptyElement.getAttribute( 'alt' ) ).to.equal( 'alternative text' );
  79. expect( newEmptyElement.getStyle( 'margin-top' ) ).to.equal( '2em' );
  80. expect( newEmptyElement.getStyle( 'color' ) ).to.equal( 'white' );
  81. expect( newEmptyElement.hasClass( 'image' ) ).to.be.true;
  82. expect( newEmptyElement.hasClass( 'big' ) ).to.be.true;
  83. expect( newEmptyElement.isSimilar( emptyElement ) ).to.be.true;
  84. } );
  85. } );
  86. describe( 'getFillerOffset', () => {
  87. it( 'should return null', () => {
  88. expect( emptyElement.getFillerOffset() ).to.be.null;
  89. } );
  90. } );
  91. } );