8
0

uielement.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /* 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. import ViewDocument from '../../../src/view/document';
  10. describe( 'DOMConverter UIElement integration', () => {
  11. let converter, viewDocument;
  12. function createUIElement( name ) {
  13. const element = new ViewUIElement( viewDocument, name );
  14. element.render = function( domDocument ) {
  15. const root = this.toDomElement( domDocument );
  16. root.innerHTML = '<p><span>foo</span> bar</p>';
  17. return root;
  18. };
  19. return element;
  20. }
  21. beforeEach( () => {
  22. converter = new DomConverter();
  23. viewDocument = new ViewDocument();
  24. } );
  25. describe( 'viewToDom()', () => {
  26. it( 'should create DOM element from UIElement', () => {
  27. const uiElement = new ViewUIElement( viewDocument, 'div' );
  28. const domElement = converter.viewToDom( uiElement, document );
  29. expect( domElement ).to.be.instanceOf( HTMLElement );
  30. } );
  31. it( 'should create DOM structure from UIElement', () => {
  32. const myElement = createUIElement( 'div' );
  33. const domElement = converter.viewToDom( myElement, document );
  34. expect( domElement ).to.be.instanceOf( HTMLElement );
  35. expect( domElement.innerHTML ).to.equal( '<p><span>foo</span> bar</p>' );
  36. } );
  37. it( 'should create DOM structure that all is mapped to single UIElement', () => {
  38. const myElement = createUIElement( 'div' );
  39. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  40. const domParagraph = domElement.childNodes[ 0 ];
  41. expect( converter.mapDomToView( domElement ) ).to.equal( myElement );
  42. expect( converter.mapDomToView( domParagraph ) ).to.equal( myElement );
  43. expect( converter.mapDomToView( domParagraph.childNodes[ 0 ] ) ).to.equal( myElement );
  44. } );
  45. } );
  46. describe( 'domToView()', () => {
  47. it( 'should return UIElement itself', () => {
  48. const uiElement = createUIElement( 'div' );
  49. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  50. expect( converter.domToView( domElement ) ).to.equal( uiElement );
  51. } );
  52. it( 'should return UIElement for nodes inside', () => {
  53. const uiElement = createUIElement( 'div' );
  54. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  55. const domParagraph = domElement.childNodes[ 0 ];
  56. const domSpan = domParagraph.childNodes[ 0 ];
  57. expect( converter.domToView( domParagraph ) ).to.equal( uiElement );
  58. expect( converter.domToView( domSpan ) ).to.be.equal( uiElement );
  59. expect( converter.domToView( domParagraph.childNodes[ 0 ] ) ).equal( uiElement );
  60. expect( converter.domToView( domSpan.childNodes[ 0 ] ) ).equal( uiElement );
  61. } );
  62. } );
  63. describe( 'domPositionToView()', () => {
  64. it( 'should convert position inside UIElement to position before it', () => {
  65. const uiElement = createUIElement( 'h1' );
  66. const container = new ViewContainer( viewDocument, 'div', null, [ new ViewContainer( viewDocument, 'div' ), uiElement ] );
  67. const domContainer = converter.viewToDom( container, document, { bind: true } );
  68. const viewPosition = converter.domPositionToView( domContainer.childNodes[ 1 ], 0 );
  69. expect( viewPosition.parent ).to.equal( container );
  70. expect( viewPosition.offset ).to.equal( 1 );
  71. } );
  72. it( 'should convert position inside UIElement children to position before UIElement', () => {
  73. const uiElement = createUIElement( 'h1' );
  74. const container = new ViewContainer( viewDocument, 'div', null, [ new ViewContainer( viewDocument, 'div' ), uiElement ] );
  75. const domContainer = converter.viewToDom( container, document, { bind: true } );
  76. const viewPosition = converter.domPositionToView( domContainer.childNodes[ 1 ].childNodes[ 0 ], 1 );
  77. expect( viewPosition.parent ).to.equal( container );
  78. expect( viewPosition.offset ).to.equal( 1 );
  79. } );
  80. } );
  81. describe( 'mapDomToView()', () => {
  82. it( 'should return UIElement for DOM elements inside', () => {
  83. const myElement = createUIElement( 'div' );
  84. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  85. expect( converter.mapDomToView( domElement ) ).to.equal( myElement );
  86. const domParagraph = domElement.childNodes[ 0 ];
  87. expect( converter.mapDomToView( domParagraph ) ).to.equal( myElement );
  88. const domSpan = domParagraph.childNodes[ 0 ];
  89. expect( converter.mapDomToView( domSpan ) ).to.equal( myElement );
  90. } );
  91. } );
  92. describe( 'findCorrespondingViewText()', () => {
  93. it( 'should return UIElement for DOM text inside', () => {
  94. const myElement = createUIElement( 'div' );
  95. const domElement = converter.viewToDom( myElement, document, { bind: true } );
  96. const domText = domElement.querySelector( 'span' ).childNodes[ 0 ];
  97. expect( converter.findCorrespondingViewText( domText ) ).to.equal( myElement );
  98. } );
  99. } );
  100. describe( 'getParentUIElement()', () => {
  101. it( 'should return UIElement for DOM children', () => {
  102. const uiElement = createUIElement( 'div' );
  103. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  104. const domParagraph = domElement.childNodes[ 0 ];
  105. const domSpan = domParagraph.childNodes[ 0 ];
  106. expect( converter.getParentUIElement( domParagraph ) ).to.equal( uiElement );
  107. expect( converter.getParentUIElement( domSpan ) ).to.equal( uiElement );
  108. } );
  109. it( 'should return null for element itself', () => {
  110. const uiElement = createUIElement( 'div' );
  111. const domElement = converter.viewToDom( uiElement, document, { bind: true } );
  112. expect( converter.getParentUIElement( domElement ) ).to.be.null;
  113. } );
  114. } );
  115. } );