view-to-dom.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. describe( 'it should convert spaces to  ', () => {
  125. it( 'at the beginning of each container element', () => {
  126. const viewDiv = new ViewContainerElement( 'div', null, [
  127. new ViewContainerElement( 'p', null, new ViewText( ' foo' ) ),
  128. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  129. new ViewContainerElement( 'p', null, new ViewText( ' xxx' ) )
  130. ] );
  131. const domDiv = converter.viewToDom( viewDiv, document );
  132. expect( domDiv.innerHTML ).to.equal( '<p>&nbsp;foo</p><p>bar</p><p>&nbsp;xxx</p>' );
  133. } );
  134. it( 'at the end of each container element', () => {
  135. const viewDiv = new ViewContainerElement( 'div', null, [
  136. new ViewContainerElement( 'p', null, new ViewText( 'foo ' ) ),
  137. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  138. new ViewContainerElement( 'p', null, new ViewText( 'xxx ' ) )
  139. ] );
  140. const domDiv = converter.viewToDom( viewDiv, document );
  141. expect( domDiv.innerHTML ).to.equal( '<p>foo&nbsp;</p><p>bar</p><p>xxx&nbsp;</p>' );
  142. } );
  143. it( 'when there are multiple spaces next to each other or between attribute elements', () => {
  144. const viewDiv = new ViewContainerElement( 'div', null, [
  145. new ViewText( 'x x x x ' ),
  146. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  147. new ViewAttributeElement( 'i', null,
  148. new ViewAttributeElement( 'b', null,
  149. new ViewAttributeElement( 'u', null, new ViewText( ' x' ) )
  150. )
  151. )
  152. ] );
  153. const domDiv = converter.viewToDom( viewDiv, document );
  154. expect( domDiv.innerHTML ).to.equal( 'x &nbsp;x &nbsp; x x&nbsp;<b> x&nbsp;</b><i><b><u> x</u></b></i>' );
  155. } );
  156. it( 'all together', () => {
  157. const viewDiv = new ViewContainerElement( 'div', null, [
  158. new ViewContainerElement( 'p', null, [
  159. new ViewText( ' x x x x ' ),
  160. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  161. new ViewAttributeElement( 'i', null,
  162. new ViewAttributeElement( 'b', null,
  163. new ViewAttributeElement( 'u', null, new ViewText( ' x ' ) )
  164. )
  165. )
  166. ] ),
  167. new ViewContainerElement( 'p', null, new ViewText( ' x ' ) )
  168. ] );
  169. const domDiv = converter.viewToDom( viewDiv, document );
  170. expect( domDiv.innerHTML ).to.equal(
  171. '<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>'
  172. );
  173. } );
  174. function test( inputTexts, output ) {
  175. if ( typeof inputTexts == 'string' ) {
  176. inputTexts = [ inputTexts ];
  177. }
  178. it( 'spaces in a text node: ' + inputTexts.join( '|' ) + ' -> ' + output, () => {
  179. const viewElement = new ViewContainerElement( 'p' );
  180. for ( const text of inputTexts ) {
  181. viewElement._appendChild( new ViewText( text.replace( /_/g, '\u00A0' ) ) );
  182. }
  183. const domElement = converter.viewToDom( viewElement, document );
  184. const data = showNbsp( domElement.innerHTML );
  185. expect( data ).to.equal( output );
  186. } );
  187. }
  188. function showNbsp( html ) {
  189. return html.replace( /&nbsp;/g, '_' );
  190. }
  191. // At the beginning.
  192. test( ' x', '_x' );
  193. test( ' x', '_ x' );
  194. test( ' x', '_ _x' );
  195. test( ' x', '_ _ x' );
  196. // At the end.
  197. test( 'x ', 'x_' );
  198. test( 'x ', 'x _' );
  199. test( 'x ', 'x __' );
  200. test( 'x ', 'x _ _' );
  201. // In the middle.
  202. test( 'x x', 'x x' );
  203. test( 'x x', 'x _x' );
  204. test( 'x x', 'x _ x' );
  205. test( 'x x', 'x _ _x' );
  206. // Complex.
  207. test( ' x ', '_x_' );
  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. // Only spaces.
  213. test( ' ', '_' );
  214. test( ' ', '__' );
  215. test( ' ', '_ _' );
  216. test( ' ', '_ __' );
  217. test( ' ', '_ _ _' );
  218. test( ' ', '_ _ __' );
  219. // With hard &nbsp;
  220. // It should be treated like a normal sign.
  221. test( '_x', '_x' );
  222. test( ' _x', '__x' );
  223. test( ' _x', '_ _x' );
  224. test( ' __x', '___x' );
  225. test( '___x', '___x' );
  226. test( '_ _x', '_ _x' );
  227. test( ' _ x', '__ x' );
  228. test( ' _x', '_ _x' );
  229. test( 'x_', 'x_' );
  230. test( 'x_ ', 'x__' );
  231. test( 'x_ ', 'x_ _' );
  232. test( 'x__ ', 'x___' );
  233. test( 'x___', 'x___' );
  234. test( 'x_ _', 'x_ _' );
  235. test( 'x _ ', 'x __' );
  236. test( 'x _', 'x __' );
  237. test( 'x_x', 'x_x' );
  238. test( 'x___x', 'x___x' );
  239. test( 'x__ x', 'x__ x' );
  240. test( 'x_ x', 'x_ _x' );
  241. test( 'x _x', 'x __x' );
  242. test( 'x __x', 'x __x' );
  243. test( 'x _ x', 'x _ x' );
  244. test( 'x _ x', 'x __ _x' );
  245. test( [ 'x', 'y' ], 'xy' );
  246. test( [ 'x ', 'y' ], 'x y' );
  247. test( [ 'x ', 'y' ], 'x _y' );
  248. test( [ 'x ', 'y' ], 'x __y' );
  249. test( [ 'x ', 'y' ], 'x _ _y' );
  250. test( [ 'x', ' y' ], 'x y' );
  251. test( [ 'x ', ' y' ], 'x_ y' );
  252. test( [ 'x ', ' y' ], 'x _ y' );
  253. test( [ 'x ', ' y' ], 'x __ y' );
  254. test( [ 'x ', ' y' ], 'x _ _ y' );
  255. test( [ 'x', '_y' ], 'x_y' );
  256. test( [ 'x ', '_y' ], 'x _y' );
  257. test( [ 'x ', '_y' ], 'x __y' );
  258. // Two text nodes.
  259. test( [ 'x ', '_y' ], 'x ___y' );
  260. test( [ 'x ', '_y' ], 'x _ __y' );
  261. test( [ 'x', ' y' ], 'x _y' );
  262. test( [ 'x ', ' y' ], 'x_ _y' );
  263. test( [ 'x ', ' y' ], 'x _ _y' );
  264. test( [ 'x ', ' y' ], 'x __ _y' );
  265. test( [ 'x ', ' y' ], 'x _ _ _y' );
  266. test( [ 'x', ' y' ], 'x _ y' );
  267. 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', ' ' ], 'x_' );
  272. test( [ 'x', ' ' ], 'x _' );
  273. test( [ 'x', ' ' ], 'x __' );
  274. test( [ 'x ', ' ' ], 'x__' );
  275. test( [ 'x ', ' ' ], 'x_ _' );
  276. test( [ 'x ', ' ' ], 'x_ __' );
  277. test( [ 'x ', ' ' ], 'x __' );
  278. test( [ 'x ', ' ' ], 'x _ _' );
  279. test( [ 'x ', ' ' ], 'x _ __' );
  280. test( [ 'x ', ' ' ], 'x ___' );
  281. test( [ 'x ', ' ' ], 'x __ _' );
  282. test( [ 'x ', ' ' ], 'x __ __' );
  283. test( [ ' ', 'x' ], '_x' );
  284. test( [ ' ', 'x' ], '_ x' );
  285. test( [ ' ', 'x' ], '_ _x' );
  286. test( [ ' ', ' x' ], '_ x' );
  287. test( [ ' ', ' x' ], '__ x' );
  288. test( [ ' ', ' x' ], '_ _ x' );
  289. test( [ ' ', ' x' ], '_ _x' );
  290. test( [ ' ', ' x' ], '__ _x' );
  291. test( [ ' ', ' x' ], '_ _ _x' );
  292. test( [ ' ', ' x' ], '_ _ x' );
  293. test( [ ' ', ' x' ], '__ _ x' );
  294. test( [ ' ', ' x' ], '_ _ _ x' );
  295. // "Non-empty" + "empty" text nodes.
  296. test( [ 'x', ' ', 'x' ], 'x x' );
  297. test( [ 'x', ' ', ' x' ], 'x_ x' );
  298. test( [ 'x', ' ', ' x' ], 'x _ x' );
  299. test( [ 'x', ' ', ' x' ], 'x __ _x' );
  300. test( [ 'x ', ' ', ' x' ], 'x__ x' );
  301. test( [ 'x ', ' ', ' x' ], 'x_ _ x' );
  302. test( [ 'x ', ' ', ' x' ], 'x_ __ _x' );
  303. test( [ 'x ', ' ', ' x' ], 'x __ x' );
  304. test( [ 'x ', ' ', ' x' ], 'x _ _ x' );
  305. test( [ 'x ', ' ', ' x' ], 'x _ __ _x' );
  306. test( [ 'x ', ' ', ' x' ], 'x ___ x' );
  307. test( [ 'x ', ' ', ' x' ], 'x __ _ x' );
  308. test( [ 'x ', ' ', ' x' ], 'x __ __ _x' );
  309. // "Empty" + "empty" text nodes.
  310. test( [ ' ', ' ' ], '__' );
  311. test( [ ' ', ' ' ], '___' );
  312. test( [ ' ', ' ' ], '_ __' );
  313. test( [ ' ', ' ' ], '_ _' );
  314. test( [ ' ', ' ' ], '_ __' );
  315. test( [ ' ', ' ' ], '__ _' );
  316. test( [ ' ', ' ' ], '__ __' );
  317. test( [ ' ', ' ' ], '_ _ _' );
  318. test( [ ' ', ' ' ], '_ _ __' );
  319. it( 'not in preformatted blocks', () => {
  320. const viewPre = new ViewContainerElement( 'pre', null, [ new ViewText( ' foo ' ), new ViewText( ' bar ' ) ] );
  321. const domPre = converter.viewToDom( viewPre, document );
  322. expect( domPre.innerHTML ).to.equal( ' foo bar ' );
  323. } );
  324. it( 'not in a preformatted block followed by a text', () => {
  325. const viewPre = new ViewAttributeElement( 'pre', null, new ViewText( 'foo ' ) );
  326. const viewDiv = new ViewContainerElement( 'div', null, [ viewPre, new ViewText( ' bar' ) ] );
  327. const domDiv = converter.viewToDom( viewDiv, document );
  328. expect( domDiv.innerHTML ).to.equal( '<pre>foo </pre> bar' );
  329. } );
  330. describe( 'around <br>s', () => {
  331. it( 'before <br> – a single space', () => {
  332. const viewDiv = new ViewContainerElement( 'div', null, [
  333. new ViewText( 'foo ' ),
  334. new ViewEmptyElement( 'br' ),
  335. new ViewText( 'bar' )
  336. ] );
  337. const domDiv = converter.viewToDom( viewDiv, document );
  338. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo_<br>bar' );
  339. } );
  340. it( 'before <br> – two spaces', () => {
  341. const viewDiv = new ViewContainerElement( 'div', null, [
  342. new ViewText( 'foo ' ),
  343. new ViewEmptyElement( 'br' ),
  344. new ViewText( 'bar' )
  345. ] );
  346. const domDiv = converter.viewToDom( viewDiv, document );
  347. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo _<br>bar' );
  348. } );
  349. it( 'before <br> – three spaces', () => {
  350. const viewDiv = new ViewContainerElement( 'div', null, [
  351. new ViewText( 'foo ' ),
  352. new ViewEmptyElement( 'br' ),
  353. new ViewText( 'bar' )
  354. ] );
  355. const domDiv = converter.viewToDom( viewDiv, document );
  356. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo __<br>bar' );
  357. } );
  358. it( 'before <br> – only a space', () => {
  359. const viewDiv = new ViewContainerElement( 'div', null, [
  360. new ViewText( ' ' ),
  361. new ViewEmptyElement( 'br' ),
  362. new ViewText( 'bar' )
  363. ] );
  364. const domDiv = converter.viewToDom( viewDiv, document );
  365. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '_<br>bar' );
  366. } );
  367. it( 'before <br> – only two spaces', () => {
  368. const viewDiv = new ViewContainerElement( 'div', null, [
  369. new ViewText( ' ' ),
  370. new ViewEmptyElement( 'br' ),
  371. new ViewText( 'bar' )
  372. ] );
  373. const domDiv = converter.viewToDom( viewDiv, document );
  374. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '__<br>bar' );
  375. } );
  376. it( 'before <br> – only three spaces', () => {
  377. const viewDiv = new ViewContainerElement( 'div', null, [
  378. new ViewText( ' ' ),
  379. new ViewEmptyElement( 'br' ),
  380. new ViewText( 'bar' )
  381. ] );
  382. const domDiv = converter.viewToDom( viewDiv, document );
  383. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '_ _<br>bar' );
  384. } );
  385. it( 'after <br> – a single space', () => {
  386. const viewDiv = new ViewContainerElement( 'div', null, [
  387. new ViewText( 'foo' ),
  388. new ViewEmptyElement( 'br' ),
  389. new ViewText( ' bar' )
  390. ] );
  391. const domDiv = converter.viewToDom( viewDiv, document );
  392. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_bar' );
  393. } );
  394. it( 'after <br> – two spaces', () => {
  395. const viewDiv = new ViewContainerElement( 'div', null, [
  396. new ViewText( 'foo' ),
  397. new ViewEmptyElement( 'br' ),
  398. new ViewText( ' bar' )
  399. ] );
  400. const domDiv = converter.viewToDom( viewDiv, document );
  401. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ bar' );
  402. } );
  403. it( 'after <br> – three spaces', () => {
  404. const viewDiv = new ViewContainerElement( 'div', null, [
  405. new ViewText( 'foo' ),
  406. new ViewEmptyElement( 'br' ),
  407. new ViewText( ' bar' )
  408. ] );
  409. const domDiv = converter.viewToDom( viewDiv, document );
  410. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ _bar' );
  411. } );
  412. it( 'after <br> – only a space', () => {
  413. const viewDiv = new ViewContainerElement( 'div', null, [
  414. new ViewText( 'foo' ),
  415. new ViewEmptyElement( 'br' ),
  416. new ViewText( ' ' )
  417. ] );
  418. const domDiv = converter.viewToDom( viewDiv, document );
  419. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_' );
  420. } );
  421. it( 'after <br> – only two spaces', () => {
  422. const viewDiv = new ViewContainerElement( 'div', null, [
  423. new ViewText( 'foo' ),
  424. new ViewEmptyElement( 'br' ),
  425. new ViewText( ' ' )
  426. ] );
  427. const domDiv = converter.viewToDom( viewDiv, document );
  428. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>__' );
  429. } );
  430. it( 'after <br> – only three spaces', () => {
  431. const viewDiv = new ViewContainerElement( 'div', null, [
  432. new ViewText( 'foo' ),
  433. new ViewEmptyElement( 'br' ),
  434. new ViewText( ' ' )
  435. ] );
  436. const domDiv = converter.viewToDom( viewDiv, document );
  437. expect( showNbsp( domDiv.innerHTML ) ).to.equal( 'foo<br>_ _' );
  438. } );
  439. it( 'between <br>s – a single space', () => {
  440. const viewDiv = new ViewContainerElement( 'div', null, [
  441. new ViewEmptyElement( 'br' ),
  442. new ViewText( ' ' ),
  443. new ViewEmptyElement( 'br' ),
  444. new ViewText( 'foo' )
  445. ] );
  446. const domDiv = converter.viewToDom( viewDiv, document );
  447. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_<br>foo' );
  448. } );
  449. it( 'between <br>s – only two spaces', () => {
  450. const viewDiv = new ViewContainerElement( 'div', null, [
  451. new ViewEmptyElement( 'br' ),
  452. new ViewText( ' ' ),
  453. new ViewEmptyElement( 'br' ),
  454. new ViewText( 'foo' )
  455. ] );
  456. const domDiv = converter.viewToDom( viewDiv, document );
  457. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>__<br>foo' );
  458. } );
  459. it( 'between <br>s – only three spaces', () => {
  460. const viewDiv = new ViewContainerElement( 'div', null, [
  461. new ViewEmptyElement( 'br' ),
  462. new ViewText( ' ' ),
  463. new ViewEmptyElement( 'br' ),
  464. new ViewText( 'foo' )
  465. ] );
  466. const domDiv = converter.viewToDom( viewDiv, document );
  467. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_ _<br>foo' );
  468. } );
  469. it( 'between <br>s – space and text', () => {
  470. const viewDiv = new ViewContainerElement( 'div', null, [
  471. new ViewEmptyElement( 'br' ),
  472. new ViewText( ' foo' ),
  473. new ViewEmptyElement( 'br' ),
  474. new ViewText( 'foo' )
  475. ] );
  476. const domDiv = converter.viewToDom( viewDiv, document );
  477. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>_foo<br>foo' );
  478. } );
  479. it( 'between <br>s – text and space', () => {
  480. const viewDiv = new ViewContainerElement( 'div', null, [
  481. new ViewEmptyElement( 'br' ),
  482. new ViewText( 'foo ' ),
  483. new ViewEmptyElement( 'br' ),
  484. new ViewText( 'foo' )
  485. ] );
  486. const domDiv = converter.viewToDom( viewDiv, document );
  487. expect( showNbsp( domDiv.innerHTML ) ).to.equal( '<br>foo_<br>foo' );
  488. } );
  489. } );
  490. } );
  491. } );
  492. describe( 'viewChildrenToDom()', () => {
  493. it( 'should convert children', () => {
  494. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  495. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  496. expect( domChildren.length ).to.equal( 2 );
  497. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  498. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  499. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  500. } );
  501. it( 'should add filler', () => {
  502. const viewP = parse( '<container:p></container:p>' );
  503. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  504. expect( domChildren.length ).to.equal( 1 );
  505. expect( converter.isBlockFiller( domChildren[ 0 ] ) ).to.be.true;
  506. } );
  507. it( 'should add filler according to fillerPositionOffset', () => {
  508. const viewP = parse( '<container:p>foo</container:p>' );
  509. viewP.getFillerOffset = () => 0;
  510. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  511. expect( domChildren.length ).to.equal( 2 );
  512. expect( converter.isBlockFiller( domChildren[ 0 ] ) ).to.be.true;
  513. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  514. } );
  515. it( 'should pass options', () => {
  516. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  517. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  518. expect( domChildren.length ).to.equal( 2 );
  519. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  520. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  521. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  522. } );
  523. } );
  524. describe( 'viewPositionToDom()', () => {
  525. it( 'should convert the position in the text', () => {
  526. const domFoo = document.createTextNode( 'foo' );
  527. const domP = createElement( document, 'p', null, domFoo );
  528. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  529. converter.bindElements( domP, viewP );
  530. const viewPosition = selection.getFirstPosition();
  531. const domPosition = converter.viewPositionToDom( viewPosition );
  532. expect( domPosition.offset ).to.equal( 2 );
  533. expect( domPosition.parent ).to.equal( domFoo );
  534. } );
  535. it( 'should support unicode', () => {
  536. const domText = document.createTextNode( 'நிலைக்கு' );
  537. const domP = createElement( document, 'p', null, domText );
  538. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  539. converter.bindElements( domP, viewP );
  540. const viewPosition = selection.getFirstPosition();
  541. const domPosition = converter.viewPositionToDom( viewPosition );
  542. expect( domPosition.offset ).to.equal( 4 );
  543. expect( domPosition.parent ).to.equal( domText );
  544. } );
  545. it( 'should convert the position in the empty element', () => {
  546. const domP = createElement( document, 'p' );
  547. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  548. converter.bindElements( domP, viewP );
  549. const viewPosition = selection.getFirstPosition();
  550. const domPosition = converter.viewPositionToDom( viewPosition );
  551. expect( domPosition.offset ).to.equal( 0 );
  552. expect( domPosition.parent ).to.equal( domP );
  553. } );
  554. it( 'should convert the position in the non-empty element', () => {
  555. const domB = createElement( document, 'b', null, 'foo' );
  556. const domP = createElement( document, 'p', null, domB );
  557. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  558. converter.bindElements( domP, viewP );
  559. converter.bindElements( domB, viewP.getChild( 0 ) );
  560. const viewPosition = selection.getFirstPosition();
  561. const domPosition = converter.viewPositionToDom( viewPosition );
  562. expect( domPosition.offset ).to.equal( 1 );
  563. expect( domPosition.parent ).to.equal( domP );
  564. } );
  565. it( 'should convert the position after text', () => {
  566. const domP = createElement( document, 'p', null, 'foo' );
  567. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  568. converter.bindElements( domP, viewP );
  569. const viewPosition = selection.getFirstPosition();
  570. const domPosition = converter.viewPositionToDom( viewPosition );
  571. expect( domPosition.offset ).to.equal( 1 );
  572. expect( domPosition.parent ).to.equal( domP );
  573. } );
  574. it( 'should convert the position before text', () => {
  575. const domP = createElement( document, 'p', null, 'foo' );
  576. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  577. converter.bindElements( domP, viewP );
  578. const viewPosition = selection.getFirstPosition();
  579. const domPosition = converter.viewPositionToDom( viewPosition );
  580. expect( domPosition.offset ).to.equal( 0 );
  581. expect( domPosition.parent ).to.equal( domP );
  582. } );
  583. it( 'should update offset if DOM text node starts with inline filler', () => {
  584. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  585. const domP = createElement( document, 'p', null, domFoo );
  586. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  587. converter.bindElements( domP, viewP );
  588. const viewPosition = selection.getFirstPosition();
  589. const domPosition = converter.viewPositionToDom( viewPosition );
  590. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  591. expect( domPosition.parent ).to.equal( domFoo );
  592. } );
  593. it( 'should move the position to the text node if the position is where inline filler is', () => {
  594. const domFiller = document.createTextNode( INLINE_FILLER );
  595. const domP = createElement( document, 'p', null, domFiller );
  596. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  597. converter.bindElements( domP, viewP );
  598. const viewPosition = selection.getFirstPosition();
  599. const domPosition = converter.viewPositionToDom( viewPosition );
  600. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  601. expect( domPosition.parent ).to.equal( domFiller );
  602. } );
  603. it( 'should return null if view position is after a view element that has not been rendered to DOM', () => {
  604. const domP = createElement( document, 'p', null );
  605. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  606. converter.bindElements( domP, viewP );
  607. const viewPosition = selection.getFirstPosition();
  608. const domPosition = converter.viewPositionToDom( viewPosition );
  609. expect( domPosition ).to.equal( null );
  610. } );
  611. it( 'should return null if view position is in a view text node that has not been rendered to DOM', () => {
  612. const viewText = new ViewText( 'foo' );
  613. const viewPosition = new ViewPosition( viewText, 1 );
  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 element that has not been rendered to DOM', () => {
  618. const viewElement = new ViewContainerElement( 'div' );
  619. const viewPosition = new ViewPosition( viewElement, 0 );
  620. const domPosition = converter.viewPositionToDom( viewPosition );
  621. expect( domPosition ).to.equal( null );
  622. } );
  623. } );
  624. describe( 'viewRangeToDom()', () => {
  625. it( 'should convert view range to DOM range', () => {
  626. const domFoo = document.createTextNode( 'foo' );
  627. const domP = createElement( document, 'p', null, domFoo );
  628. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  629. converter.bindElements( domP, viewP );
  630. const viewRange = selection.getFirstRange();
  631. const domRange = converter.viewRangeToDom( viewRange );
  632. expect( domRange ).to.be.instanceof( Range );
  633. expect( domRange.startContainer ).to.equal( domFoo );
  634. expect( domRange.startOffset ).to.equal( 2 );
  635. expect( domRange.endContainer ).to.equal( domP );
  636. expect( domRange.endOffset ).to.equal( 1 );
  637. } );
  638. } );
  639. } );