8
0

view-to-dom.js 31 KB

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