|
| 1 | +require('./utils'); |
| 2 | + |
| 3 | +const escapeHtml = require('../lib').escapeHtml; |
| 4 | +const jsdom = require('jsdom'); |
| 5 | + |
| 6 | +describe('Escape HTML', function () { |
| 7 | + it('escapes all characters', function () { |
| 8 | + const result = escapeHtml(`<tag>"Black" & 'White'</tag>`); |
| 9 | + result.should.equal(`<tag>"Black" & 'White'</tag>`); |
| 10 | + }); |
| 11 | + |
| 12 | + it('produces valid HTML attributes', function () { |
| 13 | + const myAttribute = `><tag>"Black" & 'White'</tag>`; |
| 14 | + const htmls = [ |
| 15 | + `<input value="${escapeHtml(myAttribute)}" type="text" />`, |
| 16 | + `<input value='${escapeHtml(myAttribute)}' type='text' />` |
| 17 | + ]; |
| 18 | + |
| 19 | + for (const html of htmls) { |
| 20 | + const dom = new jsdom.JSDOM(html); |
| 21 | + should(dom.window.document.querySelector('input').getAttribute('value')).equal(myAttribute); |
| 22 | + should(dom.window.document.querySelector('input').value).equal(myAttribute); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + it('produces valid HTML text', function () { |
| 27 | + const texts = [ |
| 28 | + `<tag>"Black" & 'White'</tag>`, |
| 29 | + `<![CDATA[This is no data]]>`, |
| 30 | + `<!--This is no comment-->`, |
| 31 | + `</p>This doesn't end<p>` |
| 32 | + ]; |
| 33 | + const htmls = texts.map(text => [ |
| 34 | + `<p>${escapeHtml(text)}</p>` |
| 35 | + ]); |
| 36 | + |
| 37 | + for (const [index, html] of htmls.entries()) { |
| 38 | + const text = texts[index]; |
| 39 | + const dom = new jsdom.JSDOM(html); |
| 40 | + should(dom.window.document.querySelector('p').textContent).equal(text); |
| 41 | + should(dom.window.document.querySelector('p').innerHTML).not.equal(text); |
| 42 | + } |
| 43 | + }); |
| 44 | +}); |
0 commit comments