view-to-dom.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 ViewEmptyElement from '../../../src/view/emptyelement';
  12. import DomConverter from '../../../src/view/domconverter';
  13. import ViewDocumentFragment from '../../../src/view/documentfragment';
  14. import { INLINE_FILLER, INLINE_FILLER_LENGTH } from '../../../src/view/filler';
  15. import { parse } from '../../../src/dev-utils/view';
  16. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  17. describe( 'DomConverter', () => {
  18. let converter;
  19. before( () => {
  20. converter = new DomConverter();
  21. } );
  22. describe( 'viewToDom()', () => {
  23. it( 'should create tree of DOM elements from view elements', () => {
  24. const viewImg = new ViewElement( 'img' );
  25. const viewText = new ViewText( 'foo' );
  26. const viewP = new ViewElement( 'p', { class: 'foo' } );
  27. viewP._appendChild( viewImg );
  28. viewP._appendChild( viewText );
  29. const domImg = document.createElement( 'img' );
  30. converter.bindElements( domImg, viewImg );
  31. const domP = converter.viewToDom( viewP, document );
  32. expect( domP ).to.be.an.instanceof( HTMLElement );
  33. expect( domP.tagName ).to.equal( 'P' );
  34. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  35. expect( domP.attributes.length ).to.equal( 1 );
  36. expect( domP.childNodes.length ).to.equal( 2 );
  37. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  38. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  39. expect( converter.mapDomToView( domP ) ).not.to.equal( viewP );
  40. expect( converter.mapDomToView( domP.childNodes[ 0 ] ) ).to.equal( viewImg );
  41. } );
  42. it( 'should create tree of DOM elements from view elements and bind elements', () => {
  43. const viewImg = new ViewElement( 'img' );
  44. const viewText = new ViewText( 'foo' );
  45. const viewP = new ViewElement( 'p', { class: 'foo' } );
  46. viewP._appendChild( viewImg );
  47. viewP._appendChild( viewText );
  48. const domP = converter.viewToDom( viewP, document, { bind: true } );
  49. expect( domP ).to.be.an.instanceof( HTMLElement );
  50. expect( domP.tagName ).to.equal( 'P' );
  51. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  52. expect( domP.attributes.length ).to.equal( 1 );
  53. expect( domP.childNodes.length ).to.equal( 2 );
  54. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  55. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  56. expect( converter.mapDomToView( domP ) ).to.equal( viewP );
  57. expect( converter.mapDomToView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  58. } );
  59. it( 'should support unicode', () => {
  60. const viewText = new ViewText( 'நிலைக்கு' );
  61. const viewP = new ViewElement( 'p', null, viewText );
  62. const domP = converter.viewToDom( viewP, document, { bind: true } );
  63. expect( domP.childNodes.length ).to.equal( 1 );
  64. expect( domP.childNodes[ 0 ].data ).to.equal( 'நிலைக்கு' );
  65. expect( converter.mapDomToView( domP ) ).to.equal( viewP );
  66. expect( converter.findCorrespondingViewText( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  67. } );
  68. it( 'should create tree of DOM elements from view element without children', () => {
  69. const viewImg = new ViewElement( 'img' );
  70. const viewText = new ViewText( 'foo' );
  71. const viewP = new ViewElement( 'p', { class: 'foo' } );
  72. viewP._appendChild( viewImg );
  73. viewP._appendChild( viewText );
  74. const domImg = document.createElement( 'img' );
  75. converter.bindElements( domImg, viewImg );
  76. const domP = converter.viewToDom( viewP, document, { withChildren: false } );
  77. expect( domP ).to.be.an.instanceof( HTMLElement );
  78. expect( domP.tagName ).to.equal( 'P' );
  79. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  80. expect( domP.attributes.length ).to.equal( 1 );
  81. expect( domP.childNodes.length ).to.equal( 0 );
  82. expect( converter.mapDomToView( domP ) ).not.to.equal( viewP );
  83. } );
  84. it( 'should create DOM document fragment from view document fragment and bind elements', () => {
  85. const viewImg = new ViewElement( 'img' );
  86. const viewText = new ViewText( 'foo' );
  87. const viewFragment = new ViewDocumentFragment();
  88. viewFragment._appendChild( viewImg );
  89. viewFragment._appendChild( viewText );
  90. const domFragment = converter.viewToDom( viewFragment, document, { bind: true } );
  91. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  92. expect( domFragment.childNodes.length ).to.equal( 2 );
  93. expect( domFragment.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  94. expect( domFragment.childNodes[ 1 ].data ).to.equal( 'foo' );
  95. expect( converter.mapDomToView( domFragment ) ).to.equal( viewFragment );
  96. expect( converter.mapDomToView( domFragment.childNodes[ 0 ] ) ).to.equal( viewFragment.getChild( 0 ) );
  97. } );
  98. it( 'should create DOM document fragment from view document without children', () => {
  99. const viewImg = new ViewElement( 'img' );
  100. const viewText = new ViewText( 'foo' );
  101. const viewFragment = new ViewDocumentFragment();
  102. viewFragment._appendChild( viewImg );
  103. viewFragment._appendChild( viewText );
  104. const domImg = document.createElement( 'img' );
  105. converter.bindElements( domImg, viewImg );
  106. const domFragment = converter.viewToDom( viewFragment, document, { withChildren: false } );
  107. expect( domFragment ).to.be.an.instanceof( DocumentFragment );
  108. expect( domFragment.childNodes.length ).to.equal( 0 );
  109. expect( converter.mapDomToView( domFragment ) ).not.to.equal( viewFragment );
  110. } );
  111. it( 'should return already bind document fragment', () => {
  112. const domFragment = document.createDocumentFragment();
  113. const viewFragment = new ViewDocumentFragment();
  114. converter.bindDocumentFragments( domFragment, viewFragment );
  115. const domFragment2 = converter.viewToDom( viewFragment, document );
  116. expect( domFragment2 ).to.equal( domFragment );
  117. } );
  118. it( 'should create DOM text node from view text node', () => {
  119. const viewTextNode = new ViewText( 'foo' );
  120. const domTextNode = converter.viewToDom( viewTextNode, document );
  121. expect( domTextNode ).to.be.instanceof( Text );
  122. expect( domTextNode.data ).to.equal( 'foo' );
  123. } );
  124. it( 'should create namespaced elements', () => {
  125. const namespace = 'http://www.w3.org/2000/svg';
  126. const viewSvg = new ViewElement( 'svg', { xmlns: namespace } );
  127. const domSvg = converter.viewToDom( viewSvg, document );
  128. expect( domSvg.createSVGRect ).to.be.a( 'function' );
  129. } );
  130. describe( 'it should convert spaces to  ', () => {
  131. it( 'at the beginning of each container element', () => {
  132. const viewDiv = new ViewContainerElement( 'div', null, [
  133. new ViewContainerElement( 'p', null, new ViewText( ' foo' ) ),
  134. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  135. new ViewContainerElement( 'p', null, new ViewText( ' xxx' ) )
  136. ] );
  137. const domDiv = converter.viewToDom( viewDiv, document );
  138. expect( domDiv.innerHTML ).to.equal( '<p>&nbsp;foo</p><p>bar</p><p>&nbsp;xxx</p>' );
  139. } );
  140. it( 'at the end of each container element', () => {
  141. const viewDiv = new ViewContainerElement( 'div', null, [
  142. new ViewContainerElement( 'p', null, new ViewText( 'foo ' ) ),
  143. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  144. new ViewContainerElement( 'p', null, new ViewText( 'xxx ' ) )
  145. ] );
  146. const domDiv = converter.viewToDom( viewDiv, document );
  147. expect( domDiv.innerHTML ).to.equal( '<p>foo&nbsp;</p><p>bar</p><p>xxx&nbsp;</p>' );
  148. } );
  149. it( 'when there are multiple spaces next to each other or between attribute elements', () => {
  150. const viewDiv = new ViewContainerElement( 'div', null, [
  151. new ViewText( 'x x x x ' ),
  152. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  153. new ViewAttributeElement( 'i', null,
  154. new ViewAttributeElement( 'b', null,
  155. new ViewAttributeElement( 'u', null, new ViewText( ' x' ) )
  156. )
  157. )
  158. ] );
  159. const domDiv = converter.viewToDom( viewDiv, document );
  160. expect( domDiv.innerHTML ).to.equal( 'x &nbsp;x &nbsp; x x&nbsp;<b> x&nbsp;</b><i><b><u> x</u></b></i>' );
  161. } );
  162. it( 'all together', () => {
  163. const viewDiv = new ViewContainerElement( 'div', null, [
  164. new ViewContainerElement( 'p', null, [
  165. new ViewText( ' x x x x ' ),
  166. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  167. new ViewAttributeElement( 'i', null,
  168. new ViewAttributeElement( 'b', null,
  169. new ViewAttributeElement( 'u', null, new ViewText( ' x ' ) )
  170. )
  171. )
  172. ] ),
  173. new ViewContainerElement( 'p', null, new ViewText( ' x ' ) )
  174. ] );
  175. const domDiv = converter.viewToDom( viewDiv, document );
  176. expect( domDiv.innerHTML ).to.equal(
  177. '<p>&nbsp;x &nbsp;x &nbsp; x x&nbsp;<b> x&nbsp;</b><i><b><u> x&nbsp;</u></b></i></p><p>&nbsp; x &nbsp;</p>'
  178. );
  179. } );
  180. function test( inputTexts, output ) {
  181. if ( typeof inputTexts == 'string' ) {
  182. inputTexts = [ inputTexts ];
  183. }
  184. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  185. const viewElement = new ViewContainerElement( 'p' );
  186. for ( const text of inputTexts ) {
  187. viewElement._appendChild( new ViewText( text.replace( /_/g, '\u00A0' ) ) );
  188. }
  189. const domElement = converter.viewToDom( viewElement, document );
  190. const data = showNbsp( domElement.innerHTML );
  191. expect( data ).to.equal( output );
  192. } );
  193. }
  194. function showNbsp( html ) {
  195. return html.replace( /&nbsp;/g, '_' );
  196. }
  197. // At the beginning.
  198. test( ' x', '_x' );
  199. test( ' x', '_ x' );
  200. test( ' x', '_ _x' );
  201. test( ' x', '_ _ x' );
  202. // At the end.
  203. test( 'x ', 'x_' );
  204. test( 'x ', 'x _' );
  205. test( 'x ', 'x __' );
  206. test( 'x ', 'x _ _' );
  207. // In the middle.
  208. test( 'x x', 'x x' );
  209. test( 'x x', 'x _x' );
  210. test( 'x x', 'x _ x' );
  211. test( 'x x', 'x _ _x' );
  212. // Complex.
  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. // Only spaces.
  219. test( ' ', '_' );
  220. test( ' ', '__' );
  221. test( ' ', '_ _' );
  222. test( ' ', '_ __' );
  223. test( ' ', '_ _ _' );
  224. test( ' ', '_ _ __' );
  225. // With hard &nbsp;
  226. // It should be treated like a normal sign.
  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' );
  234. test( ' _x', '_ _x' );
  235. test( 'x_', 'x_' );
  236. test( 'x_ ', 'x__' );
  237. test( 'x_ ', 'x_ _' );
  238. test( 'x__ ', 'x___' );
  239. test( 'x___', 'x___' );
  240. test( 'x_ _', 'x_ _' );
  241. test( 'x _ ', 'x __' );
  242. test( 'x _', 'x __' );
  243. test( 'x_x', 'x_x' );
  244. test( 'x___x', 'x___x' );
  245. test( 'x__ x', 'x__ x' );
  246. test( 'x_ x', 'x_ _x' );
  247. test( 'x _x', 'x __x' );
  248. test( 'x __x', 'x __x' );
  249. test( 'x _ x', 'x _ x' );
  250. test( 'x _ x', 'x __ _x' );
  251. test( [ 'x', 'y' ], 'xy' );
  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. // Two text nodes.
  265. test( [ 'x ', '_y' ], 'x ___y' );
  266. test( [ 'x ', '_y' ], 'x _ __y' );
  267. test( [ 'x', ' y' ], 'x _y' );
  268. test( [ 'x ', ' y' ], 'x_ _y' );
  269. test( [ 'x ', ' y' ], 'x _ _y' );
  270. test( [ 'x ', ' y' ], 'x __ _y' );
  271. test( [ 'x ', ' y' ], 'x _ _ _y' );
  272. test( [ 'x', ' y' ], 'x _ y' );
  273. test( [ 'x ', ' y' ], 'x_ _ y' );
  274. test( [ 'x ', ' y' ], 'x _ _ y' );
  275. test( [ 'x ', ' y' ], 'x __ _ y' );
  276. test( [ 'x ', ' y' ], 'x _ _ _ y' );
  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' );
  293. test( [ ' ', ' x' ], '__ x' );
  294. test( [ ' ', ' x' ], '_ _ x' );
  295. test( [ ' ', ' x' ], '_ _x' );
  296. test( [ ' ', ' x' ], '__ _x' );
  297. test( [ ' ', ' x' ], '_ _ _x' );
  298. test( [ ' ', ' x' ], '_ _ x' );
  299. test( [ ' ', ' x' ], '__ _ x' );
  300. test( [ ' ', ' x' ], '_ _ _ x' );
  301. // "Non-empty" + "empty" text nodes.
  302. test( [ 'x', ' ', 'x' ], 'x x' );
  303. test( [ 'x', ' ', ' x' ], 'x_ x' );
  304. test( [ 'x', ' ', ' x' ], 'x _ x' );
  305. test( [ 'x', ' ', ' x' ], 'x __ _x' );
  306. test( [ 'x ', ' ', ' x' ], 'x__ x' );
  307. test( [ 'x ', ' ', ' x' ], 'x_ _ x' );
  308. test( [ 'x ', ' ', ' x' ], 'x_ __ _x' );
  309. test( [ 'x ', ' ', ' x' ], 'x __ x' );
  310. test( [ 'x ', ' ', ' x' ], 'x _ _ x' );
  311. test( [ 'x ', ' ', ' x' ], 'x _ __ _x' );
  312. test( [ 'x ', ' ', ' x' ], 'x ___ x' );
  313. test( [ 'x ', ' ', ' x' ], 'x __ _ x' );
  314. test( [ 'x ', ' ', ' x' ], 'x __ __ _x' );
  315. // "Empty" + "empty" text nodes.
  316. test( [ ' ', ' ' ], '__' );
  317. test( [ ' ', ' ' ], '___' );
  318. test( [ ' ', ' ' ], '_ __' );
  319. test( [ ' ', ' ' ], '_ _' );
  320. test( [ ' ', ' ' ], '_ __' );
  321. test( [ ' ', ' ' ], '__ _' );
  322. test( [ ' ', ' ' ], '__ __' );
  323. test( [ ' ', ' ' ], '_ _ _' );
  324. test( [ ' ', ' ' ], '_ _ __' );
  325. it( 'not in preformatted blocks', () => {
  326. const viewPre = new ViewContainerElement( 'pre', null, [ new ViewText( ' foo ' ), new ViewText( ' bar ' ) ] );
  327. const domPre = converter.viewToDom( viewPre, document );
  328. expect( domPre.innerHTML ).to.equal( ' foo bar ' );
  329. } );
  330. it( 'not in a preformatted block followed by a text', () => {
  331. const viewPre = new ViewAttributeElement( 'pre', null, new ViewText( 'foo ' ) );
  332. const viewDiv = new ViewContainerElement( 'div', null, [ viewPre, new ViewText( ' bar' ) ] );
  333. const domDiv = converter.viewToDom( viewDiv, document );
  334. expect( domDiv.innerHTML ).to.equal( '<pre>foo </pre> bar' );
  335. } );
  336. describe( 'around <br>s', () => {
  337. it( 'before <br> – a single space', () => {
  338. const viewDiv = new ViewContainerElement( 'div', null, [
  339. new ViewText( 'foo ' ),
  340. new ViewEmptyElement( 'br' ),
  341. new ViewText( 'bar' )
  342. ] );
  343. const domDiv = converter.viewToDom( viewDiv, document );
  344. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo_<br>bar' );
  345. } );
  346. it( 'before <br> – two spaces', () => {
  347. const viewDiv = new ViewContainerElement( 'div', null, [
  348. new ViewText( 'foo ' ),
  349. new ViewEmptyElement( 'br' ),
  350. new ViewText( 'bar' )
  351. ] );
  352. const domDiv = converter.viewToDom( viewDiv, document );
  353. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo _<br>bar' );
  354. } );
  355. it( 'before <br> – three spaces', () => {
  356. const viewDiv = new ViewContainerElement( 'div', null, [
  357. new ViewText( 'foo ' ),
  358. new ViewEmptyElement( 'br' ),
  359. new ViewText( 'bar' )
  360. ] );
  361. const domDiv = converter.viewToDom( viewDiv, document );
  362. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo __<br>bar' );
  363. } );
  364. it( 'before <br> – only a space', () => {
  365. const viewDiv = new ViewContainerElement( 'div', null, [
  366. new ViewText( ' ' ),
  367. new ViewEmptyElement( 'br' ),
  368. new ViewText( 'bar' )
  369. ] );
  370. const domDiv = converter.viewToDom( viewDiv, document );
  371. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '_<br>bar' );
  372. } );
  373. it( 'before <br> – only two spaces', () => {
  374. const viewDiv = new ViewContainerElement( 'div', null, [
  375. new ViewText( ' ' ),
  376. new ViewEmptyElement( 'br' ),
  377. new ViewText( 'bar' )
  378. ] );
  379. const domDiv = converter.viewToDom( viewDiv, document );
  380. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '__<br>bar' );
  381. } );
  382. it( 'before <br> – only three spaces', () => {
  383. const viewDiv = new ViewContainerElement( 'div', null, [
  384. new ViewText( ' ' ),
  385. new ViewEmptyElement( 'br' ),
  386. new ViewText( 'bar' )
  387. ] );
  388. const domDiv = converter.viewToDom( viewDiv, document );
  389. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '_ _<br>bar' );
  390. } );
  391. it( 'after <br> – a single space', () => {
  392. const viewDiv = new ViewContainerElement( 'div', null, [
  393. new ViewText( 'foo' ),
  394. new ViewEmptyElement( 'br' ),
  395. new ViewText( ' bar' )
  396. ] );
  397. const domDiv = converter.viewToDom( viewDiv, document );
  398. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_bar' );
  399. } );
  400. it( 'after <br> – two spaces', () => {
  401. const viewDiv = new ViewContainerElement( 'div', null, [
  402. new ViewText( 'foo' ),
  403. new ViewEmptyElement( 'br' ),
  404. new ViewText( ' bar' )
  405. ] );
  406. const domDiv = converter.viewToDom( viewDiv, document );
  407. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ bar' );
  408. } );
  409. it( 'after <br> – three spaces', () => {
  410. const viewDiv = new ViewContainerElement( 'div', null, [
  411. new ViewText( 'foo' ),
  412. new ViewEmptyElement( 'br' ),
  413. new ViewText( ' bar' )
  414. ] );
  415. const domDiv = converter.viewToDom( viewDiv, document );
  416. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ _bar' );
  417. } );
  418. it( 'after <br> – only a space', () => {
  419. const viewDiv = new ViewContainerElement( 'div', null, [
  420. new ViewText( 'foo' ),
  421. new ViewEmptyElement( 'br' ),
  422. new ViewText( ' ' )
  423. ] );
  424. const domDiv = converter.viewToDom( viewDiv, document );
  425. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_' );
  426. } );
  427. it( 'after <br> – only two spaces', () => {
  428. const viewDiv = new ViewContainerElement( 'div', null, [
  429. new ViewText( 'foo' ),
  430. new ViewEmptyElement( 'br' ),
  431. new ViewText( ' ' )
  432. ] );
  433. const domDiv = converter.viewToDom( viewDiv, document );
  434. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>__' );
  435. } );
  436. it( 'after <br> – only three spaces', () => {
  437. const viewDiv = new ViewContainerElement( 'div', null, [
  438. new ViewText( 'foo' ),
  439. new ViewEmptyElement( 'br' ),
  440. new ViewText( ' ' )
  441. ] );
  442. const domDiv = converter.viewToDom( viewDiv, document );
  443. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ _' );
  444. } );
  445. it( 'between <br>s – a single space', () => {
  446. const viewDiv = new ViewContainerElement( 'div', null, [
  447. new ViewEmptyElement( 'br' ),
  448. new ViewText( ' ' ),
  449. new ViewEmptyElement( 'br' ),
  450. new ViewText( 'foo' )
  451. ] );
  452. const domDiv = converter.viewToDom( viewDiv, document );
  453. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_<br>foo' );
  454. } );
  455. it( 'between <br>s – only two spaces', () => {
  456. const viewDiv = new ViewContainerElement( 'div', null, [
  457. new ViewEmptyElement( 'br' ),
  458. new ViewText( ' ' ),
  459. new ViewEmptyElement( 'br' ),
  460. new ViewText( 'foo' )
  461. ] );
  462. const domDiv = converter.viewToDom( viewDiv, document );
  463. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>__<br>foo' );
  464. } );
  465. it( 'between <br>s – only three spaces', () => {
  466. const viewDiv = new ViewContainerElement( 'div', null, [
  467. new ViewEmptyElement( 'br' ),
  468. new ViewText( ' ' ),
  469. new ViewEmptyElement( 'br' ),
  470. new ViewText( 'foo' )
  471. ] );
  472. const domDiv = converter.viewToDom( viewDiv, document );
  473. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_ _<br>foo' );
  474. } );
  475. it( 'between <br>s – space and text', () => {
  476. const viewDiv = new ViewContainerElement( 'div', null, [
  477. new ViewEmptyElement( 'br' ),
  478. new ViewText( ' foo' ),
  479. new ViewEmptyElement( 'br' ),
  480. new ViewText( 'foo' )
  481. ] );
  482. const domDiv = converter.viewToDom( viewDiv, document );
  483. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_foo<br>foo' );
  484. } );
  485. it( 'between <br>s – text and space', () => {
  486. const viewDiv = new ViewContainerElement( 'div', null, [
  487. new ViewEmptyElement( 'br' ),
  488. new ViewText( 'foo ' ),
  489. new ViewEmptyElement( 'br' ),
  490. new ViewText( 'foo' )
  491. ] );
  492. const domDiv = converter.viewToDom( viewDiv, document );
  493. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>foo_<br>foo' );
  494. } );
  495. } );
  496. } );
  497. } );
  498. describe( 'viewChildrenToDom()', () => {
  499. it( 'should convert children', () => {
  500. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  501. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  502. expect( domChildren.length ).to.equal( 2 );
  503. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  504. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  505. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  506. } );
  507. it( 'should add filler', () => {
  508. const viewP = parse( '<container:p></container:p>' );
  509. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  510. expect( domChildren.length ).to.equal( 1 );
  511. expect( converter.isBlockFiller( domChildren[ 0 ] ) ).to.be.true;
  512. } );
  513. it( 'should add filler according to fillerPositionOffset', () => {
  514. const viewP = parse( '<container:p>foo</container:p>' );
  515. viewP.getFillerOffset = () => 0;
  516. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  517. expect( domChildren.length ).to.equal( 2 );
  518. expect( converter.isBlockFiller( domChildren[ 0 ] ) ).to.be.true;
  519. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  520. } );
  521. it( 'should pass options', () => {
  522. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  523. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  524. expect( domChildren.length ).to.equal( 2 );
  525. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  526. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  527. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  528. } );
  529. } );
  530. describe( 'viewPositionToDom()', () => {
  531. it( 'should convert the position in the text', () => {
  532. const domFoo = document.createTextNode( 'foo' );
  533. const domP = createElement( document, 'p', null, domFoo );
  534. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  535. converter.bindElements( domP, viewP );
  536. const viewPosition = selection.getFirstPosition();
  537. const domPosition = converter.viewPositionToDom( viewPosition );
  538. expect( domPosition.offset ).to.equal( 2 );
  539. expect( domPosition.parent ).to.equal( domFoo );
  540. } );
  541. it( 'should support unicode', () => {
  542. const domText = document.createTextNode( 'நிலைக்கு' );
  543. const domP = createElement( document, 'p', null, domText );
  544. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  545. converter.bindElements( domP, viewP );
  546. const viewPosition = selection.getFirstPosition();
  547. const domPosition = converter.viewPositionToDom( viewPosition );
  548. expect( domPosition.offset ).to.equal( 4 );
  549. expect( domPosition.parent ).to.equal( domText );
  550. } );
  551. it( 'should convert the position in the empty element', () => {
  552. const domP = createElement( document, 'p' );
  553. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  554. converter.bindElements( domP, viewP );
  555. const viewPosition = selection.getFirstPosition();
  556. const domPosition = converter.viewPositionToDom( viewPosition );
  557. expect( domPosition.offset ).to.equal( 0 );
  558. expect( domPosition.parent ).to.equal( domP );
  559. } );
  560. it( 'should convert the position in the non-empty element', () => {
  561. const domB = createElement( document, 'b', null, 'foo' );
  562. const domP = createElement( document, 'p', null, domB );
  563. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  564. converter.bindElements( domP, viewP );
  565. converter.bindElements( domB, viewP.getChild( 0 ) );
  566. const viewPosition = selection.getFirstPosition();
  567. const domPosition = converter.viewPositionToDom( viewPosition );
  568. expect( domPosition.offset ).to.equal( 1 );
  569. expect( domPosition.parent ).to.equal( domP );
  570. } );
  571. it( 'should convert the position after text', () => {
  572. const domP = createElement( document, 'p', null, 'foo' );
  573. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  574. converter.bindElements( domP, viewP );
  575. const viewPosition = selection.getFirstPosition();
  576. const domPosition = converter.viewPositionToDom( viewPosition );
  577. expect( domPosition.offset ).to.equal( 1 );
  578. expect( domPosition.parent ).to.equal( domP );
  579. } );
  580. it( 'should convert the position before text', () => {
  581. const domP = createElement( document, 'p', null, 'foo' );
  582. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  583. converter.bindElements( domP, viewP );
  584. const viewPosition = selection.getFirstPosition();
  585. const domPosition = converter.viewPositionToDom( viewPosition );
  586. expect( domPosition.offset ).to.equal( 0 );
  587. expect( domPosition.parent ).to.equal( domP );
  588. } );
  589. it( 'should update offset if DOM text node starts with inline filler', () => {
  590. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  591. const domP = createElement( document, 'p', null, domFoo );
  592. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  593. converter.bindElements( domP, viewP );
  594. const viewPosition = selection.getFirstPosition();
  595. const domPosition = converter.viewPositionToDom( viewPosition );
  596. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  597. expect( domPosition.parent ).to.equal( domFoo );
  598. } );
  599. it( 'should move the position to the text node if the position is where inline filler is', () => {
  600. const domFiller = document.createTextNode( INLINE_FILLER );
  601. const domP = createElement( document, 'p', null, domFiller );
  602. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  603. converter.bindElements( domP, viewP );
  604. const viewPosition = selection.getFirstPosition();
  605. const domPosition = converter.viewPositionToDom( viewPosition );
  606. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  607. expect( domPosition.parent ).to.equal( domFiller );
  608. } );
  609. it( 'should return null if view position is after a view element that has not been rendered to DOM', () => {
  610. const domP = createElement( document, 'p', null );
  611. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  612. converter.bindElements( domP, viewP );
  613. const viewPosition = selection.getFirstPosition();
  614. const domPosition = converter.viewPositionToDom( viewPosition );
  615. expect( domPosition ).to.equal( null );
  616. } );
  617. it( 'should return null if view position is in a view text node that has not been rendered to DOM', () => {
  618. const viewText = new ViewText( 'foo' );
  619. const viewPosition = new ViewPosition( viewText, 1 );
  620. const domPosition = converter.viewPositionToDom( viewPosition );
  621. expect( domPosition ).to.equal( null );
  622. } );
  623. it( 'should return null if view position is in a view element that has not been rendered to DOM', () => {
  624. const viewElement = new ViewContainerElement( 'div' );
  625. const viewPosition = new ViewPosition( viewElement, 0 );
  626. const domPosition = converter.viewPositionToDom( viewPosition );
  627. expect( domPosition ).to.equal( null );
  628. } );
  629. } );
  630. describe( 'viewRangeToDom()', () => {
  631. it( 'should convert view range to DOM range', () => {
  632. const domFoo = document.createTextNode( 'foo' );
  633. const domP = createElement( document, 'p', null, domFoo );
  634. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  635. converter.bindElements( domP, viewP );
  636. const viewRange = selection.getFirstRange();
  637. const domRange = converter.viewRangeToDom( viewRange );
  638. expect( domRange ).to.be.instanceof( Range );
  639. expect( domRange.startContainer ).to.equal( domFoo );
  640. expect( domRange.startOffset ).to.equal( 2 );
  641. expect( domRange.endContainer ).to.equal( domP );
  642. expect( domRange.endOffset ).to.equal( 1 );
  643. } );
  644. } );
  645. } );