htmldataprocessor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. describe( 'https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731 + #404', () => {
  47. it( 'does not lose whitespaces in Chrome\'s paste-like content', () => {
  48. const fragment = dataProcessor.toView(
  49. '<meta charset=\'utf-8\'>' +
  50. '<span>This is the<span>\u00a0</span></span>' +
  51. '<a href="url">third developer preview</a>' +
  52. '<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
  53. '<strong>CKEditor\u00a05</strong>' +
  54. '<span>.</span>'
  55. );
  56. expect( stringify( fragment ) ).to.equal(
  57. '<span>This is the<span>\u00a0</span></span>' +
  58. '<a href="url">third developer preview</a>' +
  59. '<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
  60. '<strong>CKEditor\u00a05</strong>' +
  61. '<span>.</span>'
  62. );
  63. // Just to be sure... stringify() uses conversion and the browser extensively,
  64. // so it's not entirely safe.
  65. expect( fragment.getChild( 0 ).getChild( 1 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  66. expect( fragment.getChild( 2 ).getChild( 0 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  67. expect( fragment.getChild( 2 ).getChild( 2 ).getChild( 0 ).data ).to.equal( '\u00a0' );
  68. } );
  69. } );
  70. } );
  71. describe( 'toData()', () => {
  72. it( 'should return empty string when empty DocumentFragment is passed', () => {
  73. const fragment = new ViewDocumentFragment();
  74. expect( dataProcessor.toData( fragment ) ).to.equal( '' );
  75. } );
  76. it( 'should return text if document fragment with single text node is passed', () => {
  77. const fragment = new ViewDocumentFragment();
  78. fragment._appendChild( parse( 'foo bar' ) );
  79. expect( dataProcessor.toData( fragment ) ).to.equal( 'foo bar' );
  80. } );
  81. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  82. const fragment = parse( '<p>foo</p><p>bar</p>' );
  83. expect( dataProcessor.toData( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  84. } );
  85. } );
  86. } );