view-to-dom.js 13 KB

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