view-to-dom.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals Range, DocumentFragment, HTMLElement, document, Text */
  6. import ViewText from '../../../src/view/text';
  7. import ViewElement from '../../../src/view/element';
  8. import ViewPosition from '../../../src/view/position';
  9. import ViewContainerElement from '../../../src/view/containerelement';
  10. import ViewAttributeElement from '../../../src/view/attributeelement';
  11. import DomConverter from '../../../src/view/domconverter';
  12. import ViewDocumentFragment from '../../../src/view/documentfragment';
  13. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from '../../../src/view/filler';
  14. import { parse } from '../../../src/dev-utils/view';
  15. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  16. describe( 'DomConverter', () => {
  17. let converter;
  18. before( () => {
  19. converter = new DomConverter();
  20. } );
  21. describe( 'viewToDom()', () => {
  22. it( 'should create tree of DOM elements from view elements', () => {
  23. const viewImg = new ViewElement( 'img' );
  24. const viewText = new ViewText( 'foo' );
  25. const viewP = new ViewElement( 'p', { class: 'foo' } );
  26. viewP.appendChildren( viewImg );
  27. viewP.appendChildren( viewText );
  28. const domImg = document.createElement( 'img' );
  29. converter.bindElements( domImg, viewImg );
  30. const domP = converter.viewToDom( viewP, document );
  31. expect( domP ).to.be.an.instanceof( HTMLElement );
  32. expect( domP.tagName ).to.equal( 'P' );
  33. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  34. expect( domP.attributes.length ).to.equal( 1 );
  35. expect( domP.childNodes.length ).to.equal( 2 );
  36. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  37. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  38. expect( converter.mapDomToView( domP ) ).not.to.equal( viewP );
  39. expect( converter.mapDomToView( domP.childNodes[ 0 ] ) ).to.equal( viewImg );
  40. } );
  41. it( 'should create tree of DOM elements from view elements and bind elements', () => {
  42. const viewImg = new ViewElement( 'img' );
  43. const viewText = new ViewText( 'foo' );
  44. const viewP = new ViewElement( 'p', { 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.mapDomToView( domP ) ).to.equal( viewP );
  56. expect( converter.mapDomToView( 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.mapDomToView( domP ) ).to.equal( viewP );
  65. expect( converter.findCorrespondingViewText( 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', { 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.mapDomToView( 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.mapDomToView( domFragment ) ).to.equal( viewFragment );
  95. expect( converter.mapDomToView( 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.mapDomToView( 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, document );
  115. expect( domFragment2 ).to.equal( domFragment );
  116. } );
  117. it( 'should create DOM text node from view text node', () => {
  118. const viewTextNode = new ViewText( 'foo' );
  119. const domTextNode = converter.viewToDom( viewTextNode, document );
  120. expect( domTextNode ).to.be.instanceof( Text );
  121. expect( domTextNode.data ).to.equal( 'foo' );
  122. } );
  123. describe( 'it should convert spaces to  ', () => {
  124. it( 'at the beginning of each container element', () => {
  125. const viewDiv = new ViewContainerElement( 'div', null, [
  126. new ViewContainerElement( 'p', null, new ViewText( ' foo' ) ),
  127. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  128. new ViewContainerElement( 'p', null, new ViewText( ' xxx' ) )
  129. ] );
  130. const domDiv = converter.viewToDom( viewDiv, document );
  131. expect( domDiv.innerHTML ).to.equal( '<p>&nbsp;foo</p><p>bar</p><p>&nbsp;xxx</p>' );
  132. } );
  133. it( 'at the end of each container element', () => {
  134. const viewDiv = new ViewContainerElement( 'div', null, [
  135. new ViewContainerElement( 'p', null, new ViewText( 'foo ' ) ),
  136. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  137. new ViewContainerElement( 'p', null, new ViewText( 'xxx ' ) )
  138. ] );
  139. const domDiv = converter.viewToDom( viewDiv, document );
  140. expect( domDiv.innerHTML ).to.equal( '<p>foo&nbsp;</p><p>bar</p><p>xxx&nbsp;</p>' );
  141. } );
  142. it( 'when there are multiple spaces next to each other or between attribute elements', () => {
  143. const viewDiv = new ViewContainerElement( 'div', null, [
  144. new ViewText( 'x x x x ' ),
  145. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  146. new ViewAttributeElement( 'i', null,
  147. new ViewAttributeElement( 'b', null,
  148. new ViewAttributeElement( 'u', null, new ViewText( ' x' ) )
  149. )
  150. )
  151. ] );
  152. const domDiv = converter.viewToDom( viewDiv, document );
  153. expect( domDiv.innerHTML ).to.equal( 'x &nbsp;x &nbsp; x x <b>&nbsp;x </b><i><b><u>&nbsp;x</u></b></i>' );
  154. } );
  155. it( 'all together', () => {
  156. const viewDiv = new ViewContainerElement( 'div', null, [
  157. new ViewContainerElement( 'p', null, [
  158. new ViewText( ' x x x x ' ),
  159. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  160. new ViewAttributeElement( 'i', null,
  161. new ViewAttributeElement( 'b', null,
  162. new ViewAttributeElement( 'u', null, new ViewText( ' x ' ) )
  163. )
  164. )
  165. ] ),
  166. new ViewContainerElement( 'p', null, new ViewText( ' x ' ) )
  167. ] );
  168. const domDiv = converter.viewToDom( viewDiv, document );
  169. expect( domDiv.innerHTML ).to.equal(
  170. '<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>'
  171. );
  172. } );
  173. function test( inputTexts, output ) {
  174. if ( typeof inputTexts == 'string' ) {
  175. inputTexts = [ inputTexts ];
  176. }
  177. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  178. const viewElement = new ViewContainerElement( 'p' );
  179. for ( const text of inputTexts ) {
  180. viewElement.appendChildren( new ViewText( text.replace( /_/g, '\u00A0' ) ) );
  181. }
  182. const domElement = converter.viewToDom( viewElement, document );
  183. const data = domElement.innerHTML.replace( /&nbsp;/g, '_' );
  184. expect( data ).to.equal( output );
  185. } );
  186. }
  187. // At the beginning.
  188. test( ' x', '_x' );
  189. test( ' x', '_ x' );
  190. test( ' x', '_ _x' );
  191. test( ' x', '_ _ x' );
  192. // At the end.
  193. test( 'x ', 'x_' );
  194. test( 'x ', 'x _' );
  195. test( 'x ', 'x __' );
  196. test( 'x ', 'x _ _' );
  197. // In the middle.
  198. test( 'x x', 'x x' );
  199. test( 'x x', 'x _x' );
  200. test( 'x x', 'x _ x' );
  201. test( 'x x', 'x _ _x' );
  202. // Complex.
  203. test( ' x ', '_x_' );
  204. test( ' x x ', '_ x _x _' );
  205. test( ' x x ', '_ _x x _' );
  206. test( ' x x ', '_ _x x __' );
  207. test( ' x x ', '_ _x _ _x_' );
  208. // Only spaces.
  209. test( ' ', '_' );
  210. test( ' ', '__' );
  211. test( ' ', '_ _' );
  212. test( ' ', '_ __' );
  213. test( ' ', '_ _ _' );
  214. test( ' ', '_ _ __' );
  215. // With hard &nbsp;
  216. // It should be treated like a normal sign.
  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_ _' );
  231. test( 'x _ ', 'x __' );
  232. test( '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. test( 'x __x', 'x __x' );
  239. test( 'x _ x', 'x _ x' );
  240. test( 'x _ x', 'x __ _x' );
  241. // Two text nodes.
  242. test( [ 'x', 'y' ], 'xy' );
  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. test( [ 'x ', ' y' ], 'x _ _ y' );
  265. test( [ 'x ', ' y' ], 'x _ _ _y' );
  266. test( [ 'x ', ' y' ], 'x _ _ _ y' );
  267. // "Non-empty" + "empty" text nodes.
  268. test( [ 'x', ' ' ], 'x_' );
  269. test( [ 'x', ' ' ], 'x _' );
  270. test( [ 'x', ' ' ], 'x __' );
  271. test( [ 'x ', ' ' ], 'x _' );
  272. test( [ 'x ', ' ' ], 'x __' );
  273. test( [ 'x ', ' ' ], 'x _ _' );
  274. test( [ 'x ', ' ' ], 'x __' );
  275. test( [ 'x ', ' ' ], 'x _ _' );
  276. test( [ 'x ', ' ' ], 'x _ __' );
  277. test( [ 'x ', ' ' ], 'x _ _' );
  278. test( [ 'x ', ' ' ], 'x _ __' );
  279. test( [ 'x ', ' ' ], 'x _ _ _' );
  280. test( [ ' ', 'x' ], '_x' );
  281. test( [ ' ', 'x' ], '_ x' );
  282. test( [ ' ', 'x' ], '_ _x' );
  283. test( [ ' ', ' x' ], '_ x' );
  284. test( [ ' ', ' x' ], '_ _x' );
  285. test( [ ' ', ' x' ], '_ _ x' );
  286. test( [ ' ', ' x' ], '_ _x' );
  287. test( [ ' ', ' x' ], '_ _ x' );
  288. test( [ ' ', ' x' ], '_ _ _x' );
  289. test( [ ' ', ' x' ], '_ _ x' );
  290. test( [ ' ', ' x' ], '_ _ _x' );
  291. test( [ ' ', ' x' ], '_ _ _ x' );
  292. test( [ 'x', ' ', 'x' ], 'x x' );
  293. test( [ 'x', ' ', ' x' ], 'x _x' );
  294. test( [ 'x', ' ', ' x' ], 'x _ x' );
  295. test( [ 'x', ' ', ' x' ], 'x _ _ x' );
  296. test( [ 'x ', ' ', ' x' ], 'x _ x' );
  297. test( [ 'x ', ' ', ' x' ], 'x _ _x' );
  298. test( [ 'x ', ' ', ' x' ], 'x _ _ _x' );
  299. test( [ 'x ', ' ', ' x' ], 'x _ _x' );
  300. test( [ 'x ', ' ', ' x' ], 'x _ _ x' );
  301. test( [ 'x ', ' ', ' x' ], 'x _ _ _ x' );
  302. test( [ 'x ', ' ', ' x' ], 'x _ _ x' );
  303. test( [ 'x ', ' ', ' x' ], 'x _ _ _x' );
  304. test( [ 'x ', ' ', ' x' ], 'x _ _ _ _x' );
  305. // "Empty" + "empty" text nodes.
  306. test( [ ' ', ' ' ], '__' );
  307. test( [ ' ', ' ' ], '_ _' );
  308. test( [ ' ', ' ' ], '_ __' );
  309. test( [ ' ', ' ' ], '_ _' );
  310. test( [ ' ', ' ' ], '_ __' );
  311. test( [ ' ', ' ' ], '_ __' );
  312. test( [ ' ', ' ' ], '_ _ _' );
  313. test( [ ' ', ' ' ], '_ _ _' );
  314. test( [ ' ', ' ' ], '_ _ __' );
  315. it( 'not in preformatted blocks', () => {
  316. const viewDiv = new ViewContainerElement( 'pre', null, [ new ViewText( ' foo ' ), new ViewText( ' bar ' ) ] );
  317. const domDiv = converter.viewToDom( viewDiv, document );
  318. expect( domDiv.innerHTML ).to.equal( ' foo bar ' );
  319. } );
  320. it( 'text node before in a preformatted node', () => {
  321. const viewCode = new ViewAttributeElement( 'pre', null, new ViewText( 'foo ' ) );
  322. const viewDiv = new ViewContainerElement( 'div', null, [ viewCode, new ViewText( ' bar' ) ] );
  323. const domDiv = converter.viewToDom( viewDiv, document );
  324. expect( domDiv.innerHTML ).to.equal( '<pre>foo </pre> bar' );
  325. } );
  326. } );
  327. } );
  328. describe( 'viewChildrenToDom()', () => {
  329. it( 'should convert children', () => {
  330. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  331. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  332. expect( domChildren.length ).to.equal( 2 );
  333. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  334. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  335. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  336. } );
  337. it( 'should add filler', () => {
  338. const viewP = parse( '<container:p></container:p>' );
  339. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  340. expect( domChildren.length ).to.equal( 1 );
  341. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  342. } );
  343. it( 'should add filler according to fillerPositionOffset', () => {
  344. const viewP = parse( '<container:p>foo</container:p>' );
  345. viewP.getFillerOffset = () => 0;
  346. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  347. expect( domChildren.length ).to.equal( 2 );
  348. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  349. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  350. } );
  351. it( 'should pass options', () => {
  352. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  353. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  354. expect( domChildren.length ).to.equal( 2 );
  355. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  356. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  357. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  358. } );
  359. } );
  360. describe( 'viewPositionToDom()', () => {
  361. it( 'should convert the position in the text', () => {
  362. const domFoo = document.createTextNode( 'foo' );
  363. const domP = createElement( document, 'p', null, domFoo );
  364. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  365. converter.bindElements( domP, viewP );
  366. const viewPosition = selection.getFirstPosition();
  367. const domPosition = converter.viewPositionToDom( viewPosition );
  368. expect( domPosition.offset ).to.equal( 2 );
  369. expect( domPosition.parent ).to.equal( domFoo );
  370. } );
  371. it( 'should support unicode', () => {
  372. const domText = document.createTextNode( 'நிலைக்கு' );
  373. const domP = createElement( document, 'p', null, domText );
  374. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  375. converter.bindElements( domP, viewP );
  376. const viewPosition = selection.getFirstPosition();
  377. const domPosition = converter.viewPositionToDom( viewPosition );
  378. expect( domPosition.offset ).to.equal( 4 );
  379. expect( domPosition.parent ).to.equal( domText );
  380. } );
  381. it( 'should convert the position in the empty element', () => {
  382. const domP = createElement( document, 'p' );
  383. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  384. converter.bindElements( domP, viewP );
  385. const viewPosition = selection.getFirstPosition();
  386. const domPosition = converter.viewPositionToDom( viewPosition );
  387. expect( domPosition.offset ).to.equal( 0 );
  388. expect( domPosition.parent ).to.equal( domP );
  389. } );
  390. it( 'should convert the position in the non-empty element', () => {
  391. const domB = createElement( document, 'b', null, 'foo' );
  392. const domP = createElement( document, 'p', null, domB );
  393. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  394. converter.bindElements( domP, viewP );
  395. converter.bindElements( domB, viewP.getChild( 0 ) );
  396. const viewPosition = selection.getFirstPosition();
  397. const domPosition = converter.viewPositionToDom( viewPosition );
  398. expect( domPosition.offset ).to.equal( 1 );
  399. expect( domPosition.parent ).to.equal( domP );
  400. } );
  401. it( 'should convert the position after text', () => {
  402. const domP = createElement( document, 'p', null, 'foo' );
  403. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  404. converter.bindElements( domP, viewP );
  405. const viewPosition = selection.getFirstPosition();
  406. const domPosition = converter.viewPositionToDom( viewPosition );
  407. expect( domPosition.offset ).to.equal( 1 );
  408. expect( domPosition.parent ).to.equal( domP );
  409. } );
  410. it( 'should convert the position before text', () => {
  411. const domP = createElement( document, 'p', null, 'foo' );
  412. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  413. converter.bindElements( domP, viewP );
  414. const viewPosition = selection.getFirstPosition();
  415. const domPosition = converter.viewPositionToDom( viewPosition );
  416. expect( domPosition.offset ).to.equal( 0 );
  417. expect( domPosition.parent ).to.equal( domP );
  418. } );
  419. it( 'should update offset if DOM text node starts with inline filler', () => {
  420. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  421. const domP = createElement( document, 'p', null, domFoo );
  422. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  423. converter.bindElements( domP, viewP );
  424. const viewPosition = selection.getFirstPosition();
  425. const domPosition = converter.viewPositionToDom( viewPosition );
  426. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  427. expect( domPosition.parent ).to.equal( domFoo );
  428. } );
  429. it( 'should move the position to the text node if the position is where inline filler is', () => {
  430. const domFiller = document.createTextNode( INLINE_FILLER );
  431. const domP = createElement( document, 'p', null, domFiller );
  432. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  433. converter.bindElements( domP, viewP );
  434. const viewPosition = selection.getFirstPosition();
  435. const domPosition = converter.viewPositionToDom( viewPosition );
  436. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  437. expect( domPosition.parent ).to.equal( domFiller );
  438. } );
  439. it( 'should return null if view position is after a view element that has not been rendered to DOM', () => {
  440. const domP = createElement( document, 'p', null );
  441. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  442. converter.bindElements( domP, viewP );
  443. const viewPosition = selection.getFirstPosition();
  444. const domPosition = converter.viewPositionToDom( viewPosition );
  445. expect( domPosition ).to.equal( null );
  446. } );
  447. it( 'should return null if view position is in a view text node that has not been rendered to DOM', () => {
  448. const viewText = new ViewText( 'foo' );
  449. const viewPosition = new ViewPosition( viewText, 1 );
  450. const domPosition = converter.viewPositionToDom( viewPosition );
  451. expect( domPosition ).to.equal( null );
  452. } );
  453. it( 'should return null if view position is in a view element that has not been rendered to DOM', () => {
  454. const viewElement = new ViewContainerElement( 'div' );
  455. const viewPosition = new ViewPosition( viewElement, 0 );
  456. const domPosition = converter.viewPositionToDom( viewPosition );
  457. expect( domPosition ).to.equal( null );
  458. } );
  459. } );
  460. describe( 'viewRangeToDom()', () => {
  461. it( 'should convert view range to DOM range', () => {
  462. const domFoo = document.createTextNode( 'foo' );
  463. const domP = createElement( document, 'p', null, domFoo );
  464. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  465. converter.bindElements( domP, viewP );
  466. const viewRange = selection.getFirstRange();
  467. const domRange = converter.viewRangeToDom( viewRange );
  468. expect( domRange ).to.be.instanceof( Range );
  469. expect( domRange.startContainer ).to.equal( domFoo );
  470. expect( domRange.startOffset ).to.equal( 2 );
  471. expect( domRange.endContainer ).to.equal( domP );
  472. expect( domRange.endOffset ).to.equal( 1 );
  473. } );
  474. } );
  475. } );