rawelement.js 4.2 KB

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