htmldataprocessor.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals setTimeout, window */
  6. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  7. import xssTemplates from '../../tests/dataprocessor/_utils/xsstemplates';
  8. import ViewDocumentFragment from '../../src/view/documentfragment';
  9. import { stringify, parse } from '../../src/dev-utils/view';
  10. describe( 'HtmlDataProcessor', () => {
  11. const dataProcessor = new HtmlDataProcessor();
  12. describe( 'toView()', () => {
  13. it( 'should return empty DocumentFragment when empty string is passed', () => {
  14. const fragment = dataProcessor.toView( '' );
  15. expect( fragment ).to.be.an.instanceOf( ViewDocumentFragment );
  16. expect( fragment.childCount ).to.equal( 0 );
  17. } );
  18. it( 'should convert HTML to DocumentFragment with single text node', () => {
  19. const fragment = dataProcessor.toView( 'foo bar' );
  20. expect( stringify( fragment ) ).to.equal( 'foo bar' );
  21. } );
  22. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  23. const fragment = dataProcessor.toView( '<p>foo</p><p>bar</p>' );
  24. expect( stringify( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  25. } );
  26. it( 'should return only elements inside body tag', () => {
  27. const fragment = dataProcessor.toView( '<html><head></head><body><p>foo</p></body></html>' );
  28. expect( stringify( fragment ) ).to.equal( '<p>foo</p>' );
  29. } );
  30. it( 'should not add any additional nodes', () => {
  31. const fragment = dataProcessor.toView( 'foo <b>bar</b> text' );
  32. expect( stringify( fragment ) ).to.equal( 'foo <b>bar</b> text' );
  33. } );
  34. // Test against XSS attacks.
  35. for ( const name in xssTemplates ) {
  36. const input = xssTemplates[ name ].replace( /%xss%/g, 'testXss()' );
  37. it( 'should prevent XSS attacks: ' + name, done => {
  38. window.testXss = sinon.spy();
  39. dataProcessor.toView( input );
  40. setTimeout( () => {
  41. sinon.assert.notCalled( window.testXss );
  42. done();
  43. }, 10 );
  44. } );
  45. }
  46. // Uncomment this test after fixing #404.
  47. // describe( 'https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731 + #404', () => {
  48. // it( 'does not lose whitespaces in Chrome\'s paste-like content', () => {
  49. // const fragment = dataProcessor.toView(
  50. // '<meta charset=\'utf-8\'>' +
  51. // '<span>This is the<span>\u00a0</span></span>' +
  52. // '<a href="url">third developer preview</a>' +
  53. // '<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
  54. // '<strong>CKEditor\u00a05</strong>' +
  55. // '<span>.</span>'
  56. // );
  57. // expect( stringify( fragment ) ).to.equal(
  58. // '<span>This is the<span>\u00a0</span></span>' +
  59. // '<a href="url">third developer preview</a>' +
  60. // '<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
  61. // '<strong>CKEditor\u00a05</strong>' +
  62. // '<span>.</span>'
  63. // );
  64. // // Just to be sure... stringify() uses conversion and the browser extensively,
  65. // // so it's not entirely safe.
  66. // expect( fragment.getChild( 0 ).getChild( 1 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  67. // expect( fragment.getChild( 2 ).getChild( 0 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  68. // expect( fragment.getChild( 2 ).getChild( 2 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  69. // } );
  70. // } );
  71. } );
  72. describe( 'toData()', () => {
  73. it( 'should return empty string when empty DocumentFragment is passed', () => {
  74. const fragment = new ViewDocumentFragment();
  75. expect( dataProcessor.toData( fragment ) ).to.equal( '' );
  76. } );
  77. it( 'should return text if document fragment with single text node is passed', () => {
  78. const fragment = new ViewDocumentFragment();
  79. fragment._appendChild( parse( 'foo bar' ) );
  80. expect( dataProcessor.toData( fragment ) ).to.equal( 'foo bar' );
  81. } );
  82. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  83. const fragment = parse( '<p>foo</p><p>bar</p>' );
  84. expect( dataProcessor.toData( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  85. } );
  86. } );
  87. } );