8
0

dom-to-view.js 24 KB

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