xmldataprocessor.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. import XmlDataProcessor from '/ckeditor5/engine/dataprocessor/xmldataprocessor.js';
  7. import xssTemplates from '/tests/engine/dataprocessor/_utils/xsstemplates.js';
  8. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  9. import { stringify, parse } from '/tests/engine/_utils/view.js';
  10. describe( 'XmlDataProcessor', () => {
  11. const dataProcessor = new XmlDataProcessor();
  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 XML 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 not add any additional nodes', () => {
  27. const fragment = dataProcessor.toView( 'foo <b>bar</b> text' );
  28. expect( stringify( fragment ) ).to.equal( 'foo <b>bar</b> text' );
  29. } );
  30. it( 'should thrown an error when markup is invalid', () => {
  31. expect( () => {
  32. dataProcessor.toView( '<b>missing closing tag' );
  33. } ).to.throw( Error, /Parse error/ );
  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. } );