8
0

emptyelement.js 4.0 KB

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