htmldataprocessor.js 3.2 KB

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