8
0

xmldataprocessor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals window */
  6. import XmlDataProcessor from '../../src/dataprocessor/xmldataprocessor';
  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( 'XmlDataProcessor', () => {
  11. let dataProcessor;
  12. beforeEach( () => {
  13. dataProcessor = new XmlDataProcessor();
  14. } );
  15. describe( 'toView', () => {
  16. it( 'should return empty DocumentFragment when empty string is passed', () => {
  17. const fragment = dataProcessor.toView( '' );
  18. expect( fragment ).to.be.an.instanceOf( ViewDocumentFragment );
  19. expect( fragment.childCount ).to.equal( 0 );
  20. } );
  21. it( 'should convert XML to DocumentFragment with single text node', () => {
  22. const fragment = dataProcessor.toView( 'foo bar' );
  23. expect( stringify( fragment ) ).to.equal( 'foo bar' );
  24. } );
  25. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  26. const fragment = dataProcessor.toView( '<p>foo</p><p>bar</p>' );
  27. expect( stringify( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  28. } );
  29. it( 'should not add any additional nodes', () => {
  30. const fragment = dataProcessor.toView( 'foo <b>bar</b> text' );
  31. expect( stringify( fragment ) ).to.equal( 'foo <b>bar</b> text' );
  32. } );
  33. it( 'should allow to use registered namespaces', () => {
  34. dataProcessor = new XmlDataProcessor( {
  35. namespaces: [ 'foo', 'bar' ]
  36. } );
  37. const fragment = dataProcessor.toView( '<foo:a><bar:b></bar:b></foo:a><bar:b><foo:a></foo:a></bar:b>' );
  38. expect( stringify( fragment ) ).to.equal( '<foo:a><bar:b></bar:b></foo:a><bar:b><foo:a></foo:a></bar:b>' );
  39. } );
  40. it( 'should throw an error when use not registered namespaces', () => {
  41. expect( () => {
  42. dataProcessor.toView( '<foo:a></foo:a>' );
  43. } ).to.throw( Error, /Parse error/ );
  44. } );
  45. it( 'should thrown an error when markup is invalid', () => {
  46. expect( () => {
  47. dataProcessor.toView( '<b>missing closing tag' );
  48. } ).to.throw( Error, /Parse error/ );
  49. } );
  50. // Test against XSS attacks.
  51. for ( const name in xssTemplates ) {
  52. const input = xssTemplates[ name ].replace( /%xss%/g, 'testXss()' );
  53. it( 'should prevent XSS attacks: ' + name, done => {
  54. window.testXss = sinon.spy();
  55. dataProcessor.toView( input );
  56. window.setTimeout( () => {
  57. sinon.assert.notCalled( window.testXss );
  58. done();
  59. }, 10 );
  60. } );
  61. }
  62. } );
  63. describe( 'toData', () => {
  64. it( 'should return empty string when empty DocumentFragment is passed', () => {
  65. const fragment = new ViewDocumentFragment();
  66. expect( dataProcessor.toData( fragment ) ).to.equal( '' );
  67. } );
  68. it( 'should return text if document fragment with single text node is passed', () => {
  69. const fragment = new ViewDocumentFragment();
  70. fragment._appendChild( parse( 'foo bar' ) );
  71. expect( dataProcessor.toData( fragment ) ).to.equal( 'foo bar' );
  72. } );
  73. it( 'should convert HTML to DocumentFragment with multiple child nodes', () => {
  74. const fragment = parse( '<p>foo</p><p>bar</p>' );
  75. expect( dataProcessor.toData( fragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  76. } );
  77. } );
  78. } );