containerelement.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ContainerElement from '../../src/view/containerelement';
  6. import Element from '../../src/view/element';
  7. import { parse } from '../../src/dev-utils/view';
  8. describe( 'ContainerElement', () => {
  9. describe( 'constructor()', () => {
  10. it( 'should create element with default priority', () => {
  11. const el = new ContainerElement( 'p' );
  12. expect( el ).to.be.an.instanceof( ContainerElement );
  13. expect( el ).to.be.an.instanceof( Element );
  14. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  15. } );
  16. } );
  17. describe( 'is', () => {
  18. let el;
  19. before( () => {
  20. el = new ContainerElement( 'p' );
  21. } );
  22. it( 'should return true for containerElement/element, also with correct name and element name', () => {
  23. expect( el.is( 'containerElement' ) ).to.be.true;
  24. expect( el.is( 'containerElement', 'p' ) ).to.be.true;
  25. expect( el.is( 'element' ) ).to.be.true;
  26. expect( el.is( 'element', 'p' ) ).to.be.true;
  27. expect( el.is( 'p' ) ).to.be.true;
  28. } );
  29. it( 'should return false for other accept values', () => {
  30. expect( el.is( 'containerElement', 'span' ) ).to.be.false;
  31. expect( el.is( 'element', 'span' ) ).to.be.false;
  32. expect( el.is( 'span' ) ).to.be.false;
  33. expect( el.is( 'text' ) ).to.be.false;
  34. expect( el.is( 'textProxy' ) ).to.be.false;
  35. expect( el.is( 'attributeElement' ) ).to.be.false;
  36. expect( el.is( 'uiElement' ) ).to.be.false;
  37. expect( el.is( 'emptyElement' ) ).to.be.false;
  38. expect( el.is( 'rootElement' ) ).to.be.false;
  39. expect( el.is( 'documentFragment' ) ).to.be.false;
  40. } );
  41. } );
  42. describe( 'getFillerOffset', () => {
  43. it( 'should return position 0 if element is empty', () => {
  44. expect( parse( '<container:p></container:p>' ).getFillerOffset() ).to.equals( 0 );
  45. } );
  46. it( 'should return offset after all children if element contains only ui elements', () => {
  47. expect( parse( '<container:p><ui:span></ui:span><ui:span></ui:span></container:p>' ).getFillerOffset() ).to.equals( 2 );
  48. } );
  49. it( 'should return null if element is not empty', () => {
  50. expect( parse( '<container:p>foo</container:p>' ).getFillerOffset() ).to.be.null;
  51. } );
  52. // Block filler is required after the `<br>` element if the element is the last child in the container. See #1422.
  53. describe( 'for <br> elements in container', () => {
  54. it( 'returns null because container does not need the block filler', () => {
  55. expect( parse( '<container:p>Foo.</container:p>' ).getFillerOffset() ).to.equals( null );
  56. } );
  57. it( 'returns offset of the last child which is the <br> element (1)', () => {
  58. expect( parse( '<container:p><empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 1 );
  59. } );
  60. it( 'returns offset of the last child which is the <br> element (2)', () => {
  61. expect( parse( '<container:p>Foo.<empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 2 );
  62. } );
  63. it( 'always returns the last <br> element in the container', () => {
  64. expect( parse( '<container:p>Foo.<empty:br></empty:br><empty:br></empty:br></container:p>' ).getFillerOffset() )
  65. .to.equals( 3 );
  66. } );
  67. it( 'works fine with non-empty container with multi <br> elements', () => {
  68. expect( parse( '<container:p>Foo.<empty:br></empty:br>Bar.<empty:br></empty:br></container:p>' ).getFillerOffset() )
  69. .to.equals( 4 );
  70. } );
  71. it( 'ignores the ui elements', () => {
  72. expect( parse( '<container:p><ui:span></ui:span><empty:br></empty:br></container:p>' ).getFillerOffset() )
  73. .to.equals( 2 );
  74. } );
  75. it( 'empty element must be the <br> element', () => {
  76. expect( parse( '<container:p>Foo<empty:img></empty:img></container:p>' ).getFillerOffset() )
  77. .to.equals( null );
  78. } );
  79. } );
  80. } );
  81. } );