dom-to-view.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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.mapViewToDom( viewP ) ).to.not.equal( domP );
  36. expect( converter.mapViewToDom( 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.mapViewToDom( viewP ) ).to.equal( domP );
  51. expect( converter.mapViewToDom( 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.mapViewToDom( viewP ) ).to.equal( domP );
  61. expect( converter.findCorrespondingDomText( 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.mapViewToDom( 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.mapViewToDom( viewFragment ) ).to.equal( domFragment );
  89. expect( converter.mapViewToDom( 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.mapViewToDom( 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. it( 'should return null for a comment', () => {
  120. const comment = document.createComment( 'abc' );
  121. expect( converter.domToView( comment ) ).to.be.null;
  122. } );
  123. describe( 'it should clear whitespaces', () => {
  124. it( 'at the beginning of block element', () => {
  125. const domDiv = createElement( document, 'div', {}, [
  126. document.createTextNode( ' ' ),
  127. createElement( document, 'p', {}, [
  128. document.createTextNode( ' foo' )
  129. ] ),
  130. createElement( document, 'p', {}, [
  131. document.createTextNode( ' foo' )
  132. ] )
  133. ] );
  134. const viewDiv = converter.domToView( domDiv );
  135. expect( viewDiv.childCount ).to.equal( 2 );
  136. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  137. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'foo' );
  138. } );
  139. it( 'at the end of block element', () => {
  140. const domDiv = createElement( document, 'div', {}, [
  141. createElement( document, 'p', {}, [
  142. document.createTextNode( 'foo ' )
  143. ] ),
  144. createElement( document, 'p', {}, [
  145. document.createTextNode( 'foo ' )
  146. ] ),
  147. document.createTextNode( ' ' )
  148. ] );
  149. const viewDiv = converter.domToView( domDiv );
  150. expect( viewDiv.childCount ).to.equal( 2 );
  151. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  152. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'foo' );
  153. } );
  154. it( 'multiple consecutive whitespaces changed to one', () => {
  155. const domDiv = createElement( document, 'div', {}, [
  156. createElement( document, 'p', {}, [
  157. document.createTextNode( ' f o o' )
  158. ] ),
  159. createElement( document, 'p', {}, [
  160. document.createTextNode( 'fo o ' )
  161. ] )
  162. ] );
  163. const viewDiv = converter.domToView( domDiv );
  164. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( 'f o o' );
  165. expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( 'fo o' );
  166. } );
  167. function test( inputTexts, output ) {
  168. if ( typeof inputTexts == 'string' ) {
  169. inputTexts = [ inputTexts ];
  170. }
  171. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  172. const domElement = createElement( document, 'div', {}, [] );
  173. for ( const text of inputTexts ) {
  174. domElement.appendChild( document.createTextNode( text.replace( /_/g, '\u00A0' ) ) );
  175. }
  176. const viewElement = converter.domToView( domElement );
  177. let data = '';
  178. for ( const child of viewElement.getChildren() ) {
  179. data += child.data.replace( /\u00A0/g, '_' );
  180. }
  181. expect( data ).to.equal( output );
  182. } );
  183. }
  184. // At the beginning.
  185. test( '_x', ' x' );
  186. test( '_ x', ' x' );
  187. test( '_ _x', ' x' );
  188. test( '_ _ x', ' x' );
  189. // At the end.
  190. test( 'x_', 'x ' );
  191. test( 'x _', 'x ' );
  192. test( 'x_ _', 'x ' );
  193. test( 'x _ _', 'x ' );
  194. // In the middle.
  195. test( 'x x', 'x x' );
  196. test( 'x _x', 'x x' );
  197. test( 'x _ x', 'x x' );
  198. test( 'x _ _x', 'x x' );
  199. // Complex.
  200. test( '_x_', ' x ' );
  201. test( '_ x _x _', ' x x ' );
  202. test( '_ _x x _', ' x x ' );
  203. test( '_ _x x_ _', ' x x ' );
  204. test( '_ _x _ _x_', ' x x ' );
  205. test( '_', ' ' );
  206. // With hard  
  207. test( '_x', ' x' );
  208. test( '__x', ' _x' );
  209. test( '___x', ' __x' );
  210. test( '__ x', ' _ x' );
  211. test( 'x_', 'x ' );
  212. test( 'x__', 'x_ ' );
  213. test( 'x___', 'x__ ' );
  214. // This is an edge case, but it's impossible to write elegant and compact algorithm that is also
  215. // 100% correct. We might assume that expected result is `x _` but it will be converted to `x `
  216. // by the algorithm. This is acceptable, though.
  217. test( '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. test( 'x_ _x', 'x_ x' );
  224. test( 'x __x', 'x _x' );
  225. test( 'x _ x', 'x x' );
  226. test( 'x __ _x', 'x _ x' );
  227. // Two text nodes.
  228. test( [ 'x', 'y' ], 'xy' );
  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. test( [ 'x ', '_ y' ], 'x y' );
  240. test( [ 'x_ ', '_ y' ], 'x y' );
  241. test( [ 'x _ ', '_ y' ], 'x y' );
  242. test( [ 'x_ _ ', '_ y' ], 'x y' );
  243. // Some tests with hard  
  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. it( 'not in preformatted blocks', () => {
  250. const domDiv = createElement( document, 'div', {}, [
  251. createElement( document, 'pre', {}, [
  252. document.createTextNode( ' foo\n foo ' )
  253. ] )
  254. ] );
  255. const viewDiv = converter.domToView( domDiv );
  256. expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( ' foo\n foo ' );
  257. } );
  258. } );
  259. } );
  260. describe( 'domChildrenToView', () => {
  261. it( 'should convert children', () => {
  262. const domImg = createElement( document, 'img' );
  263. const domText = document.createTextNode( 'foo' );
  264. const domP = createElement( document, 'p', null, [ domImg, domText ] );
  265. const viewChildren = Array.from( converter.domChildrenToView( domP ) );
  266. expect( viewChildren.length ).to.equal( 2 );
  267. expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<img></img>' );
  268. expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
  269. } );
  270. it( 'should skip filler', () => {
  271. const domFiller = converter.blockFiller( document );
  272. const domP = createElement( document, 'p', null, domFiller );
  273. const viewChildren = Array.from( converter.domChildrenToView( domP ) );
  274. expect( viewChildren.length ).to.equal( 0 );
  275. } );
  276. it( 'should pass options', () => {
  277. const domText = document.createTextNode( 'foo' );
  278. const domB = createElement( document, 'b', null, 'bar' );
  279. const domP = createElement( document, 'p', null, [ domB, domText ] );
  280. const viewChildren = Array.from( converter.domChildrenToView( domP, { withChildren: false } ) );
  281. expect( viewChildren.length ).to.equal( 2 );
  282. expect( stringify( viewChildren[ 0 ] ) ).to.equal( '<b></b>' );
  283. expect( stringify( viewChildren[ 1 ] ) ).to.equal( 'foo' );
  284. } );
  285. } );
  286. describe( 'domPositionToView()', () => {
  287. it( 'should converter position in text', () => {
  288. const domText = document.createTextNode( 'foo' );
  289. const domB = createElement( document, 'b', null, 'bar' );
  290. const domP = createElement( document, 'p', null, [ domText, domB ] );
  291. const viewP = parse( '<p>foo<b>bar</b></p>' );
  292. converter.bindElements( domP, viewP );
  293. converter.bindElements( domB, viewP.getChild( 0 ) );
  294. const viewPosition = converter.domPositionToView( domText, 2 );
  295. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>fo{}o<b>bar</b></p>' );
  296. } );
  297. it( 'should support unicode', () => {
  298. const domText = document.createTextNode( 'நிலைக்கு' );
  299. const domP = createElement( document, 'p', null, [ domText ] );
  300. const viewP = parse( '<p>நிலைக்கு</p>' );
  301. converter.bindElements( domP, viewP );
  302. const viewPosition = converter.domPositionToView( domText, 4 );
  303. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>நிலை{}க்கு</p>' );
  304. } );
  305. it( 'should converter position in element', () => {
  306. const domText = document.createTextNode( 'foo' );
  307. const domB = createElement( document, 'b', null, 'bar' );
  308. const domP = createElement( document, 'p', null, [ domText, domB ] );
  309. const viewP = parse( '<p>foo<b>bar</b></p>' );
  310. converter.bindElements( domP, viewP );
  311. converter.bindElements( domB, viewP.getChild( 0 ) );
  312. const viewPosition = converter.domPositionToView( domP, 1 );
  313. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]<b>bar</b></p>' );
  314. } );
  315. it( 'should converter position at the beginning', () => {
  316. const domText = document.createTextNode( 'foo' );
  317. const domP = createElement( document, 'p', null, domText );
  318. const viewP = parse( '<p>foo</p>' );
  319. converter.bindElements( domP, viewP );
  320. const viewPosition = converter.domPositionToView( domP, 0 );
  321. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]foo</p>' );
  322. } );
  323. it( 'should converter position inside block filler', () => {
  324. const converter = new DomConverter( { blockFiller: NBSP_FILLER } );
  325. const domFiller = NBSP_FILLER( document ); // eslint-disable-line new-cap
  326. const domP = createElement( document, 'p', null, domFiller );
  327. const viewP = parse( '<p></p>' );
  328. converter.bindElements( domP, viewP );
  329. const viewPosition = converter.domPositionToView( domFiller, 0 );
  330. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>[]</p>' );
  331. } );
  332. it( 'should converter position inside inline filler', () => {
  333. const domFiller = document.createTextNode( INLINE_FILLER );
  334. const domText = document.createTextNode( 'foo' );
  335. const domB = createElement( document, 'b', null, domFiller );
  336. const domP = createElement( document, 'p', null, [ domText, domB ] );
  337. const viewP = parse( '<p>foo<b></b></p>' );
  338. converter.bindElements( domP, viewP );
  339. converter.bindElements( domB, viewP.getChild( 1 ) );
  340. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH );
  341. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>[]</b></p>' );
  342. } );
  343. it( 'should converter position inside inline filler with text', () => {
  344. const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
  345. const domText = document.createTextNode( 'foo' );
  346. const domB = createElement( document, 'b', null, domFiller );
  347. const domP = createElement( document, 'p', null, [ domText, domB ] );
  348. const viewP = parse( '<p>foo<b>bar</b></p>' );
  349. converter.bindElements( domP, viewP );
  350. converter.bindElements( domB, viewP.getChild( 1 ) );
  351. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH + 2 );
  352. expect( viewPosition.offset ).to.equal( 2 );
  353. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>ba{}r</b></p>' );
  354. } );
  355. it( 'should converter position inside inline filler with text at the beginning', () => {
  356. const domFiller = document.createTextNode( INLINE_FILLER + 'bar' );
  357. const domText = document.createTextNode( 'foo' );
  358. const domB = createElement( document, 'b', null, domFiller );
  359. const domP = createElement( document, 'p', null, [ domText, domB ] );
  360. const viewP = parse( '<p>foo<b>bar</b></p>' );
  361. converter.bindElements( domP, viewP );
  362. converter.bindElements( domB, viewP.getChild( 1 ) );
  363. const viewPosition = converter.domPositionToView( domFiller, INLINE_FILLER_LENGTH - 1 );
  364. expect( viewPosition.offset ).to.equal( 0 );
  365. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo<b>{}bar</b></p>' );
  366. } );
  367. it( 'should converter position at the end', () => {
  368. const domText = document.createTextNode( 'foo' );
  369. const domP = createElement( document, 'p', null, domText );
  370. const viewP = parse( '<p>foo</p>' );
  371. converter.bindElements( domP, viewP );
  372. const viewPosition = converter.domPositionToView( domP, 1 );
  373. expect( stringify( viewP, viewPosition ) ).to.equal( '<p>foo[]</p>' );
  374. } );
  375. it( 'should return null if there is no corresponding parent node', () => {
  376. const domText = document.createTextNode( 'foo' );
  377. const domP = createElement( document, 'p', null, domText );
  378. const viewPosition = converter.domPositionToView( domP, 0 );
  379. expect( viewPosition ).to.be.null;
  380. } );
  381. it( 'should return null if there is no corresponding sibling node', () => {
  382. const domB = createElement( document, 'b', null, 'bar' );
  383. const domText = document.createTextNode( 'foo' );
  384. const domP = createElement( document, 'p', null, [ domB, domText ] );
  385. const viewPosition = converter.domPositionToView( domP, 1 );
  386. expect( viewPosition ).to.be.null;
  387. } );
  388. it( 'should return null if there is no corresponding text node', () => {
  389. const domText = document.createTextNode( 'foo' );
  390. const viewPosition = converter.domPositionToView( domText, 1 );
  391. expect( viewPosition ).to.be.null;
  392. } );
  393. } );
  394. describe( 'domRangeToView()', () => {
  395. it( 'should converter DOM range', () => {
  396. const domFoo = document.createTextNode( 'foo' );
  397. const domBar = document.createTextNode( 'bar' );
  398. const domB = createElement( document, 'b', null, domBar );
  399. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  400. const viewP = parse( '<p>foo<b>bar</b></p>' );
  401. converter.bindElements( domP, viewP );
  402. converter.bindElements( domB, viewP.getChild( 1 ) );
  403. const domRange = document.createRange();
  404. domRange.setStart( domFoo, 1 );
  405. domRange.setEnd( domBar, 2 );
  406. const viewRange = converter.domRangeToView( domRange );
  407. expect( stringify( viewP, viewRange ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
  408. } );
  409. it( 'should return null if start or end is null', () => {
  410. const domFoo = document.createTextNode( 'foo' );
  411. const domBar = document.createTextNode( 'bar' );
  412. const domB = createElement( document, 'b', null, domBar );
  413. createElement( document, 'p', null, [ domFoo, domB ] );
  414. const domRange = document.createRange();
  415. domRange.setStart( domFoo, 1 );
  416. domRange.setEnd( domBar, 2 );
  417. const viewRange = converter.domRangeToView( domRange );
  418. expect( viewRange ).to.be.null;
  419. } );
  420. } );
  421. describe( 'domSelectionToView()', () => {
  422. it( 'should convert selection', () => {
  423. const domFoo = document.createTextNode( 'foo' );
  424. const domBar = document.createTextNode( 'bar' );
  425. const domB = createElement( document, 'b', null, domBar );
  426. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  427. const viewP = parse( '<p>foo<b>bar</b></p>' );
  428. converter.bindElements( domP, viewP );
  429. converter.bindElements( domB, viewP.getChild( 1 ) );
  430. document.body.appendChild( domP );
  431. const domRange = document.createRange();
  432. domRange.setStart( domFoo, 1 );
  433. domRange.setEnd( domBar, 2 );
  434. const domSelection = document.getSelection();
  435. domSelection.removeAllRanges();
  436. domSelection.addRange( domRange );
  437. const viewSelection = converter.domSelectionToView( domSelection );
  438. expect( viewSelection.rangeCount ).to.equal( 1 );
  439. expect( stringify( viewP, viewSelection.getFirstRange() ) ).to.equal( '<p>f{oo<b>ba}r</b></p>' );
  440. } );
  441. it( 'should convert empty selection to empty selection', () => {
  442. const domSelection = document.getSelection();
  443. domSelection.removeAllRanges();
  444. const viewSelection = converter.domSelectionToView( domSelection );
  445. expect( viewSelection.rangeCount ).to.equal( 0 );
  446. } );
  447. it( 'should handle selection direction', () => {
  448. const domFoo = document.createTextNode( 'foo' );
  449. const domP = createElement( document, 'p', null, [ domFoo ] );
  450. const viewP = parse( '<p>foo</p>' );
  451. converter.bindElements( domP, viewP );
  452. document.body.appendChild( domP );
  453. const domRange = document.createRange();
  454. domRange.setStart( domFoo, 2 );
  455. domRange.collapse( true );
  456. const domSelection = document.getSelection();
  457. domSelection.removeAllRanges();
  458. domSelection.addRange( domRange );
  459. domSelection.extend( domFoo, 1 );
  460. const viewSelection = converter.domSelectionToView( domSelection );
  461. expect( viewSelection.rangeCount ).to.equal( 1 );
  462. expect( viewSelection.anchor.offset ).to.equal( 2 );
  463. expect( viewSelection.focus.offset ).to.equal( 1 );
  464. expect( viewSelection.isBackward ).to.be.true;
  465. } );
  466. it( 'should not add null ranges', () => {
  467. const domFoo = document.createTextNode( 'foo' );
  468. const domBar = document.createTextNode( 'bar' );
  469. const domB = createElement( document, 'b', null, domBar );
  470. const domP = createElement( document, 'p', null, [ domFoo, domB ] );
  471. document.body.appendChild( domP );
  472. const domRange = document.createRange();
  473. domRange.setStart( domFoo, 1 );
  474. domRange.setEnd( domBar, 2 );
  475. const domSelection = document.getSelection();
  476. domSelection.removeAllRanges();
  477. domSelection.addRange( domRange );
  478. const viewSelection = converter.domSelectionToView( domSelection );
  479. expect( viewSelection.rangeCount ).to.equal( 0 );
  480. } );
  481. it( 'should return fake selection', () => {
  482. const domContainer = document.createElement( 'div' );
  483. const domSelection = document.getSelection();
  484. domContainer.innerHTML = 'fake selection container';
  485. document.body.appendChild( domContainer );
  486. const viewSelection = new ViewSelection();
  487. viewSelection.addRange( ViewRange.createIn( new ViewElement() ) );
  488. converter.bindFakeSelection( domContainer, viewSelection );
  489. const domRange = document.createRange();
  490. domRange.selectNodeContents( domContainer );
  491. domSelection.removeAllRanges();
  492. domSelection.addRange( domRange );
  493. const bindViewSelection = converter.domSelectionToView( domSelection );
  494. expect( bindViewSelection.isEqual( viewSelection ) ).to.be.true;
  495. } );
  496. it( 'should return fake selection if selection is placed inside text node', () => {
  497. const domContainer = document.createElement( 'div' );
  498. const domSelection = document.getSelection();
  499. domContainer.innerHTML = 'fake selection container';
  500. document.body.appendChild( domContainer );
  501. const viewSelection = new ViewSelection();
  502. viewSelection.addRange( ViewRange.createIn( new ViewElement() ) );
  503. converter.bindFakeSelection( domContainer, viewSelection );
  504. const domRange = document.createRange();
  505. domRange.selectNodeContents( domContainer.firstChild );
  506. domSelection.removeAllRanges();
  507. domSelection.addRange( domRange );
  508. const bindViewSelection = converter.domSelectionToView( domSelection );
  509. expect( bindViewSelection.isEqual( viewSelection ) ).to.be.true;
  510. } );
  511. } );
  512. } );