view-to-dom.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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, Text */
  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 ViewPosition from 'ckeditor5/engine/view/position.js';
  10. import ViewContainerElement from 'ckeditor5/engine/view/containerelement.js';
  11. import ViewAttributeElement from 'ckeditor5/engine/view/attributeelement.js';
  12. import DomConverter from 'ckeditor5/engine/view/domconverter.js';
  13. import ViewDocumentFragment from 'ckeditor5/engine/view/documentfragment.js';
  14. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from 'ckeditor5/engine/view/filler.js';
  15. import { parse } from 'ckeditor5/engine/dev-utils/view.js';
  16. import createElement from 'ckeditor5/utils/dom/createelement.js';
  17. describe( 'DomConverter', () => {
  18. let converter;
  19. before( () => {
  20. converter = new DomConverter();
  21. } );
  22. describe( 'viewToDom', () => {
  23. it( 'should create tree of DOM elements from view elements', () => {
  24. const viewImg = new ViewElement( 'img' );
  25. const viewText = new ViewText( 'foo' );
  26. const viewP = new ViewElement( 'p' );
  27. viewP.setAttribute( 'class', 'foo' );
  28. viewP.appendChildren( viewImg );
  29. viewP.appendChildren( viewText );
  30. const domImg = document.createElement( 'img' );
  31. converter.bindElements( domImg, viewImg );
  32. const domP = converter.viewToDom( viewP, document );
  33. expect( domP ).to.be.an.instanceof( HTMLElement );
  34. expect( domP.tagName ).to.equal( 'P' );
  35. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  36. expect( domP.attributes.length ).to.equal( 1 );
  37. expect( domP.childNodes.length ).to.equal( 2 );
  38. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  39. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  40. expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
  41. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewImg );
  42. } );
  43. it( 'should create tree of DOM elements from view elements and bind elements', () => {
  44. const viewImg = new ViewElement( 'img' );
  45. const viewText = new ViewText( 'foo' );
  46. const viewP = new ViewElement( 'p' );
  47. viewP.setAttribute( 'class', 'foo' );
  48. viewP.appendChildren( viewImg );
  49. viewP.appendChildren( viewText );
  50. const domP = converter.viewToDom( viewP, document, { bind: true } );
  51. expect( domP ).to.be.an.instanceof( HTMLElement );
  52. expect( domP.tagName ).to.equal( 'P' );
  53. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  54. expect( domP.attributes.length ).to.equal( 1 );
  55. expect( domP.childNodes.length ).to.equal( 2 );
  56. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  57. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  58. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  59. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  60. } );
  61. it( 'should support unicode', () => {
  62. const viewText = new ViewText( 'நிலைக்கு' );
  63. const viewP = new ViewElement( 'p', null, viewText );
  64. const domP = converter.viewToDom( viewP, document, { bind: true } );
  65. expect( domP.childNodes.length ).to.equal( 1 );
  66. expect( domP.childNodes[ 0 ].data ).to.equal( 'நிலைக்கு' );
  67. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  68. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  69. } );
  70. it( 'should create tree of DOM elements from view element without children', () => {
  71. const viewImg = new ViewElement( 'img' );
  72. const viewText = new ViewText( 'foo' );
  73. const viewP = new ViewElement( 'p' );
  74. viewP.setAttribute( 'class', 'foo' );
  75. viewP.appendChildren( viewImg );
  76. viewP.appendChildren( viewText );
  77. const domImg = document.createElement( 'img' );
  78. converter.bindElements( domImg, viewImg );
  79. const domP = converter.viewToDom( viewP, document, { withChildren: false } );
  80. expect( domP ).to.be.an.instanceof( HTMLElement );
  81. expect( domP.tagName ).to.equal( 'P' );
  82. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  83. expect( domP.attributes.length ).to.equal( 1 );
  84. expect( domP.childNodes.length ).to.equal( 0 );
  85. expect( converter.getCorrespondingView( domP ) ).not.to.equal( viewP );
  86. } );
  87. it( 'should create DOM document fragment from view document fragment and bind elements', () => {
  88. const viewImg = new ViewElement( 'img' );
  89. const viewText = new ViewText( 'foo' );
  90. const viewFragment = new ViewDocumentFragment();
  91. viewFragment.appendChildren( viewImg );
  92. viewFragment.appendChildren( viewText );
  93. const domFragment = converter.viewToDom( viewFragment, document, { bind: true } );
  94. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  95. expect( domFragment.childNodes.length ).to.equal( 2 );
  96. expect( domFragment.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  97. expect( domFragment.childNodes[ 1 ].data ).to.equal( 'foo' );
  98. expect( converter.getCorrespondingView( domFragment ) ).to.equal( viewFragment );
  99. expect( converter.getCorrespondingView( domFragment.childNodes[ 0 ] ) ).to.equal( viewFragment.getChild( 0 ) );
  100. } );
  101. it( 'should create DOM document fragment from view document without children', () => {
  102. const viewImg = new ViewElement( 'img' );
  103. const viewText = new ViewText( 'foo' );
  104. const viewFragment = new ViewDocumentFragment();
  105. viewFragment.appendChildren( viewImg );
  106. viewFragment.appendChildren( viewText );
  107. const domImg = document.createElement( 'img' );
  108. converter.bindElements( domImg, viewImg );
  109. const domFragment = converter.viewToDom( viewFragment, document, { withChildren: false } );
  110. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  111. expect( domFragment.childNodes.length ).to.equal( 0 );
  112. expect( converter.getCorrespondingView( domFragment ) ).not.to.equal( viewFragment );
  113. } );
  114. it( 'should return already bind document fragment', () => {
  115. const domFragment = document.createDocumentFragment();
  116. const viewFragment = new ViewDocumentFragment();
  117. converter.bindDocumentFragments( domFragment, viewFragment );
  118. const domFragment2 = converter.viewToDom( viewFragment, document );
  119. expect( domFragment2 ).to.equal( domFragment );
  120. } );
  121. it( 'should create DOM text node from view text node', () => {
  122. const viewTextNode = new ViewText( 'foo' );
  123. const domTextNode = converter.viewToDom( viewTextNode, document );
  124. expect( domTextNode ).to.be.instanceof( Text );
  125. expect( domTextNode.data ).to.equal( 'foo' );
  126. } );
  127. describe( 'it should convert spaces to  ', () => {
  128. it( 'at the beginning of each container element', () => {
  129. const viewDiv = new ViewContainerElement( 'div', null, [
  130. new ViewContainerElement( 'p', null, new ViewText( ' foo' ) ),
  131. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  132. new ViewContainerElement( 'p', null, new ViewText( ' xxx' ) )
  133. ] );
  134. const domDiv = converter.viewToDom( viewDiv, document );
  135. expect( domDiv.innerHTML ).to.equal( '<p>&nbsp;foo</p><p>bar</p><p>&nbsp;xxx</p>' );
  136. } );
  137. it( 'at the end of each container element', () => {
  138. const viewDiv = new ViewContainerElement( 'div', null, [
  139. new ViewContainerElement( 'p', null, new ViewText( 'foo ' ) ),
  140. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  141. new ViewContainerElement( 'p', null, new ViewText( 'xxx ' ) )
  142. ] );
  143. const domDiv = converter.viewToDom( viewDiv, document );
  144. expect( domDiv.innerHTML ).to.equal( '<p>foo&nbsp;</p><p>bar</p><p>xxx&nbsp;</p>' );
  145. } );
  146. it( 'when there are multiple spaces next to each other or between attribute elements', () => {
  147. const viewDiv = new ViewContainerElement( 'div', null, [
  148. new ViewText( 'x x x x ' ),
  149. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  150. new ViewAttributeElement( 'i', null,
  151. new ViewAttributeElement( 'b', null,
  152. new ViewAttributeElement( 'u' , null, new ViewText( ' x' ) )
  153. )
  154. )
  155. ] );
  156. const domDiv = converter.viewToDom( viewDiv, document );
  157. expect( domDiv.innerHTML ).to.equal( 'x &nbsp;x &nbsp; x x <b>&nbsp;x </b><i><b><u>&nbsp;x</u></b></i>' );
  158. } );
  159. it( 'all together', () => {
  160. const viewDiv = new ViewContainerElement( 'div', null, [
  161. new ViewContainerElement( 'p', null, [
  162. new ViewText( ' x x x x ' ),
  163. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  164. new ViewAttributeElement( 'i', null,
  165. new ViewAttributeElement( 'b', null,
  166. new ViewAttributeElement( 'u' , null, new ViewText( ' x ' ) )
  167. )
  168. )
  169. ] ),
  170. new ViewContainerElement( 'p', null, new ViewText( ' x ' ) )
  171. ] );
  172. const domDiv = converter.viewToDom( viewDiv, document );
  173. expect( domDiv.innerHTML ).to.equal(
  174. '<p>&nbsp;x &nbsp;x &nbsp; x x <b>&nbsp;x </b><i><b><u>&nbsp;x&nbsp;</u></b></i></p><p>&nbsp; x &nbsp;</p>'
  175. );
  176. } );
  177. function test( inputTexts, output ) {
  178. if ( typeof inputTexts == 'string' ) {
  179. inputTexts = [ inputTexts ];
  180. }
  181. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  182. const viewElement = new ViewContainerElement( 'p' );
  183. for ( let text of inputTexts ) {
  184. viewElement.appendChildren( new ViewText( text.replace( /_/g, '\u00A0' ) ) );
  185. }
  186. const domElement = converter.viewToDom( viewElement, document );
  187. const data = domElement.innerHTML.replace( /&nbsp;/g, '_' );
  188. expect( data ).to.equal( output );
  189. } );
  190. }
  191. // At the beginning.
  192. test( ' x', '_x' );
  193. test( ' x', '_ x' );
  194. test( ' x', '_ _x' );
  195. test( ' x', '_ _ x' );
  196. // At the end.
  197. test( 'x ', 'x_' );
  198. test( 'x ', 'x _' );
  199. test( 'x ', 'x_ _' );
  200. test( 'x ', 'x _ _' );
  201. // In the middle.
  202. test( 'x x', 'x x' );
  203. test( 'x x', 'x _x' );
  204. test( 'x x', 'x _ x' );
  205. test( 'x x', 'x _ _x' );
  206. // Complex.
  207. test( ' x ', '_x_' );
  208. test( ' x x ', '_ x _x _' );
  209. test( ' x x ', '_ _x x _' );
  210. test( ' x x ', '_ _x x_ _' );
  211. test( ' x x ', '_ _x _ _x_' );
  212. test( ' ', '_' );
  213. // With hard &nbsp;
  214. test( '_x', '_x' );
  215. test( ' _x', '__x' );
  216. test( ' _x', '_ _x' );
  217. test( ' __x', '___x' );
  218. test( '___x', '___x' );
  219. test( '_ _x', '_ _x' );
  220. test( ' _ x', '__ x' );
  221. test( ' _x', '_ _x' );
  222. test( 'x_', 'x_' );
  223. test( 'x_ ', 'x__' );
  224. test( 'x_ ', 'x_ _' );
  225. test( 'x__ ', 'x___' );
  226. test( 'x___', 'x___' );
  227. test( 'x_ _', 'x_ _' );
  228. test( 'x _ ', 'x __' );
  229. test( 'x _', 'x __' );
  230. test( 'x_x', 'x_x' );
  231. test( 'x___x', 'x___x' );
  232. test( 'x__ x', 'x__ x' );
  233. test( 'x_ x', 'x_ _x' );
  234. test( 'x _x', 'x __x' );
  235. test( 'x __x', 'x __x' );
  236. test( 'x _ x', 'x _ x' );
  237. test( 'x _ x', 'x __ _x' );
  238. // Two text nodes.
  239. test( [ 'x', 'y' ], 'xy' );
  240. test( [ 'x ', 'y' ], 'x y' );
  241. test( [ 'x ', 'y' ], 'x _y' );
  242. test( [ 'x ', 'y' ], 'x _ y' );
  243. test( [ 'x ', 'y' ], 'x _ _y' );
  244. test( [ 'x', ' y' ], 'x y' );
  245. test( [ 'x ', ' y' ], 'x _y' );
  246. test( [ 'x ', ' y' ], 'x_ _y' );
  247. test( [ 'x ', ' y' ], 'x _ _y' );
  248. test( [ 'x ', ' y' ], 'x_ _ _y' );
  249. test( [ 'x', '_y' ], 'x_y' );
  250. test( [ 'x ', '_y' ], 'x _y' );
  251. test( [ 'x ', '_y' ], 'x_ _y' );
  252. test( [ 'x ', '_y' ], 'x _ _y' );
  253. test( [ 'x ', '_y' ], 'x_ _ _y' );
  254. test( [ 'x', ' y' ], 'x _y' );
  255. test( [ 'x ', ' y' ], 'x _ y' );
  256. test( [ 'x ', ' y' ], 'x_ _ y' );
  257. test( [ 'x ', ' y' ], 'x _ _ y' );
  258. test( [ 'x ', ' y' ], 'x_ _ _ y' );
  259. test( [ 'x', ' y' ], 'x _ y' );
  260. test( [ 'x ', ' y' ], 'x _ _y' );
  261. test( [ 'x ', ' y' ], 'x_ _ _y' );
  262. test( [ 'x ', ' y' ], 'x _ _ _y' );
  263. test( [ 'x ', ' y' ], 'x_ _ _ _y' );
  264. it( 'not in preformatted blocks', () => {
  265. const viewDiv = new ViewContainerElement( 'pre', null, new ViewText( ' foo ' ) );
  266. const domDiv = converter.viewToDom( viewDiv, document );
  267. expect( domDiv.innerHTML ).to.equal( ' foo ' );
  268. } );
  269. } );
  270. } );
  271. describe( 'viewChildrenToDom', () => {
  272. it( 'should convert children', () => {
  273. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  274. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  275. expect( domChildren.length ).to.equal( 2 );
  276. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  277. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  278. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  279. } );
  280. it( 'should add filler', () => {
  281. const viewP = parse( '<container:p></container:p>' );
  282. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  283. expect( domChildren.length ).to.equal( 1 );
  284. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  285. } );
  286. it( 'should add filler according to fillerPositionOffset', () => {
  287. const viewP = parse( '<container:p>foo</container:p>' );
  288. viewP.getFillerOffset = () => 0;
  289. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  290. expect( domChildren.length ).to.equal( 2 );
  291. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  292. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  293. } );
  294. it( 'should pass options', () => {
  295. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  296. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  297. expect( domChildren.length ).to.equal( 2 );
  298. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  299. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  300. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  301. } );
  302. } );
  303. describe( 'viewPositionToDom', () => {
  304. it( 'should convert the position in the text', () => {
  305. const domFoo = document.createTextNode( 'foo' );
  306. const domP = createElement( document, 'p', null, domFoo );
  307. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  308. converter.bindElements( domP, viewP );
  309. const viewPosition = selection.getFirstPosition();
  310. const domPosition = converter.viewPositionToDom( viewPosition );
  311. expect( domPosition.offset ).to.equal( 2 );
  312. expect( domPosition.parent ).to.equal( domFoo );
  313. } );
  314. it( 'should support unicode', () => {
  315. const domText = document.createTextNode( 'நிலைக்கு' );
  316. const domP = createElement( document, 'p', null, domText );
  317. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  318. converter.bindElements( domP, viewP );
  319. const viewPosition = selection.getFirstPosition();
  320. const domPosition = converter.viewPositionToDom( viewPosition );
  321. expect( domPosition.offset ).to.equal( 4 );
  322. expect( domPosition.parent ).to.equal( domText );
  323. } );
  324. it( 'should convert the position in the empty element', () => {
  325. const domP = createElement( document, 'p' );
  326. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  327. converter.bindElements( domP, viewP );
  328. const viewPosition = selection.getFirstPosition();
  329. const domPosition = converter.viewPositionToDom( viewPosition );
  330. expect( domPosition.offset ).to.equal( 0 );
  331. expect( domPosition.parent ).to.equal( domP );
  332. } );
  333. it( 'should convert the position in the non-empty element', () => {
  334. const domB = createElement( document, 'b', null, 'foo' );
  335. const domP = createElement( document, 'p', null, domB );
  336. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  337. converter.bindElements( domP, viewP );
  338. converter.bindElements( domB, viewP.getChild( 0 ) );
  339. const viewPosition = selection.getFirstPosition();
  340. const domPosition = converter.viewPositionToDom( viewPosition );
  341. expect( domPosition.offset ).to.equal( 1 );
  342. expect( domPosition.parent ).to.equal( domP );
  343. } );
  344. it( 'should convert the position after text', () => {
  345. const domP = createElement( document, 'p', null, 'foo' );
  346. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  347. converter.bindElements( domP, viewP );
  348. const viewPosition = selection.getFirstPosition();
  349. const domPosition = converter.viewPositionToDom( viewPosition );
  350. expect( domPosition.offset ).to.equal( 1 );
  351. expect( domPosition.parent ).to.equal( domP );
  352. } );
  353. it( 'should convert the position before text', () => {
  354. const domP = createElement( document, 'p', null, 'foo' );
  355. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  356. converter.bindElements( domP, viewP );
  357. const viewPosition = selection.getFirstPosition();
  358. const domPosition = converter.viewPositionToDom( viewPosition );
  359. expect( domPosition.offset ).to.equal( 0 );
  360. expect( domPosition.parent ).to.equal( domP );
  361. } );
  362. it( 'should update offset if DOM text node starts with inline filler', () => {
  363. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  364. const domP = createElement( document, 'p', null, domFoo );
  365. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  366. converter.bindElements( domP, viewP );
  367. const viewPosition = selection.getFirstPosition();
  368. const domPosition = converter.viewPositionToDom( viewPosition );
  369. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  370. expect( domPosition.parent ).to.equal( domFoo );
  371. } );
  372. it( 'should move the position to the text node if the position is where inline filler is', () => {
  373. const domFiller = document.createTextNode( INLINE_FILLER );
  374. const domP = createElement( document, 'p', null, domFiller );
  375. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  376. converter.bindElements( domP, viewP );
  377. const viewPosition = selection.getFirstPosition();
  378. const domPosition = converter.viewPositionToDom( viewPosition );
  379. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  380. expect( domPosition.parent ).to.equal( domFiller );
  381. } );
  382. it( 'should return null if view position is after a view element that has not been rendered to DOM', () => {
  383. const domP = createElement( document, 'p', null );
  384. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  385. converter.bindElements( domP, viewP );
  386. const viewPosition = selection.getFirstPosition();
  387. const domPosition = converter.viewPositionToDom( viewPosition );
  388. expect( domPosition ).to.equal( null );
  389. } );
  390. it( 'should return null if view position is in a view text node that has not been rendered to DOM', () => {
  391. const viewText = new ViewText( 'foo' );
  392. const viewPosition = new ViewPosition( viewText, 1 );
  393. const domPosition = converter.viewPositionToDom( viewPosition );
  394. expect( domPosition ).to.equal( null );
  395. } );
  396. it( 'should return null if view position is in a view element that has not been rendered to DOM', () => {
  397. const viewElement = new ViewContainerElement( 'div' );
  398. const viewPosition = new ViewPosition( viewElement, 0 );
  399. const domPosition = converter.viewPositionToDom( viewPosition );
  400. expect( domPosition ).to.equal( null );
  401. } );
  402. } );
  403. describe( 'viewRangeToDom', () => {
  404. it( 'should convert view range to DOM range', () => {
  405. const domFoo = document.createTextNode( 'foo' );
  406. const domP = createElement( document, 'p', null, domFoo );
  407. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  408. converter.bindElements( domP, viewP );
  409. const viewRange = selection.getFirstRange();
  410. const domRange = converter.viewRangeToDom( viewRange );
  411. expect( domRange ).to.be.instanceof( Range );
  412. expect( domRange.startContainer ).to.equal( domFoo );
  413. expect( domRange.startOffset ).to.equal( 2 );
  414. expect( domRange.endContainer ).to.equal( domP );
  415. expect( domRange.endOffset ).to.equal( 1 );
  416. } );
  417. } );
  418. } );