uielement.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, HTMLElement */
  6. import ViewUIElement from '../../../src/view/uielement';
  7. import ViewContainer from '../../../src/view/containerelement';
  8. import DomConverter from '../../../src/view/domconverter';
  9. describe( 'DOMConverter UIElement integration', () => {
  10. let converter;
  11. class MyUIElement extends ViewUIElement {
  12. render( domDocument ) {
  13. const root = super.render( domDocument );
  14. root.innerHTML = '<p><span>foo</span> bar</p>';
  15. return root;
  16. }
  17. }
  18. beforeEach( () => {
  19. converter = new DomConverter();
  20. } );
  21. describe( 'viewToDom', () => {
  22. it( 'should create DOM element from UIElement', () => {
  23. const uiElement = new ViewUIElement( 'div' );
  24. const domElement = converter.viewToDom( uiElement, document );
  25. expect( domElement ).to.be.instanceOf( HTMLElement );
  26. } );
  27. it( 'should create DOM structure from UIElement', () => {
  28. const myElement = new MyUIElement( 'div' );
  29. const domElement = converter.viewToDom( myElement, document );
  30. expect( domElement ).to.be.instanceOf( HTMLElement );
  31. expect( domElement.innerHTML ).to.equal( '<p><span>foo</span> bar</p>' );
  32. } );
  33. it( 'should not bind rendered elements', () => {
  34. const myElement = new MyUIElement( 'div' );
  35. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  36. const domSpan = domElement.childNodes[ 0 ];
  37. expect( converter.getCorrespondingView( domElement ) ).to.equal( myElement );
  38. expect( converter.getCorrespondingView( domSpan ) ).to.be.falsy;
  39. expect( converter.getCorrespondingView( domSpan.childNodes[ 0 ] ) ).to.be.falsy;
  40. } );
  41. } );
  42. describe( 'domToView', () => {
  43. it( 'should return UIElement itself', () => {
  44. const uiElement = new MyUIElement( 'div' );
  45. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  46. expect( converter.domToView( domElement ) ).to.equal( uiElement );
  47. } );
  48. it( 'should return UIElement for nodes inside', () => {
  49. const uiElement = new MyUIElement( 'div' );
  50. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  51. const domParagraph = domElement.childNodes[ 0 ];
  52. const domSpan = domParagraph.childNodes[ 0 ];
  53. expect( converter.domToView( domParagraph ) ).to.equal( uiElement );
  54. expect( converter.domToView( domSpan ) ).to.be.equal( uiElement );
  55. expect( converter.domToView( domParagraph.childNodes[ 0 ] ) ).equal( uiElement );
  56. expect( converter.domToView( domSpan.childNodes[ 0 ] ) ).equal( uiElement );
  57. } );
  58. } );
  59. describe( 'domPositionToView', () => {
  60. it( 'should convert position inside UIElement to position before it', () => {
  61. const uiElement = new MyUIElement( 'h1' );
  62. const container = new ViewContainer( 'div', null, [ new ViewContainer( 'div' ), uiElement ] );
  63. const domContainer = converter.viewToDom( container, document, { bind: true } );
  64. const viewPosition = converter.domPositionToView( domContainer.childNodes[ 1 ], 0 );
  65. expect( viewPosition.parent ).to.equal( container );
  66. expect( viewPosition.offset ).to.equal( 1 );
  67. } );
  68. it( 'should convert position inside UIElement children to position before UIElement', () => {
  69. const uiElement = new MyUIElement( 'h1' );
  70. const container = new ViewContainer( 'div', null, [ new ViewContainer( 'div' ), uiElement ] );
  71. const domContainer = converter.viewToDom( container, document, { bind: true } );
  72. const viewPosition = converter.domPositionToView( domContainer.childNodes[ 1 ].childNodes[ 0 ], 1 );
  73. expect( viewPosition.parent ).to.equal( container );
  74. expect( viewPosition.offset ).to.equal( 1 );
  75. } );
  76. } );
  77. } );