8
0

view-to-dom.js 12 KB

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