8
0

upcastwriter.js 16 KB

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