dom-to-view.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ViewElement from '../../../src/view/element';
  7. import ViewRange from '../../../src/view/range';
  8. import ViewSelection from '../../../src/view/selection';
  9. import DomConverter from '../../../src/view/domconverter';
  10. import ViewDocumentFragment from '../../../src/view/documentfragment';
  11. import { INLINE_FILLER, INLINE_FILLER_LENGTH, NBSP_FILLER } from '../../../src/view/filler';
  12. import { parse, stringify } from '../../../src/dev-utils/view';
  13. import count from '@ckeditor/ckeditor5-utils/src/count';
  14. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  15. describe( 'DomConverter', () => {
  16. let converter;
  17. before( () => {
  18. converter = new DomConverter();
  19. } );
  20. describe( 'domToView', () => {
  21. it( 'should create tree of view elements from DOM elements', () => {
  22. const domImg = createElement( document, 'img' );
  23. const domText = document.createTextNode( 'foo' );
  24. const domP = createElement( document, 'p', { 'class': 'foo' }, [ domImg, domText ] );
  25. const viewImg = new ViewElement( 'img' );
  26. converter.bindElements( domImg, viewImg );
  27. const viewP = converter.domToView( domP );
  28. expect( viewP ).to.be.an.instanceof( ViewElement );
  29. expect( viewP.name ).to.equal( 'p' );
  30. expect( viewP.getAttribute( 'class' ) ).to.equal( 'foo' );
  31. expect( count( viewP.getAttributeKeys() ) ).to.equal( 1 );
  32. expect( viewP.childCount ).to.equal( 2 );
  33. expect( viewP.getChild( 0 ).name ).to.equal( 'img' );
  34. expect( viewP.getChild( 1 ).data ).to.equal( 'foo' );
  35. expect( converter.getCorrespondingDom( viewP ) ).to.not.equal( domP );
  36. expect( converter.getCorrespondingDom( viewP.getChild( 0 ) ) ).to.equal( domImg );
  37. } );
  38. it( 'should create tree of view elements from DOM elements and bind elements', () => {
  39. const domImg = createElement( document, 'img' );
  40. const domText = document.createTextNode( 'foo' );
  41. const domP = createElement( document, 'p', { 'class': 'foo' }, [ domImg, domText ] );
  42. const viewP = converter.domToView( domP, { bind: true } );
  43. expect( viewP ).to.be.an.instanceof( ViewElement );
  44. expect( viewP.name ).to.equal( 'p' );
  45. expect( viewP.getAttribute( 'class' ) ).to.equal( 'foo' );
  46. expect( count( viewP.getAttributeKeys() ) ).to.equal( 1 );
  47. expect( viewP.childCount ).to.equal( 2 );
  48. expect( viewP.getChild( 0 ).name ).to.equal( 'img' );
  49. expect( viewP.getChild( 1 ).data ).to.equal( 'foo' );
  50. expect( converter.getCorrespondingDom( viewP ) ).to.equal( domP );
  51. expect( converter.getCorrespondingDom( viewP.getChild( 0 ) ) ).to.equal( domP.childNodes[ 0 ] );
  52. } );
  53. it( 'should support unicode', () => {
  54. const domText = document.createTextNode( 'நிலைக்கு' );
  55. const domP = createElement( document, 'p', { 'class': 'foo' }, [ domText ] );
  56. const viewP = converter.domToView( domP, { bind: true } );
  57. expect( viewP.childCount ).to.equal( 1 );
  58. const viewText = viewP.getChild( 0 );
  59. expect( viewText.data ).to.equal( 'நிலைக்கு' );
  60. expect( converter.getCorrespondingDom( viewP ) ).to.equal( domP );
  61. expect( converter.getCorrespondingDom( viewP.getChild( 0 ) ) ).to.equal( domP.childNodes[ 0 ] );
  62. } );
  63. it( 'should create tree of view elements from DOM element without children', () => {
  64. const domImg = createElement( document, 'img' );
  65. const domText = document.createTextNode( 'foo' );
  66. const domP = createElement( document, 'p', { 'class': 'foo' }, [ domImg, domText ] );
  67. const viewImg = new ViewElement( 'img' );
  68. converter.bindElements( domImg, viewImg );
  69. const viewP = converter.domToView( domP, { withChildren: false } );
  70. expect( viewP ).to.be.an.instanceof( ViewElement );
  71. expect( viewP.name ).to.equal( 'p' );
  72. expect( viewP.getAttribute( 'class' ) ).to.equal( 'foo' );
  73. expect( count( viewP.getAttributeKeys() ) ).to.equal( 1 );
  74. expect( viewP.childCount ).to.equal( 0 );
  75. expect( converter.getCorrespondingDom( viewP ) ).to.not.equal( domP );
  76. } );
  77. it( 'should create view document fragment from DOM document fragment', () => {
  78. const domImg = createElement( document, 'img' );
  79. const domText = document.createTextNode( 'foo' );
  80. const domFragment = document.createDocumentFragment();
  81. domFragment.appendChild( domImg );
  82. domFragment.appendChild( domText );
  83. const viewFragment = converter.domToView( domFragment, { bind: true } );
  84. expect( viewFragment ).to.be.an.instanceof( ViewDocumentFragment );
  85. expect( viewFragment.childCount ).to.equal( 2 );
  86. expect( viewFragment.getChild( 0 ).name ).to.equal( 'img' );
  87. expect( viewFragment.getChild( 1 ).data ).to.equal( 'foo' );
  88. expect( converter.getCorrespondingDom( viewFragment ) ).to.equal( domFragment );
  89. expect( converter.getCorrespondingDom( viewFragment.getChild( 0 ) ) ).to.equal( domFragment.childNodes[ 0 ] );
  90. } );
  91. it( 'should create view document fragment from DOM document fragment without children', () => {
  92. const domImg = createElement( document, 'img' );
  93. const domText = document.createTextNode( 'foo' );
  94. const domFragment = document.createDocumentFragment();
  95. domFragment.appendChild( domImg );
  96. domFragment.appendChild( domText );
  97. const viewImg = new ViewElement( 'img' );
  98. converter.bindElements( domImg, viewImg );
  99. const viewFragment = converter.domToView( domFragment, { withChildren: false } );
  100. expect( viewFragment ).to.be.an.instanceof( ViewDocumentFragment );
  101. expect( viewFragment.childCount ).to.equal( 0 );
  102. expect( converter.getCorrespondingDom( viewFragment ) ).to.not.equal( domFragment );
  103. } );
  104. it( 'should return already bind document fragment', () => {
  105. const domFragment = document.createDocumentFragment();
  106. const viewFragment = new ViewDocumentFragment();
  107. converter.bindDocumentFragments( domFragment, viewFragment );
  108. const viewFragment2 = converter.domToView( domFragment );
  109. expect( viewFragment2 ).to.equal( viewFragment );
  110. } );
  111. it( 'should return null for block filler', () => {
  112. const domFiller = converter.blockFiller( document );
  113. expect( converter.domToView( domFiller ) ).to.be.null;
  114. } );
  115. it( 'should return null for empty text node', () => {
  116. const textNode = document.createTextNode( '' );
  117. expect( converter.domToView( textNode ) ).to.be.null;
  118. } );
  119. describe( 'it should clear whitespaces', () => {
  120. it( 'at the beginning of block element', () => {
  121. const domDiv = createElement( document, 'div', {}, [
  122. document.createTextNode( ' ' ),
  123. createElement( document, 'p', {}, [
  124. document.createTextNode( ' foo' )
  125. ] ),
  126. createElement( document, 'p', {}, [
  127. document.createTextNode( ' foo' )
  128. ] )
  129. ] );
  130. const viewDiv = converter.domToView( domDiv );
  131. expect( viewDiv.childCount ).to.equal( 2 );
  132. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  133. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'foo' );
  134. } );
  135. it( 'at the end of block element', () => {
  136. const domDiv = createElement( document, 'div', {}, [
  137. createElement( document, 'p', {}, [
  138. document.createTextNode( 'foo ' )
  139. ] ),
  140. createElement( document, 'p', {}, [
  141. document.createTextNode( 'foo ' )
  142. ] ),
  143. document.createTextNode( ' ' )
  144. ] );
  145. const viewDiv = converter.domToView( domDiv );
  146. expect( viewDiv.childCount ).to.equal( 2 );
  147. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  148. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'foo' );
  149. } );
  150. it( 'multiple consecutive whitespaces changed to one', () => {
  151. const domDiv = createElement( document, 'div', {}, [
  152. createElement( document, 'p', {}, [
  153. document.createTextNode( ' f o o' )
  154. ] ),
  155. createElement( document, 'p', {}, [
  156. document.createTextNode( 'fo o ' )
  157. ] )
  158. ] );
  159. const viewDiv = converter.domToView( domDiv );
  160. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'f o o' );
  161. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'fo o' );
  162. } );
  163. function test( inputTexts, output ) {
  164. if ( typeof inputTexts == 'string' ) {
  165. inputTexts = [ inputTexts ];
  166. }
  167. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  168. const domElement = createElement( document, 'div', {}, [] );
  169. for ( let text of inputTexts ) {
  170. domElement.appendChild( document.createTextNode( text.replace( /_/g, '\u00A0' ) ) );
  171. }
  172. const viewElement = converter.domToView( domElement );
  173. let data = '';
  174. for ( let child of viewElement.getChildren() ) {
  175. data += child.data.replace( /\u00A0/g, '_' );
  176. }
  177. expect( data ).to.equal( output );
  178. } );
  179. }
  180. // At the beginning.
  181. test( '_x', ' x' );
  182. test( '_ x', ' x' );
  183. test( '_ _x', ' x' );
  184. test( '_ _ x', ' x' );
  185. // At the end.
  186. test( 'x_', 'x ' );
  187. test( 'x _', 'x ' );
  188. test( 'x_ _', 'x ' );
  189. test( 'x _ _', 'x ' );
  190. // In the middle.
  191. test( 'x x', 'x x' );
  192. test( 'x _x', 'x x' );
  193. test( 'x _ x', 'x x' );
  194. test( 'x _ _x', 'x x' );
  195. // Complex.
  196. test( '_x_', ' x ' );
  197. test( '_ x _x _', ' x x ' );
  198. test( '_ _x x _', ' x x ' );
  199. test( '_ _x x_ _', ' x x ' );
  200. test( '_ _x _ _x_', ' x x ' );
  201. test( '_', ' ' );
  202. // With hard  
  203. test( '_x', ' x' );
  204. test( '__x', ' _x' );
  205. test( '___x', ' __x' );
  206. test( '__ x', ' _ x' );
  207. test( 'x_', 'x ' );
  208. test( 'x__', 'x_ ' );
  209. test( 'x___', 'x__ ' );
  210. // This is an edge case, but it's impossible to write elegant and compact algorithm that is also
  211. // 100% correct. We might assume that expected result is `x _` but it will be converted to `x `
  212. // by the algorithm. This is acceptable, though.
  213. test( 'x __', 'x ' );
  214. test( 'x_x', 'x_x' );
  215. test( 'x___x', 'x___x' );
  216. test( 'x____x', 'x____x' );
  217. test( 'x__ x', 'x__ x' );
  218. test( 'x___ x', 'x___ x' );
  219. test( 'x_ _x', 'x_ x' );
  220. test( 'x __x', 'x _x' );
  221. test( 'x _ x', 'x x' );
  222. test( 'x __ _x', 'x _ x' );
  223. // Two text nodes.
  224. test( [ 'x', 'y' ], 'xy' );
  225. test( [ 'x ', 'y' ], 'x y' );
  226. test( [ 'x _', 'y' ], 'x y' );
  227. test( [ 'x _ ', 'y' ], 'x y' );
  228. test( [ 'x _ _', 'y' ], 'x y' );
  229. test( [ 'x', ' y' ], 'x y' );
  230. test( [ 'x ', '_y' ], 'x y' );
  231. test( [ 'x_ ', '_y' ], 'x y' );
  232. test( [ 'x _ ', '_y' ], 'x y' );
  233. test( [ 'x_ _ ', '_y' ], 'x y' );
  234. test( [ 'x', ' _y' ], 'x y' );
  235. test( [ 'x ', '_ y' ], 'x y' );
  236. test( [ 'x_ ', '_ y' ], 'x y' );
  237. test( [ 'x _ ', '_ y' ], 'x y' );
  238. test( [ 'x_ _ ', '_ y' ], 'x y' );
  239. // Some tests with hard  
  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. it( 'not in preformatted blocks', () => {
  246. const domDiv = createElement( document, 'div', {}, [
  247. createElement( document, 'pre', {}, [
  248. document.createTextNode( ' foo\n foo ' )
  249. ] )
  250. ] );
  251. const viewDiv = converter.domToView( domDiv );
  252. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( ' foo\n foo ' );
  253. } );
  254. } );
  255. } );
  256. describe( 'domChildrenToView', () => {
  257. it( 'should convert children', () => {
  258. const domImg = createElement( document, 'img' );
  259. const domText = document.createTextNode( 'foo' );
  260. const domP = createElement( document, 'p', null, [ domImg, domText ] );
  261. const viewChildren = Array.from( converter.domChildrenToView( domP ) );
  262. expect( viewChildren.length ).to.equal( 2 );
  263. expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<img></img>' );
  264. expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
  265. } );
  266. it( 'should skip filler', () => {
  267. const domFiller = converter.blockFiller( document );
  268. const domP = createElement( document, 'p', null, domFiller );
  269. const viewChildren = Array.from( converter.domChildrenToView( domP ) );
  270. expect( viewChildren.length ).to.equal( 0 );
  271. } );
  272. it( 'should pass options', () => {
  273. const domText = document.createTextNode( 'foo' );
  274. const domB = createElement( document, 'b', null, 'bar' );
  275. const domP = createElement( document, 'p', null, [ domB, domText ] );
  276. const viewChildren = Array.from( converter.domChildrenToView( domP, { withChildren: false } ) );
  277. expect( viewChildren.length ).to.equal( 2 );
  278. expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<b></b>' );
  279. expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
  280. } );
  281. } );
  282. describe( 'domPositionToView', () => {
  283. it( 'should converter position in text', () => {
  284. const domText = document.createTextNode( 'foo' );
  285. const domB = createElement( document, 'b', null, 'bar' );
  286. const domP = createElement( document, 'p', null, [ domText, domB ] );
  287. const viewP = parse( '<p>foo<b>bar</b></p>' );
  288. converter.bindElements( domP, viewP );
  289. converter.bindElements( domB, viewP.getChild( 0 ) );
  290. const viewPosition = converter.domPositionToView( domText, 2 );
  291. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>fo{}o<b>bar</b></p>' );
  292. } );
  293. it( 'should support unicode', () => {
  294. const domText = document.createTextNode( 'நிலைக்கு' );
  295. const domP = createElement( document, 'p', null, [ domText ] );
  296. const viewP = parse( '<p>நிலைக்கு</p>' );
  297. converter.bindElements( domP, viewP );
  298. const viewPosition = converter.domPositionToView( domText, 4 );
  299. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>நிலை{}க்கு</p>' );
  300. } );
  301. it( 'should converter position in element', () => {
  302. const domText = document.createTextNode( 'foo' );
  303. const domB = createElement( document, 'b', null, 'bar' );
  304. const domP = createElement( document, 'p', null, [ domText, domB ] );
  305. const viewP = parse( '<p>foo<b>bar</b></p>' );
  306. converter.bindElements( domP, viewP );
  307. converter.bindElements( domB, viewP.getChild( 0 ) );
  308. const viewPosition = converter.domPositionToView( domP, 1 );
  309. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]<b>bar</b></p>' );
  310. } );
  311. it( 'should converter position at the beginning', () => {
  312. const domText = document.createTextNode( 'foo' );
  313. const domP = createElement( document, 'p', null, domText );
  314. const viewP = parse( '<p>foo</p>' );
  315. converter.bindElements( domP, viewP );
  316. const viewPosition = converter.domPositionToView( domP, 0 );
  317. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]foo</p>' );
  318. } );
  319. it( 'should converter position inside block filler', () => {
  320. const converter = new DomConverter( { blockFiller: NBSP_FILLER } );
  321. const domFiller = NBSP_FILLER( document );
  322. const domP = createElement( document, 'p', null, domFiller );
  323. const viewP = parse( '<p></p>' );
  324. converter.bindElements( domP, viewP );
  325. const viewPosition = converter.domPositionToView( domFiller, 0 );
  326. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]</p>' );
  327. } );
  328. it( 'should converter position inside inline filler', () => {
  329. const domFiller = document.createTextNode( INLINE_FILLER );
  330. const domText = document.createTextNode( 'foo' );
  331. const domB = createElement( document, 'b', null, domFiller );
  332. const domP = createElement( document, 'p', null, [ domText, domB ] );
  333. const viewP = parse( '<p>foo<b></b></p>' );
  334. converter.bindElements( domP, viewP );
  335. converter.bindElements( domB, viewP.getChild( 1 ) );
  336. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH );
  337. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>[]</b></p>' );
  338. } );
  339. it( 'should converter position inside inline filler with text', () => {
  340. const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
  341. const domText = document.createTextNode( 'foo' );
  342. const domB = createElement( document, 'b', null, domFiller );
  343. const domP = createElement( document, 'p', null, [ domText, domB ] );
  344. const viewP = parse( '<p>foo<b>bar</b></p>' );
  345. converter.bindElements( domP, viewP );
  346. converter.bindElements( domB, viewP.getChild( 1 ) );
  347. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH + 2 );
  348. expect( viewPosition.offset ).to.equal( 2 );
  349. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>ba{}r</b></p>' );
  350. } );
  351. it( 'should converter position inside inline filler with text at the beginning', () => {
  352. const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
  353. const domText = document.createTextNode( 'foo' );
  354. const domB = createElement( document, 'b', null, domFiller );
  355. const domP = createElement( document, 'p', null, [ domText, domB ] );
  356. const viewP = parse( '<p>foo<b>bar</b></p>' );
  357. converter.bindElements( domP, viewP );
  358. converter.bindElements( domB, viewP.getChild( 1 ) );
  359. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH - 1 );
  360. expect( viewPosition.offset ).to.equal( 0 );
  361. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>{}bar</b></p>' );
  362. } );
  363. it( 'should converter position at the end', () => {
  364. const domText = document.createTextNode( 'foo' );
  365. const domP = createElement( document, 'p', null, domText );
  366. const viewP = parse( '<p>foo</p>' );
  367. converter.bindElements( domP, viewP );
  368. const viewPosition = converter.domPositionToView( domP, 1 );
  369. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]</p>' );
  370. } );
  371. it( 'should return null if there is no corresponding parent node', () => {
  372. const domText = document.createTextNode( 'foo' );
  373. const domP = createElement( document, 'p', null, domText );
  374. const viewPosition = converter.domPositionToView( domP, 0 );
  375. expect( viewPosition ).to.be.null;
  376. } );
  377. it( 'should return null if there is no corresponding sibling node', () => {
  378. const domB = createElement( document, 'b', null, 'bar' );
  379. const domText = document.createTextNode( 'foo' );
  380. const domP = createElement( document, 'p', null, [ domB, domText ] );
  381. const viewPosition = converter.domPositionToView( domP, 1 );
  382. expect( viewPosition ).to.be.null;
  383. } );
  384. it( 'should return null if there is no corresponding text node', () => {
  385. const domText = document.createTextNode( 'foo' );
  386. const viewPosition = converter.domPositionToView( domText, 1 );
  387. expect( viewPosition ).to.be.null;
  388. } );
  389. } );
  390. describe( 'domRangeToView', () => {
  391. it( 'should converter DOM range', () => {
  392. const domFoo = document.createTextNode( 'foo' );
  393. const domBar = document.createTextNode( 'bar' );
  394. const domB = createElement( document, 'b', null, domBar );
  395. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  396. const viewP = parse( '<p>foo<b>bar</b></p>' );
  397. converter.bindElements( domP, viewP );
  398. converter.bindElements( domB, viewP.getChild( 1 ) );
  399. const domRange = document.createRange();
  400. domRange.setStart( domFoo, 1 );
  401. domRange.setEnd( domBar, 2 );
  402. const viewRange = converter.domRangeToView( domRange );
  403. expect( stringify( viewP, viewRange ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
  404. } );
  405. it( 'should return null if start or end is null', () => {
  406. const domFoo = document.createTextNode( 'foo' );
  407. const domBar = document.createTextNode( 'bar' );
  408. const domB = createElement( document, 'b', null, domBar );
  409. createElement( document, 'p', null, [ domFoo, domB ] );
  410. const domRange = document.createRange();
  411. domRange.setStart( domFoo, 1 );
  412. domRange.setEnd( domBar, 2 );
  413. const viewRange = converter.domRangeToView( domRange );
  414. expect( viewRange ).to.be.null;
  415. } );
  416. } );
  417. describe( 'domSelectionToView', () => {
  418. it( 'should convert selection', () => {
  419. const domFoo = document.createTextNode( 'foo' );
  420. const domBar = document.createTextNode( 'bar' );
  421. const domB = createElement( document, 'b', null, domBar );
  422. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  423. const viewP = parse( '<p>foo<b>bar</b></p>' );
  424. converter.bindElements( domP, viewP );
  425. converter.bindElements( domB, viewP.getChild( 1 ) );
  426. document.body.appendChild( domP );
  427. const domRange = document.createRange();
  428. domRange.setStart( domFoo, 1 );
  429. domRange.setEnd( domBar, 2 );
  430. const domSelection = document.getSelection();
  431. domSelection.removeAllRanges();
  432. domSelection.addRange( domRange );
  433. const viewSelection = converter.domSelectionToView( domSelection );
  434. expect( viewSelection.rangeCount ).to.equal( 1 );
  435. expect( stringify( viewP, viewSelection.getFirstRange() ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
  436. } );
  437. it( 'should convert empty selection to empty selection', () => {
  438. const domSelection = document.getSelection();
  439. domSelection.removeAllRanges();
  440. const viewSelection = converter.domSelectionToView( domSelection );
  441. expect( viewSelection.rangeCount ).to.equal( 0 );
  442. } );
  443. it( 'should handle selection direction', () => {
  444. const domFoo = document.createTextNode( 'foo' );
  445. const domP = createElement( document, 'p', null, [ domFoo ] );
  446. const viewP = parse( '<p>foo</p>' );
  447. converter.bindElements( domP, viewP );
  448. document.body.appendChild( domP );
  449. const domRange = document.createRange();
  450. domRange.setStart( domFoo, 2 );
  451. domRange.collapse( true );
  452. const domSelection = document.getSelection();
  453. domSelection.removeAllRanges();
  454. domSelection.addRange( domRange );
  455. domSelection.extend( domFoo, 1 );
  456. const viewSelection = converter.domSelectionToView( domSelection );
  457. expect( viewSelection.rangeCount ).to.equal( 1 );
  458. expect( viewSelection.anchor.offset ).to.equal( 2 );
  459. expect( viewSelection.focus.offset ).to.equal( 1 );
  460. expect( viewSelection.isBackward ).to.be.true;
  461. } );
  462. it( 'should not add null ranges', () => {
  463. const domFoo = document.createTextNode( 'foo' );
  464. const domBar = document.createTextNode( 'bar' );
  465. const domB = createElement( document, 'b', null, domBar );
  466. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  467. document.body.appendChild( domP );
  468. const domRange = document.createRange();
  469. domRange.setStart( domFoo, 1 );
  470. domRange.setEnd( domBar, 2 );
  471. const domSelection = document.getSelection();
  472. domSelection.removeAllRanges();
  473. domSelection.addRange( domRange );
  474. const viewSelection = converter.domSelectionToView( domSelection );
  475. expect( viewSelection.rangeCount ).to.equal( 0 );
  476. } );
  477. it( 'should return fake selection', () => {
  478. const domContainer = document.createElement( 'div' );
  479. const domSelection = document.getSelection();
  480. domContainer.innerHTML = 'fake selection container';
  481. document.body.appendChild( domContainer );
  482. const viewSelection = new ViewSelection();
  483. viewSelection.addRange( ViewRange.createIn( new ViewElement() ) );
  484. converter.bindFakeSelection( domContainer, viewSelection );
  485. const domRange = document.createRange();
  486. domRange.selectNodeContents( domContainer );
  487. domSelection.removeAllRanges();
  488. domSelection.addRange( domRange );
  489. const bindViewSelection = converter.domSelectionToView( domSelection );
  490. expect( bindViewSelection.isEqual( viewSelection ) ).to.be.true;
  491. } );
  492. it( 'should return fake selection if selection is placed inside text node', () => {
  493. const domContainer = document.createElement( 'div' );
  494. const domSelection = document.getSelection();
  495. domContainer.innerHTML = 'fake selection container';
  496. document.body.appendChild( domContainer );
  497. const viewSelection = new ViewSelection();
  498. viewSelection.addRange( ViewRange.createIn( new ViewElement() ) );
  499. converter.bindFakeSelection( domContainer, viewSelection );
  500. const domRange = document.createRange();
  501. domRange.selectNodeContents( domContainer.firstChild );
  502. domSelection.removeAllRanges();
  503. domSelection.addRange( domRange );
  504. const bindViewSelection = converter.domSelectionToView( domSelection );
  505. expect( bindViewSelection.isEqual( viewSelection ) ).to.be.true;
  506. } );
  507. } );
  508. } );