8
0

view-to-dom.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals Range, DocumentFragment, HTMLElement, document */
  6. /* bender-tags: view, domconverter, browser-only */
  7. import ViewText from '/ckeditor5/engine/view/text.js';
  8. import ViewElement from '/ckeditor5/engine/view/element.js';
  9. import DomConverter from '/ckeditor5/engine/view/domconverter.js';
  10. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  11. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from '/ckeditor5/engine/view/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 support unicode', () => {
  59. const viewText = new ViewText( 'நிலைக்கு' );
  60. const viewP = new ViewElement( 'p', null, viewText );
  61. const domP = converter.viewToDom( viewP, document, { bind: true } );
  62. expect( domP.childNodes.length ).to.equal( 1 );
  63. expect( domP.childNodes[ 0 ].data ).to.equal( 'நிலைக்கு' );
  64. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  65. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  66. } );
  67. it( 'should create tree of DOM elements from view element without children', () => {
  68. const viewImg = new ViewElement( 'img' );
  69. const viewText = new ViewText( 'foo' );
  70. const viewP = new ViewElement( 'p' );
  71. viewP.setAttribute( 'class', 'foo' );
  72. viewP.appendChildren( viewImg );
  73. viewP.appendChildren( viewText );
  74. const domImg = document.createElement( 'img' );
  75. converter.bindElements( domImg, viewImg );
  76. const domP = converter.viewToDom( viewP, document, { withChildren: false } );
  77. expect( domP ).to.be.an.instanceof( HTMLElement );
  78. expect( domP.tagName ).to.equal( 'P' );
  79. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  80. expect( domP.attributes.length ).to.equal( 1 );
  81. expect( domP.childNodes.length ).to.equal( 0 );
  82. expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
  83. } );
  84. it( 'should create DOM document fragment from view document fragment and bind elements', () => {
  85. const viewImg = new ViewElement( 'img' );
  86. const viewText = new ViewText( 'foo' );
  87. const viewFragment = new ViewDocumentFragment();
  88. viewFragment.appendChildren( viewImg );
  89. viewFragment.appendChildren( viewText );
  90. const domFragment = converter.viewToDom( viewFragment, document, { bind: true } );
  91. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  92. expect( domFragment.childNodes.length ).to.equal( 2 );
  93. expect( domFragment.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  94. expect( domFragment.childNodes[ 1 ].data ).to.equal( 'foo' );
  95. expect( converter.getCorrespondingView( domFragment ) ).to.equal( viewFragment );
  96. expect( converter.getCorrespondingView( domFragment.childNodes[ 0 ] ) ).to.equal( viewFragment.getChild( 0 ) );
  97. } );
  98. it( 'should create DOM document fragment from view document without children', () => {
  99. const viewImg = new ViewElement( 'img' );
  100. const viewText = new ViewText( 'foo' );
  101. const viewFragment = new ViewDocumentFragment();
  102. viewFragment.appendChildren( viewImg );
  103. viewFragment.appendChildren( viewText );
  104. const domImg = document.createElement( 'img' );
  105. converter.bindElements( domImg, viewImg );
  106. const domFragment = converter.viewToDom( viewFragment, document, { withChildren: false } );
  107. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  108. expect( domFragment.childNodes.length ).to.equal( 0 );
  109. expect( converter.getCorrespondingView( domFragment ) ).not.to.equal( viewFragment );
  110. } );
  111. it( 'should return already bind document fragment', () => {
  112. const domFragment = document.createDocumentFragment();
  113. const viewFragment = new ViewDocumentFragment();
  114. converter.bindDocumentFragments( domFragment, viewFragment );
  115. const domFragment2 = converter.viewToDom( viewFragment );
  116. expect( domFragment2 ).to.equal( domFragment );
  117. } );
  118. } );
  119. describe( 'viewChildrenToDom', () => {
  120. it( 'should convert children', () => {
  121. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  122. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  123. expect( domChildren.length ).to.equal( 2 );
  124. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  125. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  126. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  127. } );
  128. it( 'should add filler', () => {
  129. const viewP = parse( '<container:p></container:p>' );
  130. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  131. expect( domChildren.length ).to.equal( 1 );
  132. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  133. } );
  134. it( 'should add filler according to fillerPositionOffset', () => {
  135. const viewP = parse( '<container:p>foo</container:p>' );
  136. viewP.getFillerOffset = () => 0;
  137. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  138. expect( domChildren.length ).to.equal( 2 );
  139. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  140. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  141. } );
  142. it( 'should pass options', () => {
  143. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  144. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  145. expect( domChildren.length ).to.equal( 2 );
  146. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  147. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  148. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  149. } );
  150. } );
  151. describe( 'viewPositionToDom', () => {
  152. it( 'should convert the position in the text', () => {
  153. const domFoo = document.createTextNode( 'foo' );
  154. const domP = createElement( document, 'p', null, domFoo );
  155. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  156. converter.bindElements( domP, viewP );
  157. const viewPosition = selection.getFirstPosition();
  158. const domPosition = converter.viewPositionToDom( viewPosition );
  159. expect( domPosition.offset ).to.equal( 2 );
  160. expect( domPosition.parent ).to.equal( domFoo );
  161. } );
  162. it( 'should support unicode', () => {
  163. const domText = document.createTextNode( 'நிலைக்கு' );
  164. const domP = createElement( document, 'p', null, domText );
  165. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  166. converter.bindElements( domP, viewP );
  167. const viewPosition = selection.getFirstPosition();
  168. const domPosition = converter.viewPositionToDom( viewPosition );
  169. expect( domPosition.offset ).to.equal( 4 );
  170. expect( domPosition.parent ).to.equal( domText );
  171. } );
  172. it( 'should convert the position in the empty element', () => {
  173. const domP = createElement( document, 'p' );
  174. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  175. converter.bindElements( domP, viewP );
  176. const viewPosition = selection.getFirstPosition();
  177. const domPosition = converter.viewPositionToDom( viewPosition );
  178. expect( domPosition.offset ).to.equal( 0 );
  179. expect( domPosition.parent ).to.equal( domP );
  180. } );
  181. it( 'should convert the position in the non-empty element', () => {
  182. const domB = createElement( document, 'b', null, 'foo' );
  183. const domP = createElement( document, 'p', null, domB );
  184. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  185. converter.bindElements( domP, viewP );
  186. converter.bindElements( domB, viewP.getChild( 0 ) );
  187. const viewPosition = selection.getFirstPosition();
  188. const domPosition = converter.viewPositionToDom( viewPosition );
  189. expect( domPosition.offset ).to.equal( 1 );
  190. expect( domPosition.parent ).to.equal( domP );
  191. } );
  192. it( 'should convert the position after text', () => {
  193. const domP = createElement( document, 'p', null, 'foo' );
  194. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  195. converter.bindElements( domP, viewP );
  196. const viewPosition = selection.getFirstPosition();
  197. const domPosition = converter.viewPositionToDom( viewPosition );
  198. expect( domPosition.offset ).to.equal( 1 );
  199. expect( domPosition.parent ).to.equal( domP );
  200. } );
  201. it( 'should convert the position before text', () => {
  202. const domP = createElement( document, 'p', null, 'foo' );
  203. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  204. converter.bindElements( domP, viewP );
  205. const viewPosition = selection.getFirstPosition();
  206. const domPosition = converter.viewPositionToDom( viewPosition );
  207. expect( domPosition.offset ).to.equal( 0 );
  208. expect( domPosition.parent ).to.equal( domP );
  209. } );
  210. it( 'should update offset if DOM text node starts with inline filler', () => {
  211. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  212. const domP = createElement( document, 'p', null, domFoo );
  213. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  214. converter.bindElements( domP, viewP );
  215. const viewPosition = selection.getFirstPosition();
  216. const domPosition = converter.viewPositionToDom( viewPosition );
  217. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  218. expect( domPosition.parent ).to.equal( domFoo );
  219. } );
  220. it( 'should move the position to the text node if the position is where inline filler is', () => {
  221. const domFiller = document.createTextNode( INLINE_FILLER );
  222. const domP = createElement( document, 'p', null, domFiller );
  223. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  224. converter.bindElements( domP, viewP );
  225. const viewPosition = selection.getFirstPosition();
  226. const domPosition = converter.viewPositionToDom( viewPosition );
  227. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  228. expect( domPosition.parent ).to.equal( domFiller );
  229. } );
  230. } );
  231. describe( 'viewRangeToDom', () => {
  232. it( 'should convert view range to DOM range', () => {
  233. const domFoo = document.createTextNode( 'foo' );
  234. const domP = createElement( document, 'p', null, domFoo );
  235. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  236. converter.bindElements( domP, viewP );
  237. const viewRange = selection.getFirstRange();
  238. const domRange = converter.viewRangeToDom( viewRange );
  239. expect( domRange ).to.be.instanceof( Range );
  240. expect( domRange.startContainer ).to.equal( domFoo );
  241. expect( domRange.startOffset ).to.equal( 2 );
  242. expect( domRange.endContainer ).to.equal( domP );
  243. expect( domRange.endOffset ).to.equal( 1 );
  244. } );
  245. } );
  246. } );