view-to-dom.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals Range, DocumentFragment, HTMLElement, document, Text */
  6. /* bender-tags: view, domconverter, browser-only */
  7. import ViewText from '/ckeditor5/engine/view/text.js';
  8. import ViewElement from '/ckeditor5/engine/view/element.js';
  9. import ViewContainerElement from '/ckeditor5/engine/view/containerelement.js';
  10. import ViewAttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  11. import DomConverter from '/ckeditor5/engine/view/domconverter.js';
  12. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  13. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller } from '/ckeditor5/engine/view/filler.js';
  14. import { parse } from '/ckeditor5/engine/dev-utils/view.js';
  15. import createElement from '/ckeditor5/utils/dom/createelement.js';
  16. describe( 'DomConverter', () => {
  17. let converter;
  18. before( () => {
  19. converter = new DomConverter();
  20. } );
  21. describe( 'viewToDom', () => {
  22. it( 'should create tree of DOM elements from view elements', () => {
  23. const viewImg = new ViewElement( 'img' );
  24. const viewText = new ViewText( 'foo' );
  25. const viewP = new ViewElement( 'p' );
  26. viewP.setAttribute( 'class', 'foo' );
  27. viewP.appendChildren( viewImg );
  28. viewP.appendChildren( 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.getCorrespondingView( domP ) ).not.to.equal( viewP );
  40. expect( converter.getCorrespondingView( 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' );
  46. viewP.setAttribute( 'class', 'foo' );
  47. viewP.appendChildren( viewImg );
  48. viewP.appendChildren( viewText );
  49. const domP = converter.viewToDom( viewP, document, { bind: true } );
  50. expect( domP ).to.be.an.instanceof( HTMLElement );
  51. expect( domP.tagName ).to.equal( 'P' );
  52. expect( domP.getAttribute( 'class' ) ).to.equal( 'foo' );
  53. expect( domP.attributes.length ).to.equal( 1 );
  54. expect( domP.childNodes.length ).to.equal( 2 );
  55. expect( domP.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  56. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  57. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  58. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  59. } );
  60. it( 'should support unicode', () => {
  61. const viewText = new ViewText( 'நிலைக்கு' );
  62. const viewP = new ViewElement( 'p', null, viewText );
  63. const domP = converter.viewToDom( viewP, document, { bind: true } );
  64. expect( domP.childNodes.length ).to.equal( 1 );
  65. expect( domP.childNodes[ 0 ].data ).to.equal( 'நிலைக்கு' );
  66. expect( converter.getCorrespondingView( domP ) ).to.equal( viewP );
  67. expect( converter.getCorrespondingView( domP.childNodes[ 0 ] ) ).to.equal( viewP.getChild( 0 ) );
  68. } );
  69. it( 'should create tree of DOM elements from view element without children', () => {
  70. const viewImg = new ViewElement( 'img' );
  71. const viewText = new ViewText( 'foo' );
  72. const viewP = new ViewElement( 'p' );
  73. viewP.setAttribute( 'class', 'foo' );
  74. viewP.appendChildren( viewImg );
  75. viewP.appendChildren( 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.getCorrespondingView( 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( 'img' );
  88. const viewText = new ViewText( 'foo' );
  89. const viewFragment = new ViewDocumentFragment();
  90. viewFragment.appendChildren( viewImg );
  91. viewFragment.appendChildren( 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.getCorrespondingView( domFragment ) ).to.equal( viewFragment );
  98. expect( converter.getCorrespondingView( 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( 'img' );
  102. const viewText = new ViewText( 'foo' );
  103. const viewFragment = new ViewDocumentFragment();
  104. viewFragment.appendChildren( viewImg );
  105. viewFragment.appendChildren( 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.getCorrespondingView( domFragment ) ).not.to.equal( viewFragment );
  112. } );
  113. it( 'should return already bind document fragment', () => {
  114. const domFragment = document.createDocumentFragment();
  115. const viewFragment = new ViewDocumentFragment();
  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( 'foo' );
  122. const domTextNode = converter.viewToDom( viewTextNode, document );
  123. expect( domTextNode ).to.be.instanceof( Text );
  124. expect( domTextNode.data ).to.equal( 'foo' );
  125. } );
  126. describe( 'it should convert spaces to  ', () => {
  127. it( 'at the beginning of each container element', () => {
  128. const viewDiv = new ViewContainerElement( 'div', null, [
  129. new ViewContainerElement( 'p', null, new ViewText( ' foo' ) ),
  130. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  131. new ViewContainerElement( 'p', null, new ViewText( ' xxx' ) )
  132. ] );
  133. const domDiv = converter.viewToDom( viewDiv, document );
  134. expect( domDiv.innerHTML ).to.equal( '<p>&nbsp;foo</p><p>bar</p><p>&nbsp;xxx</p>' );
  135. } );
  136. it( 'at the end of each container element', () => {
  137. const viewDiv = new ViewContainerElement( 'div', null, [
  138. new ViewContainerElement( 'p', null, new ViewText( 'foo ' ) ),
  139. new ViewContainerElement( 'p', null, new ViewText( 'bar' ) ),
  140. new ViewContainerElement( 'p', null, new ViewText( 'xxx ' ) )
  141. ] );
  142. const domDiv = converter.viewToDom( viewDiv, document );
  143. expect( domDiv.innerHTML ).to.equal( '<p>foo&nbsp;</p><p>bar</p><p>xxx&nbsp;</p>' );
  144. } );
  145. it( 'when there are multiple spaces next to each other or between attribute elements', () => {
  146. const viewDiv = new ViewContainerElement( 'div', null, [
  147. new ViewText( 'x x x x ' ),
  148. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  149. new ViewAttributeElement( 'i', null,
  150. new ViewAttributeElement( 'b', null,
  151. new ViewAttributeElement( 'u' , null, new ViewText( ' x' ) )
  152. )
  153. )
  154. ] );
  155. const domDiv = converter.viewToDom( viewDiv, document );
  156. expect( domDiv.innerHTML ).to.equal( 'x &nbsp;x &nbsp; x x <b>&nbsp;x </b><i><b><u>&nbsp;x</u></b></i>' );
  157. } );
  158. it( 'all together', () => {
  159. const viewDiv = new ViewContainerElement( 'div', null, [
  160. new ViewContainerElement( 'p', null, [
  161. new ViewText( ' x x x x ' ),
  162. new ViewAttributeElement( 'b', null, new ViewText( ' x ' ) ),
  163. new ViewAttributeElement( 'i', null,
  164. new ViewAttributeElement( 'b', null,
  165. new ViewAttributeElement( 'u' , null, new ViewText( ' x ' ) )
  166. )
  167. )
  168. ] ),
  169. new ViewContainerElement( 'p', null, new ViewText( ' x ' ) )
  170. ] );
  171. const domDiv = converter.viewToDom( viewDiv, document );
  172. expect( domDiv.innerHTML ).to.equal(
  173. '<p>&nbsp;x &nbsp;x &nbsp; x x <b>&nbsp;x </b><i><b><u>&nbsp;x&nbsp;</u></b></i></p><p>&nbsp; x &nbsp;</p>'
  174. );
  175. } );
  176. } );
  177. } );
  178. describe( 'viewChildrenToDom', () => {
  179. it( 'should convert children', () => {
  180. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  181. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  182. expect( domChildren.length ).to.equal( 2 );
  183. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  184. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  185. expect( domChildren[ 1 ].childNodes.length ).to.equal( 1 );
  186. } );
  187. it( 'should add filler', () => {
  188. const viewP = parse( '<container:p></container:p>' );
  189. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  190. expect( domChildren.length ).to.equal( 1 );
  191. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  192. } );
  193. it( 'should add filler according to fillerPositionOffset', () => {
  194. const viewP = parse( '<container:p>foo</container:p>' );
  195. viewP.getFillerOffset = () => 0;
  196. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document ) );
  197. expect( domChildren.length ).to.equal( 2 );
  198. expect( isBlockFiller( domChildren[ 0 ], converter.blockFiller ) ).to.be.true;
  199. expect( domChildren[ 1 ].data ).to.equal( 'foo' );
  200. } );
  201. it( 'should pass options', () => {
  202. const viewP = parse( '<container:p>foo<attribute:b>bar</attribute:b></container:p>' );
  203. const domChildren = Array.from( converter.viewChildrenToDom( viewP, document, { withChildren: false } ) );
  204. expect( domChildren.length ).to.equal( 2 );
  205. expect( domChildren[ 0 ].data ).to.equal( 'foo' );
  206. expect( domChildren[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  207. expect( domChildren[ 1 ].childNodes.length ).to.equal( 0 );
  208. } );
  209. } );
  210. describe( 'viewPositionToDom', () => {
  211. it( 'should convert the position in the text', () => {
  212. const domFoo = document.createTextNode( 'foo' );
  213. const domP = createElement( document, 'p', null, domFoo );
  214. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  215. converter.bindElements( domP, viewP );
  216. const viewPosition = selection.getFirstPosition();
  217. const domPosition = converter.viewPositionToDom( viewPosition );
  218. expect( domPosition.offset ).to.equal( 2 );
  219. expect( domPosition.parent ).to.equal( domFoo );
  220. } );
  221. it( 'should support unicode', () => {
  222. const domText = document.createTextNode( 'நிலைக்கு' );
  223. const domP = createElement( document, 'p', null, domText );
  224. const { view: viewP, selection } = parse( '<container:p>நிலை{}க்கு</container:p>' );
  225. converter.bindElements( domP, viewP );
  226. const viewPosition = selection.getFirstPosition();
  227. const domPosition = converter.viewPositionToDom( viewPosition );
  228. expect( domPosition.offset ).to.equal( 4 );
  229. expect( domPosition.parent ).to.equal( domText );
  230. } );
  231. it( 'should convert the position in the empty element', () => {
  232. const domP = createElement( document, 'p' );
  233. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  234. converter.bindElements( domP, viewP );
  235. const viewPosition = selection.getFirstPosition();
  236. const domPosition = converter.viewPositionToDom( viewPosition );
  237. expect( domPosition.offset ).to.equal( 0 );
  238. expect( domPosition.parent ).to.equal( domP );
  239. } );
  240. it( 'should convert the position in the non-empty element', () => {
  241. const domB = createElement( document, 'b', null, 'foo' );
  242. const domP = createElement( document, 'p', null, domB );
  243. const { view: viewP, selection } = parse( '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  244. converter.bindElements( domP, viewP );
  245. converter.bindElements( domB, viewP.getChild( 0 ) );
  246. const viewPosition = selection.getFirstPosition();
  247. const domPosition = converter.viewPositionToDom( viewPosition );
  248. expect( domPosition.offset ).to.equal( 1 );
  249. expect( domPosition.parent ).to.equal( domP );
  250. } );
  251. it( 'should convert the position after text', () => {
  252. const domP = createElement( document, 'p', null, 'foo' );
  253. const { view: viewP, selection } = parse( '<container:p>foo[]</container:p>' );
  254. converter.bindElements( domP, viewP );
  255. const viewPosition = selection.getFirstPosition();
  256. const domPosition = converter.viewPositionToDom( viewPosition );
  257. expect( domPosition.offset ).to.equal( 1 );
  258. expect( domPosition.parent ).to.equal( domP );
  259. } );
  260. it( 'should convert the position before text', () => {
  261. const domP = createElement( document, 'p', null, 'foo' );
  262. const { view: viewP, selection } = parse( '<container:p>[]foo</container:p>' );
  263. converter.bindElements( domP, viewP );
  264. const viewPosition = selection.getFirstPosition();
  265. const domPosition = converter.viewPositionToDom( viewPosition );
  266. expect( domPosition.offset ).to.equal( 0 );
  267. expect( domPosition.parent ).to.equal( domP );
  268. } );
  269. it( 'should update offset if DOM text node starts with inline filler', () => {
  270. const domFoo = document.createTextNode( INLINE_FILLER + 'foo' );
  271. const domP = createElement( document, 'p', null, domFoo );
  272. const { view: viewP, selection } = parse( '<container:p>fo{}o</container:p>' );
  273. converter.bindElements( domP, viewP );
  274. const viewPosition = selection.getFirstPosition();
  275. const domPosition = converter.viewPositionToDom( viewPosition );
  276. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH + 2 );
  277. expect( domPosition.parent ).to.equal( domFoo );
  278. } );
  279. it( 'should move the position to the text node if the position is where inline filler is', () => {
  280. const domFiller = document.createTextNode( INLINE_FILLER );
  281. const domP = createElement( document, 'p', null, domFiller );
  282. const { view: viewP, selection } = parse( '<container:p>[]</container:p>' );
  283. converter.bindElements( domP, viewP );
  284. const viewPosition = selection.getFirstPosition();
  285. const domPosition = converter.viewPositionToDom( viewPosition );
  286. expect( domPosition.offset ).to.equal( INLINE_FILLER_LENGTH );
  287. expect( domPosition.parent ).to.equal( domFiller );
  288. } );
  289. } );
  290. describe( 'viewRangeToDom', () => {
  291. it( 'should convert view range to DOM range', () => {
  292. const domFoo = document.createTextNode( 'foo' );
  293. const domP = createElement( document, 'p', null, domFoo );
  294. const { view: viewP, selection } = parse( '<container:p>fo{o]</container:p>' );
  295. converter.bindElements( domP, viewP );
  296. const viewRange = selection.getFirstRange();
  297. const domRange = converter.viewRangeToDom( viewRange );
  298. expect( domRange ).to.be.instanceof( Range );
  299. expect( domRange.startContainer ).to.equal( domFoo );
  300. expect( domRange.startOffset ).to.equal( 2 );
  301. expect( domRange.endContainer ).to.equal( domP );
  302. expect( domRange.endOffset ).to.equal( 1 );
  303. } );
  304. } );
  305. } );