htmldataprocessor.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals setTimeout, window */
  6. /* bender-tags: browser-only */
  7. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  8. import xssTemplates from '/tests/engine/dataprocessor/_utils/xsstemplates.js';
  9. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  10. import { stringify, parse } from '/tests/engine/_utils/view.js';
  11. describe( 'HtmlDataProcessor', () => {
  12. const dataProcessor = new HtmlDataProcessor();
  13. describe( 'toView', () => {
  14. it( 'should return empty DocumentFragment when empty string is passed', () => {
  15. const fragment = dataProcessor.toView( '' );
  16. expect( fragment ).to.be.an.instanceOf( ViewDocumentFragment );
  17. expect( fragment.childCount ).to.equal( 0 );
  18. } );
  19. it( 'should convert HTML to DocumentFragment with single text node', () => {
  20. const fragment = dataProcessor.toView( 'foo bar' );
  21. expect( stringify( fragment ) ).to.equal( 'foo bar' );
  22. } );
  23. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  24. const fragment = dataProcessor.toView( '<p>foo</p><p>bar</p>' );
  25. expect( stringify( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  26. } );
  27. it( 'should return only elements inside body tag', () => {
  28. const fragment = dataProcessor.toView( '<html><head></head><body><p>foo</p></body></html>' );
  29. expect( stringify( fragment ) ).to.equal( '<p>foo</p>' );
  30. } );
  31. it( 'should not add any additional nodes', () => {
  32. const fragment = dataProcessor.toView( 'foo <b>bar</b> text' );
  33. expect( stringify( fragment ) ).to.equal( 'foo <b>bar</b> text' );
  34. } );
  35. // Test against XSS attacks.
  36. for ( let name in xssTemplates ) {
  37. const input = xssTemplates[ name ].replace( /%xss%/g, 'testXss()' );
  38. it( 'should prevent XSS attacks: ' + name, ( done ) => {
  39. window.testXss = sinon.spy();
  40. dataProcessor.toView( input );
  41. setTimeout( () => {
  42. sinon.assert.notCalled( window.testXss );
  43. done();
  44. }, 10 );
  45. } );
  46. }
  47. } );
  48. describe( 'toData', () => {
  49. it( 'should return empty string when empty DocumentFragment is passed', () => {
  50. const fragment = new ViewDocumentFragment();
  51. expect( dataProcessor.toData( fragment ) ).to.equal( '' );
  52. } );
  53. it( 'should return text if document fragment with single text node is passed', () => {
  54. const fragment = new ViewDocumentFragment();
  55. fragment.appendChildren( parse( 'foo bar' ) );
  56. expect( dataProcessor.toData( fragment ) ).to.equal( 'foo bar' );
  57. } );
  58. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  59. const fragment = parse( '<p>foo</p><p>bar</p>' );
  60. expect( dataProcessor.toData( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  61. } );
  62. } );
  63. } );