containerelement.js 4.6 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 { default as ContainerElement, getFillerOffset } from '../../src/view/containerelement';
  6. import Element from '../../src/view/element';
  7. import Document from '../../src/view/document';
  8. import { parse } from '../../src/dev-utils/view';
  9. describe( 'ContainerElement', () => {
  10. let document;
  11. beforeEach( () => {
  12. document = new Document();
  13. } );
  14. describe( 'constructor()', () => {
  15. it( 'should create element with default priority', () => {
  16. const el = new ContainerElement( document, 'p' );
  17. expect( el ).to.be.an.instanceof( ContainerElement );
  18. expect( el ).to.be.an.instanceof( Element );
  19. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  20. } );
  21. } );
  22. describe( 'is()', () => {
  23. let el;
  24. before( () => {
  25. el = new ContainerElement( document, 'p' );
  26. } );
  27. it( 'should return true for containerElement/element, also with correct name and element name', () => {
  28. expect( el.is( 'containerElement' ) ).to.be.true;
  29. expect( el.is( 'view:containerElement' ) ).to.be.true;
  30. expect( el.is( 'containerElement', 'p' ) ).to.be.true;
  31. expect( el.is( 'view:containerElement', '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( 'containerElement', 'span' ) ).to.be.false;
  41. expect( el.is( 'view:containerElement', '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( 'textProxy' ) ).to.be.false;
  48. expect( el.is( 'attributeElement' ) ).to.be.false;
  49. expect( el.is( 'uiElement' ) ).to.be.false;
  50. expect( el.is( 'emptyElement' ) ).to.be.false;
  51. expect( el.is( 'rootElement' ) ).to.be.false;
  52. expect( el.is( 'documentFragment' ) ).to.be.false;
  53. } );
  54. } );
  55. describe( 'getFillerOffset', () => {
  56. it( 'should return position 0 if element is empty', () => {
  57. expect( parse( '<container:p></container:p>' ).getFillerOffset() ).to.equals( 0 );
  58. } );
  59. it( 'should return offset after all children if element contains only ui elements', () => {
  60. expect( parse( '<container:p><ui:span></ui:span><ui:span></ui:span></container:p>' ).getFillerOffset() ).to.equals( 2 );
  61. } );
  62. it( 'should return null if element is not empty', () => {
  63. expect( parse( '<container:p>foo</container:p>' ).getFillerOffset() ).to.be.null;
  64. } );
  65. // Block filler is required after the `<br>` element if the element is the last child in the container. See #1422.
  66. describe( 'for <br> elements in container', () => {
  67. it( 'returns null because container does not need the block filler', () => {
  68. expect( parse( '<container:p>Foo.</container:p>' ).getFillerOffset() ).to.equals( null );
  69. } );
  70. it( 'returns offset of the last child which is the <br> element (1)', () => {
  71. expect( parse( '<container:p><empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 1 );
  72. } );
  73. it( 'returns offset of the last child which is the <br> element (2)', () => {
  74. expect( parse( '<container:p>Foo.<empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 2 );
  75. } );
  76. it( 'always returns the last <br> element in the container', () => {
  77. expect( parse( '<container:p>Foo.<empty:br></empty:br><empty:br></empty:br></container:p>' ).getFillerOffset() )
  78. .to.equals( 3 );
  79. } );
  80. it( 'works fine with non-empty container with multi <br> elements', () => {
  81. expect( parse( '<container:p>Foo.<empty:br></empty:br>Bar.<empty:br></empty:br></container:p>' ).getFillerOffset() )
  82. .to.equals( 4 );
  83. } );
  84. it( 'ignores the ui elements', () => {
  85. expect( parse( '<container:p><ui:span></ui:span><empty:br></empty:br></container:p>' ).getFillerOffset() )
  86. .to.equals( 2 );
  87. } );
  88. it( 'empty element must be the <br> element', () => {
  89. expect( parse( '<container:p>Foo<empty:img></empty:img></container:p>' ).getFillerOffset() )
  90. .to.equals( null );
  91. } );
  92. } );
  93. } );
  94. } );
  95. describe( 'getFillerOffset()', () => {
  96. it( 'should be a function that can be used in other places', () => {
  97. expect( getFillerOffset ).is.a( 'function' );
  98. } );
  99. } );