upcastwriter.js 16 KB

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