uielement.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 bind only UIElement not child 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.mapDomToView( domElement ) ).to.equal( myElement );
  38. expect( converter.mapDomToView( domSpan ) ).to.be.false;
  39. expect( converter.mapDomToView( domSpan.childNodes[ 0 ] ) ).to.be.false;
  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. describe( 'mapDomToView()', () => {
  78. it( 'should return UIElement for DOM elements inside', () => {
  79. const myElement = new MyUIElement( 'div' );
  80. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  81. expect( converter.mapDomToView( domElement ) ).to.equal( myElement );
  82. const domParagraph = domElement.childNodes[ 0 ];
  83. expect( converter.mapDomToView( domParagraph ) ).to.equal( myElement );
  84. const domSpan = domParagraph.childNodes[ 0 ];
  85. expect( converter.mapDomToView( domSpan ) ).to.equal( myElement );
  86. } );
  87. } );
  88. describe( 'findCorrespondingViewText()', () => {
  89. it( 'should return UIElement for DOM text inside', () => {
  90. const myElement = new MyUIElement( 'div' );
  91. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  92. const domText = domElement.querySelector( 'span' ).childNodes[ 0 ];
  93. expect( converter.findCorrespondingViewText( domText ) ).to.equal( myElement );
  94. } );
  95. } );
  96. describe( 'getParentUIElement()', () => {
  97. it( 'should return UIElement for DOM children', () => {
  98. const uiElement = new MyUIElement( 'div' );
  99. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  100. const domParagraph = domElement.childNodes[ 0 ];
  101. const domSpan = domParagraph.childNodes[ 0 ];
  102. expect( converter.getParentUIElement( domParagraph ) ).to.equal( uiElement );
  103. expect( converter.getParentUIElement( domSpan ) ).to.equal( uiElement );
  104. } );
  105. it( 'should return null for element itself', () => {
  106. const uiElement = new MyUIElement( 'div' );
  107. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  108. expect( converter.getParentUIElement( domElement ) ).to.be.null;
  109. } );
  110. } );
  111. } );