emptyelement.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. } );
  36. it( 'should return false for other accept values', () => {
  37. expect( el.is( 'emptyElement', 'span' ) ).to.be.false;
  38. expect( el.is( 'view:emptyElement', 'span' ) ).to.be.false;
  39. expect( el.is( 'element', 'span' ) ).to.be.false;
  40. expect( el.is( 'view:element', 'span' ) ).to.be.false;
  41. expect( el.is( 'element', 'span' ) ).to.be.false;
  42. expect( el.is( 'view:span' ) ).to.be.false;
  43. expect( el.is( '$text' ) ).to.be.false;
  44. expect( el.is( 'view:$text' ) ).to.be.false;
  45. expect( el.is( '$textProxy' ) ).to.be.false;
  46. expect( el.is( 'containerElement' ) ).to.be.false;
  47. expect( el.is( 'attributeElement' ) ).to.be.false;
  48. expect( el.is( 'uiElement' ) ).to.be.false;
  49. expect( el.is( 'rootElement' ) ).to.be.false;
  50. expect( el.is( 'documentFragment' ) ).to.be.false;
  51. expect( el.is( 'node', 'p' ) ).to.be.false;
  52. expect( el.is( 'view:node', 'p' ) ).to.be.false;
  53. } );
  54. } );
  55. it( 'should throw if child elements are passed to constructor', () => {
  56. const el = new Element( document, 'i' );
  57. expectToThrowCKEditorError( () => {
  58. new EmptyElement( document, 'img', null, [ el ] ); // eslint-disable-line no-new
  59. }, 'view-emptyelement-cannot-add', el );
  60. } );
  61. describe( '_appendChild', () => {
  62. it( 'should throw when try to append new child element', () => {
  63. expectToThrowCKEditorError( () => {
  64. emptyElement._appendChild( element );
  65. }, 'view-emptyelement-cannot-add', element );
  66. } );
  67. } );
  68. describe( '_insertChild', () => {
  69. it( 'should throw when try to insert new child element', () => {
  70. expectToThrowCKEditorError( () => {
  71. emptyElement._insertChild( 0, element );
  72. }, 'view-emptyelement-cannot-add', element );
  73. } );
  74. } );
  75. describe( '_clone()', () => {
  76. it( 'should be cloned properly', () => {
  77. const newEmptyElement = emptyElement._clone();
  78. expect( newEmptyElement.name ).to.equal( 'img' );
  79. expect( newEmptyElement.getAttribute( 'alt' ) ).to.equal( 'alternative text' );
  80. expect( newEmptyElement.getStyle( 'margin-top' ) ).to.equal( '2em' );
  81. expect( newEmptyElement.getStyle( 'color' ) ).to.equal( 'white' );
  82. expect( newEmptyElement.hasClass( 'image' ) ).to.be.true;
  83. expect( newEmptyElement.hasClass( 'big' ) ).to.be.true;
  84. expect( newEmptyElement.isSimilar( emptyElement ) ).to.be.true;
  85. } );
  86. } );
  87. describe( 'getFillerOffset', () => {
  88. it( 'should return null', () => {
  89. expect( emptyElement.getFillerOffset() ).to.be.null;
  90. } );
  91. } );
  92. } );