From 05b217916c5c5182d51a732183ed385352d11bf7 Mon Sep 17 00:00:00 2001 From: Johannes Burghardt Date: Fri, 11 Oct 2019 10:19:50 +0200 Subject: [PATCH] [Fix] escape closing `', '>'], + [' { it('escapes', () => { - const html = serialize('foo', '', { foo: '', bar: '>' }); + const html = serialize('foo', '', { foo: '', bar: '>', baz: ' ({})) - .describe('with fromScript', () => { - it('loads the escaped content correctly', () => { - const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&' }); - const $ = cheerio.load(html); + .withGlobal('document', () => ({})) + .describe('with fromScript', () => { + it('loads the escaped content correctly', () => { + const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&', foobar: ' ({ innerHTML: $($('script')[0]).html() }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const res = fromScript({ - a: 'b', - }); - - assert.isObject(res); + const res = fromScript({ + a: 'b', + }); - assert.equal(res.foo, ''); - assert.equal(res.bar, '>'); - assert.equal(res.baz, '&'); - }); + assert.isObject(res); - it('escapes multiple times the same, with interleaved decoding', () => { - const makeHTML = () => toScript({ attr: 'key' }, { - props: 'yay', - needsEncoding: '" > ', // "needsEncoding" is necessary + assert.equal(res.foo, ''); + assert.equal(res.bar, '>'); + assert.equal(res.baz, '&'); + assert.equal(res.foobar, ' { + const makeHTML = () => toScript({ attr: 'key' }, { + props: 'yay', + needsEncoding: '" > ', // "needsEncoding" is necessary + }); + const script1 = makeHTML(); + const script2 = makeHTML(); + assert.equal(script1, script2, 'two successive toScripts result in identical HTML'); - global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); + const $ = cheerio.load(script1); - const res = fromScript({ attr: 'key' }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const script3 = makeHTML(); - assert.equal( - script1, - script3, - 'third toScript after a fromScript call results in the same HTML', - ); + const res = fromScript({ attr: 'key' }); - assert.isObject(res); + const script3 = makeHTML(); + assert.equal( + script1, + script3, + 'third toScript after a fromScript call results in the same HTML', + ); - assert.equal(res.props, 'yay'); + assert.isObject(res); + + assert.equal(res.props, 'yay'); + }); }); - }); it('escapes quotes and fixes data attributes', () => { const markup = toScript({