upcastwriter.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Element from '../../src/view/element';
  6. import UpcastWriter from '../../src/view/upcastwriter';
  7. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  8. describe( 'UpcastWriter', () => {
  9. let writer, view, dataprocessor;
  10. before( () => {
  11. writer = new UpcastWriter();
  12. dataprocessor = new HtmlDataProcessor();
  13. } );
  14. beforeEach( () => {
  15. const html = '' +
  16. '<h1 style="color:blue;position:fixed;">Heading <strong>1</strong></h1>' +
  17. '<p class="foo1 bar2" style="text-align:left;" data-attr="abc">Foo <i>Bar</i> <strong>Bold</strong></p>' +
  18. '<p><u>Some underlined</u> text</p>' +
  19. '<ul>' +
  20. '<li class="single">Item 1</li>' +
  21. '<li><span>Item <s>1</s></span></li>' +
  22. '<li><h2>Item 1</h2></li>' +
  23. '</ul>';
  24. view = dataprocessor.toView( html );
  25. } );
  26. describe( 'clone', () => {
  27. it( 'should clone simple element', () => {
  28. const el = view.getChild( 0 );
  29. const clone = writer.clone( el );
  30. expect( clone ).to.not.equal( el );
  31. expect( clone.isSimilar( el ) ).to.true;
  32. expect( clone.childCount ).to.equal( 0 );
  33. } );
  34. it( 'should clone element with all attributes', () => {
  35. const el = view.getChild( 1 );
  36. const clone = writer.clone( el );
  37. expect( clone ).to.not.equal( el );
  38. expect( clone.isSimilar( el ) ).to.true;
  39. expect( clone.childCount ).to.equal( 0 );
  40. } );
  41. it( 'should deep clone element', () => {
  42. const el = view.getChild( 0 );
  43. const clone = writer.clone( el, true );
  44. expect( clone ).to.not.equal( el );
  45. expect( clone.isSimilar( el ) ).to.true;
  46. expect( clone.childCount ).to.equal( el.childCount );
  47. } );
  48. } );
  49. describe( 'appendChild', () => {
  50. it( 'should append inline child to paragraph', () => {
  51. const el = view.getChild( 2 );
  52. const newChild = new Element( 'span' );
  53. const appended = writer.appendChild( newChild, el );
  54. expect( appended ).to.equal( 1 );
  55. expect( newChild.parent ).to.equal( el );
  56. expect( el.childCount ).to.equal( 3 );
  57. } );
  58. it( 'should append block children to paragraph', () => {
  59. const el = view.getChild( 2 );
  60. const newChild1 = new Element( 'p' );
  61. const newChild2 = new Element( 'h2' );
  62. const appended = writer.appendChild( [ newChild1, newChild2 ], el );
  63. expect( appended ).to.equal( 2 );
  64. expect( newChild1.parent ).to.equal( el );
  65. expect( newChild2.parent ).to.equal( el );
  66. expect( el.childCount ).to.equal( 4 );
  67. } );
  68. it( 'should append list item to the list', () => {
  69. const el = view.getChild( 3 );
  70. const newChild = new Element( 'li' );
  71. const appended = writer.appendChild( newChild, el );
  72. expect( appended ).to.equal( 1 );
  73. expect( newChild.parent ).to.equal( el );
  74. expect( el.childCount ).to.equal( 4 );
  75. } );
  76. it( 'should append element to DocumentFragment element', () => {
  77. const newChild = new Element( 'p' );
  78. const appended = writer.appendChild( newChild, view );
  79. expect( appended ).to.equal( 1 );
  80. expect( newChild.parent ).to.equal( view );
  81. expect( view.childCount ).to.equal( 5 );
  82. } );
  83. } );
  84. describe( 'insertChild', () => {
  85. it( 'should insert inline child into the paragraph on the first position', () => {
  86. const el = view.getChild( 2 );
  87. const newChild = new Element( 'span' );
  88. const inserted = writer.insertChild( 0, newChild, el );
  89. expect( inserted ).to.equal( 1 );
  90. expect( newChild.parent ).to.equal( el );
  91. expect( el.getChild( 0 ) ).to.equal( newChild );
  92. expect( el.childCount ).to.equal( 3 );
  93. } );
  94. it( 'should insert block children into the paragraph on the last position', () => {
  95. const el = view.getChild( 2 );
  96. const newChild1 = new Element( 'blockquote' );
  97. const newChild2 = new Element( 'h2' );
  98. const inserted = writer.insertChild( 2, [ newChild1, newChild2 ], el );
  99. expect( inserted ).to.equal( 2 );
  100. expect( newChild1.parent ).to.equal( el );
  101. expect( newChild2.parent ).to.equal( el );
  102. expect( el.getChild( 2 ) ).to.equal( newChild1 );
  103. expect( el.getChild( 3 ) ).to.equal( newChild2 );
  104. expect( el.childCount ).to.equal( 4 );
  105. } );
  106. it( 'should insert list item into the list element', () => {
  107. const el = view.getChild( 3 );
  108. const newChild = new Element( 'li' );
  109. const inserted = writer.insertChild( 1, newChild, el );
  110. expect( inserted ).to.equal( 1 );
  111. expect( newChild.parent ).to.equal( el );
  112. expect( el.getChild( 1 ) ).to.equal( newChild );
  113. expect( el.childCount ).to.equal( 4 );
  114. } );
  115. it( 'should insert element to DocumentFragment element', () => {
  116. const newChild = new Element( 'p' );
  117. const inserted = writer.insertChild( 4, newChild, view );
  118. expect( inserted ).to.equal( 1 );
  119. expect( newChild.parent ).to.equal( view );
  120. expect( view.getChild( 4 ) ).to.equal( newChild );
  121. expect( view.childCount ).to.equal( 5 );
  122. } );
  123. } );
  124. describe( 'removeChildren', () => {
  125. it( 'should remove child from the beginning of the paragraph', () => {
  126. const el = view.getChild( 1 );
  127. const toRemove = el.getChild( 0 );
  128. const removed = writer.removeChildren( 0, 1, el );
  129. expect( removed.length ).to.equal( 1 );
  130. expect( removed[ 0 ] ).to.equal( toRemove );
  131. expect( el.childCount ).to.equal( 3 );
  132. } );
  133. it( 'should remove two last list items from the list element', () => {
  134. const el = view.getChild( 3 );
  135. const toRemove1 = el.getChild( 1 );
  136. const toRemove2 = el.getChild( 2 );
  137. const removed = writer.removeChildren( 1, 2, el );
  138. expect( removed.length ).to.equal( 2 );
  139. expect( removed[ 0 ] ).to.equal( toRemove1 );
  140. expect( removed[ 1 ] ).to.equal( toRemove2 );
  141. expect( el.childCount ).to.equal( 1 );
  142. } );
  143. it( 'should remove child from DocumentFragment element', () => {
  144. const toRemove = view.getChild( 2 );
  145. const removed = writer.removeChildren( 2, 1, view );
  146. expect( removed.length ).to.equal( 1 );
  147. expect( removed[ 0 ] ).to.equal( toRemove );
  148. expect( view.childCount ).to.equal( 3 );
  149. } );
  150. } );
  151. describe( 'remove', () => {
  152. it( 'should remove list item from the list element', () => {
  153. const toRemove = view.getChild( 3 ).getChild( 1 );
  154. const removed = writer.remove( toRemove );
  155. expect( removed.length ).to.equal( 1 );
  156. expect( removed[ 0 ] ).to.equal( toRemove );
  157. expect( view.getChild( 3 ).childCount ).to.equal( 2 );
  158. } );
  159. it( 'should have no effect on detached elements', () => {
  160. const newChild = new Element( 'h2' );
  161. const removed = writer.remove( newChild );
  162. expect( removed.length ).to.equal( 0 );
  163. expect( view.childCount ).to.equal( 4 );
  164. } );
  165. it( 'should remove direct root (DocumentFragment) child', () => {
  166. const toRemove = view.getChild( 3 );
  167. const removed = writer.remove( toRemove );
  168. expect( removed.length ).to.equal( 1 );
  169. expect( removed[ 0 ] ).to.equal( toRemove );
  170. expect( view.childCount ).to.equal( 3 );
  171. } );
  172. } );
  173. describe( 'replace', () => {
  174. it( 'should replace single element', () => {
  175. const el = view.getChild( 0 ).getChild( 1 );
  176. const newChild = new Element( 'span' );
  177. const replacement = writer.replace( el, newChild );
  178. expect( replacement ).to.true;
  179. expect( view.getChild( 0 ).getChild( 1 ) ).to.equal( newChild );
  180. expect( view.getChild( 0 ).childCount ).to.equal( 2 );
  181. } );
  182. it( 'should replace element with children', () => {
  183. const el = view.getChild( 3 );
  184. const newChild = new Element( 'ol' );
  185. const replacement = writer.replace( el, newChild );
  186. expect( replacement ).to.true;
  187. expect( view.getChild( 3 ) ).to.equal( newChild );
  188. expect( view.childCount ).to.equal( 4 );
  189. } );
  190. it( 'should have no effect on detached elements', () => {
  191. const oldChild = new Element( 'h2' );
  192. const newChild = new Element( 'h2' );
  193. const replacement = writer.replace( oldChild, newChild );
  194. expect( replacement ).to.false;
  195. expect( view.childCount ).to.equal( 4 );
  196. } );
  197. } );
  198. describe( 'rename', () => {
  199. it( 'should rename simple element', () => {
  200. const el = view.getChild( 0 ).getChild( 1 );
  201. const renamed = writer.rename( 'i', el );
  202. expect( renamed ).to.not.equal( el );
  203. expect( renamed ).to.equal( view.getChild( 0 ).getChild( 1 ) );
  204. expect( renamed.name ).to.equal( 'i' );
  205. expect( view.getChild( 0 ).childCount ).to.equal( 2 );
  206. } );
  207. it( 'should rename direct root (DocumentFragment) child element', () => {
  208. const el = view.getChild( 1 );
  209. const renamed = writer.rename( 'h3', el );
  210. expect( renamed ).to.not.equal( el );
  211. expect( renamed ).to.equal( view.getChild( 1 ) );
  212. expect( renamed.name ).to.equal( 'h3' );
  213. expect( view.childCount ).to.equal( 4 );
  214. } );
  215. it( 'should have no effect on detached element', () => {
  216. const el = new Element( 'h2' );
  217. const renamed = writer.rename( 'h3', el );
  218. expect( renamed ).to.null;
  219. expect( view.childCount ).to.equal( 4 );
  220. } );
  221. } );
  222. describe( 'setAttribute', () => {
  223. it( 'should add new attribute', () => {
  224. const el = view.getChild( 0 );
  225. writer.setAttribute( 'testAttr', 'testVal', el );
  226. expect( el.getAttribute( 'testAttr' ) ).to.equal( 'testVal' );
  227. } );
  228. it( 'should update existing attribute', () => {
  229. const el = view.getChild( 1 );
  230. writer.setAttribute( 'data-attr', 'foo', el );
  231. expect( el.getAttribute( 'data-attr' ) ).to.equal( 'foo' );
  232. } );
  233. } );
  234. describe( 'removeAttribute', () => {
  235. it( 'should remove existing attribute', () => {
  236. const el = view.getChild( 1 );
  237. writer.removeAttribute( 'data-attr', el );
  238. expect( el.hasAttribute( 'data-attr' ) ).to.false;
  239. } );
  240. it( 'should have no effect if attribute does not exists', () => {
  241. const el = view.getChild( 0 );
  242. writer.removeAttribute( 'non-existent', el );
  243. expect( el.hasAttribute( 'non-existent' ) ).to.false;
  244. } );
  245. } );
  246. describe( 'addClass', () => {
  247. it( 'should add new classes if no classes', () => {
  248. const el = view.getChild( 2 );
  249. writer.addClass( [ 'foo', 'bar' ], el );
  250. expect( el.hasClass( 'foo' ) ).to.true;
  251. expect( el.hasClass( 'bar' ) ).to.true;
  252. expect( Array.from( el.getClassNames() ).length ).to.equal( 2 );
  253. } );
  254. it( 'should add new class to existing classes', () => {
  255. const el = view.getChild( 1 );
  256. writer.addClass( 'newClass', el );
  257. expect( el.hasClass( 'newClass' ) ).to.true;
  258. expect( Array.from( el.getClassNames() ).length ).to.equal( 3 );
  259. } );
  260. } );
  261. describe( 'removeClass', () => {
  262. it( 'should remove existing class', () => {
  263. const el = view.getChild( 3 ).getChild( 0 );
  264. writer.removeClass( 'single', el );
  265. expect( el.hasClass( 'single' ) ).to.false;
  266. expect( Array.from( el.getClassNames() ).length ).to.equal( 0 );
  267. } );
  268. it( 'should remove existing class from many classes', () => {
  269. const el = view.getChild( 1 );
  270. writer.removeClass( 'foo1', el );
  271. expect( el.hasClass( 'foo1' ) ).to.false;
  272. expect( el.hasClass( 'bar2' ) ).to.true;
  273. expect( Array.from( el.getClassNames() ).length ).to.equal( 1 );
  274. } );
  275. it( 'should have no effect if there are no classes', () => {
  276. const el = view.getChild( 0 );
  277. writer.removeClass( 'non-existent', el );
  278. expect( el.hasClass( 'non-existent' ) ).to.false;
  279. expect( Array.from( el.getClassNames() ).length ).to.equal( 0 );
  280. } );
  281. } );
  282. describe( 'setStyle', () => {
  283. it( 'should add new style', () => {
  284. const el = view.getChild( 2 );
  285. writer.setStyle( {
  286. color: 'red',
  287. position: 'fixed'
  288. }, el );
  289. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  290. expect( el.getStyle( 'position' ) ).to.equal( 'fixed' );
  291. expect( Array.from( el.getStyleNames() ).length ).to.equal( 2 );
  292. } );
  293. it( 'should update existing styles', () => {
  294. const el = view.getChild( 1 );
  295. writer.setStyle( 'text-align', 'center', el );
  296. expect( el.getStyle( 'text-align' ) ).to.equal( 'center' );
  297. expect( Array.from( el.getStyleNames() ).length ).to.equal( 1 );
  298. } );
  299. } );
  300. describe( 'removeStyle', () => {
  301. it( 'should remove existing style', () => {
  302. const el = view.getChild( 0 );
  303. writer.removeStyle( [ 'color', 'position' ], el );
  304. expect( el.hasStyle( 'color' ) ).to.false;
  305. expect( el.hasStyle( 'position' ) ).to.false;
  306. expect( Array.from( el.getStyleNames() ).length ).to.equal( 0 );
  307. } );
  308. it( 'should remove value from existing styles', () => {
  309. const el = view.getChild( 0 );
  310. writer.removeStyle( 'position', el );
  311. expect( el.hasStyle( 'color' ) ).to.true;
  312. expect( el.hasStyle( 'position' ) ).to.false;
  313. expect( Array.from( el.getStyleNames() ).length ).to.equal( 1 );
  314. } );
  315. it( 'should have no effect if styles does not exists', () => {
  316. const el = view.getChild( 2 );
  317. writer.removeStyle( [ 'color', 'position' ], el );
  318. expect( el.hasStyle( 'color' ) ).to.false;
  319. expect( el.hasStyle( 'position' ) ).to.false;
  320. expect( Array.from( el.getStyleNames() ).length ).to.equal( 0 );
  321. } );
  322. } );
  323. describe( 'setCustomProperty', () => {
  324. it( 'should add or update custom property', () => {
  325. const el = new Element( 'span' );
  326. writer.setCustomProperty( 'prop1', 'foo', el );
  327. writer.setCustomProperty( 'prop2', 'bar', el );
  328. expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
  329. expect( el.getCustomProperty( 'prop2' ) ).to.equal( 'bar' );
  330. expect( Array.from( el.getCustomProperties() ).length ).to.equal( 2 );
  331. const objectProperty = { foo: 'bar' };
  332. writer.setCustomProperty( 'prop2', objectProperty, el );
  333. expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
  334. expect( el.getCustomProperty( 'prop2' ) ).to.equal( objectProperty );
  335. expect( Array.from( el.getCustomProperties() ).length ).to.equal( 2 );
  336. } );
  337. } );
  338. describe( 'removeCustomProperty', () => {
  339. it( 'should remove existing custom property', () => {
  340. const el = new Element( 'p' );
  341. writer.setCustomProperty( 'prop1', 'foo', el );
  342. expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
  343. expect( Array.from( el.getCustomProperties() ).length ).to.equal( 1 );
  344. writer.removeCustomProperty( 'prop1', el );
  345. expect( el.getCustomProperty( 'prop1' ) ).to.undefined;
  346. expect( Array.from( el.getCustomProperties() ).length ).to.equal( 0 );
  347. } );
  348. it( 'should have no effect if custom property does not exists', () => {
  349. const el = new Element( 'h1' );
  350. writer.removeCustomProperty( 'prop1', el );
  351. expect( el.getCustomProperty( 'prop1' ) ).to.undefined;
  352. expect( Array.from( el.getCustomProperties() ).length ).to.equal( 0 );
  353. } );
  354. } );
  355. } );