8
0

rawwriter.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 RawWriter from '../../src/view/rawwriter';
  7. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  8. describe( 'RawWriter', () => {
  9. let writer, view, dataprocessor;
  10. before( () => {
  11. writer = new RawWriter();
  12. dataprocessor = new HtmlDataProcessor();
  13. } );
  14. beforeEach( () => {
  15. const html = '' +
  16. '<h1>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>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( el, newChild );
  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( el, [ newChild1, newChild2 ] );
  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( el, newChild );
  72. expect( appended ).to.equal( 1 );
  73. expect( newChild.parent ).to.equal( el );
  74. expect( el.childCount ).to.equal( 4 );
  75. } );
  76. } );
  77. } );