containerelement.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 { 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( 'view:containerElement' ) ).to.be.true;
  25. expect( el.is( 'containerElement', 'p' ) ).to.be.true;
  26. expect( el.is( 'view:containerElement', 'p' ) ).to.be.true;
  27. expect( el.is( 'element' ) ).to.be.true;
  28. expect( el.is( 'view:element' ) ).to.be.true;
  29. expect( el.is( 'element', 'p' ) ).to.be.true;
  30. expect( el.is( 'view:element', 'p' ) ).to.be.true;
  31. expect( el.is( 'p' ) ).to.be.true;
  32. expect( el.is( 'view:p' ) ).to.be.true;
  33. } );
  34. it( 'should return false for other accept values', () => {
  35. expect( el.is( 'containerElement', 'span' ) ).to.be.false;
  36. expect( el.is( 'view:containerElement', 'span' ) ).to.be.false;
  37. expect( el.is( 'element', 'span' ) ).to.be.false;
  38. expect( el.is( 'view:element', 'span' ) ).to.be.false;
  39. expect( el.is( 'span' ) ).to.be.false;
  40. expect( el.is( 'view:span' ) ).to.be.false;
  41. expect( el.is( 'text' ) ).to.be.false;
  42. expect( el.is( 'textProxy' ) ).to.be.false;
  43. expect( el.is( 'attributeElement' ) ).to.be.false;
  44. expect( el.is( 'uiElement' ) ).to.be.false;
  45. expect( el.is( 'emptyElement' ) ).to.be.false;
  46. expect( el.is( 'rootElement' ) ).to.be.false;
  47. expect( el.is( 'documentFragment' ) ).to.be.false;
  48. } );
  49. } );
  50. describe( 'getFillerOffset', () => {
  51. it( 'should return position 0 if element is empty', () => {
  52. expect( parse( '<container:p></container:p>' ).getFillerOffset() ).to.equals( 0 );
  53. } );
  54. it( 'should return offset after all children if element contains only ui elements', () => {
  55. expect( parse( '<container:p><ui:span></ui:span><ui:span></ui:span></container:p>' ).getFillerOffset() ).to.equals( 2 );
  56. } );
  57. it( 'should return null if element is not empty', () => {
  58. expect( parse( '<container:p>foo</container:p>' ).getFillerOffset() ).to.be.null;
  59. } );
  60. // Block filler is required after the `<br>` element if the element is the last child in the container. See #1422.
  61. describe( 'for <br> elements in container', () => {
  62. it( 'returns null because container does not need the block filler', () => {
  63. expect( parse( '<container:p>Foo.</container:p>' ).getFillerOffset() ).to.equals( null );
  64. } );
  65. it( 'returns offset of the last child which is the <br> element (1)', () => {
  66. expect( parse( '<container:p><empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 1 );
  67. } );
  68. it( 'returns offset of the last child which is the <br> element (2)', () => {
  69. expect( parse( '<container:p>Foo.<empty:br></empty:br></container:p>' ).getFillerOffset() ).to.equals( 2 );
  70. } );
  71. it( 'always returns the last <br> element in the container', () => {
  72. expect( parse( '<container:p>Foo.<empty:br></empty:br><empty:br></empty:br></container:p>' ).getFillerOffset() )
  73. .to.equals( 3 );
  74. } );
  75. it( 'works fine with non-empty container with multi <br> elements', () => {
  76. expect( parse( '<container:p>Foo.<empty:br></empty:br>Bar.<empty:br></empty:br></container:p>' ).getFillerOffset() )
  77. .to.equals( 4 );
  78. } );
  79. it( 'ignores the ui elements', () => {
  80. expect( parse( '<container:p><ui:span></ui:span><empty:br></empty:br></container:p>' ).getFillerOffset() )
  81. .to.equals( 2 );
  82. } );
  83. it( 'empty element must be the <br> element', () => {
  84. expect( parse( '<container:p>Foo<empty:img></empty:img></container:p>' ).getFillerOffset() )
  85. .to.equals( null );
  86. } );
  87. } );
  88. } );
  89. } );
  90. describe( 'getFillerOffset()', () => {
  91. it( 'should be a function that can be used in other places', () => {
  92. expect( getFillerOffset ).is.a( 'function' );
  93. } );
  94. } );