8
0

view-to-dom.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, domconverter, browser-only */
  6. import ViewText from '/ckeditor5/engine/view/text.js';
  7. import ViewElement from '/ckeditor5/engine/view/element.js';
  8. import DomConverter from '/ckeditor5/engine/view/domconverter.js';
  9. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  10. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from '/ckeditor5/engine/view/filler.js';
  11. import { parse } from '/tests/engine/_utils/view.js';
  12. import createElement from '/ckeditor5/utils/dom/createelement.js';
  13. describe( 'DomConverter', () => {
  14. let converter;
  15. before( () => {
  16. converter = new DomConverter();
  17. } );
  18. describe( 'viewToDom', () => {
  19. it( 'should create tree of DOM elements from view elements', () => {
  20. const viewImg = new ViewElement( 'img' );
  21. const viewText = new ViewText( 'foo' );
  22. const viewP = new ViewElement( 'p' );
  23. viewP.setAttribute( 'class', 'foo' );
  24. viewP.appendChildren( viewImg );
  25. viewP.appendChildren( viewText );
  26. const domImg = document.createElement( 'img' );
  27. converter.bindElements( domImg, viewImg );
  28. const domP = converter.viewToDom( viewP, document );
  29. expect( domP ).to.be.an.instanceof( HTMLElement );
  30. expect( domP.tagName ).to.equal( 'P' );
  31. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  32. expect( domP.attributes.length ).to.equal( 1 );
  33. expect( domP.childNodes.length ).to.equal( 2 );
  34. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  35. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  36. expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
  37. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewImg );
  38. } );
  39. it( 'should create tree of DOM elements from view elements and bind elements', () => {
  40. const viewImg = new ViewElement( 'img' );
  41. const viewText = new ViewText( 'foo' );
  42. const viewP = new ViewElement( 'p' );
  43. viewP.setAttribute( 'class', 'foo' );
  44. viewP.appendChildren( viewImg );
  45. viewP.appendChildren( viewText );
  46. const domP = converter.viewToDom( viewP, document, { bind: true } );
  47. expect( domP ).to.be.an.instanceof( HTMLElement );
  48. expect( domP.tagName ).to.equal( 'P' );
  49. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  50. expect( domP.attributes.length ).to.equal( 1 );
  51. expect( domP.childNodes.length ).to.equal( 2 );
  52. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  53. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  54. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  55. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  56. } );
  57. it( 'should create tree of DOM elements from view element without children', () => {
  58. const viewImg = new ViewElement( 'img' );
  59. const viewText = new ViewText( 'foo' );
  60. const viewP = new ViewElement( 'p' );
  61. viewP.setAttribute( 'class', 'foo' );
  62. viewP.appendChildren( viewImg );
  63. viewP.appendChildren( viewText );
  64. const domImg = document.createElement( 'img' );
  65. converter.bindElements( domImg, viewImg );
  66. const domP = converter.viewToDom( viewP, document, { withChildren: false } );
  67. expect( domP ).to.be.an.instanceof( HTMLElement );
  68. expect( domP.tagName ).to.equal( 'P' );
  69. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  70. expect( domP.attributes.length ).to.equal( 1 );
  71. expect( domP.childNodes.length ).to.equal( 0 );
  72. expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
  73. } );
  74. it( 'should create DOM document fragment from view document fragment and bind elements', () => {
  75. const viewImg = new ViewElement( 'img' );
  76. const viewText = new ViewText( 'foo' );
  77. const viewFragment = new ViewDocumentFragment();
  78. viewFragment.appendChildren( viewImg );
  79. viewFragment.appendChildren( viewText );
  80. const domFragment = converter.viewToDom( viewFragment, document, { bind: true } );
  81. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  82. expect( domFragment.childNodes.length ).to.equal( 2 );
  83. expect( domFragment.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  84. expect( domFragment.childNodes[ 1 ].data ).to.equal( 'foo' );
  85. expect( converter.getCorrespondingView( domFragment ) ).to.equal( viewFragment );
  86. expect( converter.getCorrespondingView( domFragment.childNodes[ 0 ] ) ).to.equal( viewFragment.getChild( 0 ) );
  87. } );
  88. it( 'should create DOM document fragment from view document without children', () => {
  89. const viewImg = new ViewElement( 'img' );
  90. const viewText = new ViewText( 'foo' );
  91. const viewFragment = new ViewDocumentFragment();
  92. viewFragment.appendChildren( viewImg );
  93. viewFragment.appendChildren( viewText );
  94. const domImg = document.createElement( 'img' );
  95. converter.bindElements( domImg, viewImg );
  96. const domFragment = converter.viewToDom( viewFragment, document, { withChildren: false } );
  97. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  98. expect( domFragment.childNodes.length ).to.equal( 0 );
  99. expect( converter.getCorrespondingView( domFragment ) ).not.to.equal( viewFragment );
  100. } );
  101. it( 'should return already bind document fragment', () => {
  102. const domFragment = document.createDocumentFragment();
  103. const viewFragment = new ViewDocumentFragment();
  104. converter.bindDocumentFragments( domFragment, viewFragment );
  105. const domFragment2 = converter.viewToDom( viewFragment );
  106. expect( domFragment2 ).to.equal( domFragment );
  107. } );
  108. } );
  109. describe( 'viewChildrenToDom', () => {
  110. it( 'should convert children', () => {
  111. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  112. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  113. expect( domChildren.length ).to.equal( 2 );
  114. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  115. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  116. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  117. } );
  118. it( 'should add filler', () => {
  119. const viewP = parse( '<container:p></container:p>' );
  120. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  121. expect( domChildren.length ).to.equal( 1 );
  122. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  123. } );
  124. it( 'should add filler according to fillerPositionOffset', () => {
  125. const viewP = parse( '<container:p>foo</container:p>' );
  126. viewP.getFillerOffset = () => 0;
  127. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  128. expect( domChildren.length ).to.equal( 2 );
  129. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  130. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  131. } );
  132. it( 'should pass options', () => {
  133. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  134. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  135. expect( domChildren.length ).to.equal( 2 );
  136. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  137. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  138. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  139. } );
  140. } );
  141. describe( 'viewPositionToDom', () => {
  142. it( 'should convert the position in the text', () => {
  143. const domFoo = document.createTextNode( 'foo' );
  144. const domP = createElement( document, 'p', null, domFoo );
  145. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  146. converter.bindElements( domP, viewP );
  147. const viewPosition = selection.getFirstPosition();
  148. const domPosition = converter.viewPositionToDom( viewPosition );
  149. expect( domPosition.offset ).to.equal( 2 );
  150. expect( domPosition.parent ).to.equal( domFoo );
  151. } );
  152. it( 'should convert the position in the empty element', () => {
  153. const domP = createElement( document, 'p' );
  154. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  155. converter.bindElements( domP, viewP );
  156. const viewPosition = selection.getFirstPosition();
  157. const domPosition = converter.viewPositionToDom( viewPosition );
  158. expect( domPosition.offset ).to.equal( 0 );
  159. expect( domPosition.parent ).to.equal( domP );
  160. } );
  161. it( 'should convert the position in the non-empty element', () => {
  162. const domB = createElement( document, 'b', null, 'foo' );
  163. const domP = createElement( document, 'p', null, domB );
  164. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  165. converter.bindElements( domP, viewP );
  166. converter.bindElements( domB, viewP.getChild( 0 ) );
  167. const viewPosition = selection.getFirstPosition();
  168. const domPosition = converter.viewPositionToDom( viewPosition );
  169. expect( domPosition.offset ).to.equal( 1 );
  170. expect( domPosition.parent ).to.equal( domP );
  171. } );
  172. it( 'should convert the position after text', () => {
  173. const domP = createElement( document, 'p', null, 'foo' );
  174. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  175. converter.bindElements( domP, viewP );
  176. const viewPosition = selection.getFirstPosition();
  177. const domPosition = converter.viewPositionToDom( viewPosition );
  178. expect( domPosition.offset ).to.equal( 1 );
  179. expect( domPosition.parent ).to.equal( domP );
  180. } );
  181. it( 'should convert the position before text', () => {
  182. const domP = createElement( document, 'p', null, 'foo' );
  183. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  184. converter.bindElements( domP, viewP );
  185. const viewPosition = selection.getFirstPosition();
  186. const domPosition = converter.viewPositionToDom( viewPosition );
  187. expect( domPosition.offset ).to.equal( 0 );
  188. expect( domPosition.parent ).to.equal( domP );
  189. } );
  190. it( 'should update offset if DOM text node starts with inline filler', () => {
  191. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  192. const domP = createElement( document, 'p', null, domFoo );
  193. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  194. converter.bindElements( domP, viewP );
  195. const viewPosition = selection.getFirstPosition();
  196. const domPosition = converter.viewPositionToDom( viewPosition );
  197. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  198. expect( domPosition.parent ).to.equal( domFoo );
  199. } );
  200. it( 'should move the position to the text node if the position is where inline filler is', () => {
  201. const domFiller = document.createTextNode( INLINE_FILLER );
  202. const domP = createElement( document, 'p', null, domFiller );
  203. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  204. converter.bindElements( domP, viewP );
  205. const viewPosition = selection.getFirstPosition();
  206. const domPosition = converter.viewPositionToDom( viewPosition );
  207. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  208. expect( domPosition.parent ).to.equal( domFiller );
  209. } );
  210. } );
  211. describe( 'viewRangeToDom', () => {
  212. it( 'should convert view range to DOM range', () => {
  213. const domFoo = document.createTextNode( 'foo' );
  214. const domP = createElement( document, 'p', null, domFoo );
  215. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  216. converter.bindElements( domP, viewP );
  217. const viewRange = selection.getFirstRange();
  218. const domRange = converter.viewRangeToDom( viewRange );
  219. expect( domRange ).to.be.instanceof( Range );
  220. expect( domRange.startContainer ).to.equal( domFoo );
  221. expect( domRange.startOffset ).to.equal( 2 );
  222. expect( domRange.endContainer ).to.equal( domP );
  223. expect( domRange.endOffset ).to.equal( 1 );
  224. } );
  225. } );
  226. } );