htmldataprocessor.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 ( let 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. } );
  47. describe( 'toData', () => {
  48. it( 'should return empty string when empty DocumentFragment is passed', () => {
  49. const fragment = new ViewDocumentFragment();
  50. expect( dataProcessor.toData( fragment ) ).to.equal( '' );
  51. } );
  52. it( 'should return text if document fragment with single text node is passed', () => {
  53. const fragment = new ViewDocumentFragment();
  54. fragment.appendChildren( parse( 'foo bar' ) );
  55. expect( dataProcessor.toData( fragment ) ).to.equal( 'foo bar' );
  56. } );
  57. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  58. const fragment = parse( '<p>foo</p><p>bar</p>' );
  59. expect( dataProcessor.toData( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  60. } );
  61. } );
  62. } );