containerelement.js 4.7 KB

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