8
0

uielement.js 5.8 KB

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