-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added escapeHtml method to string package
refs https://github.com/TryGhost/Team/issues/1871 This method is required in a couple of places where we want to escape HTML manually, such as the email template.
- Loading branch information
1 parent
6082ff4
commit 4a605f5
Showing
5 changed files
with
357 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* escapeHTML | ||
* | ||
* Escape a string to be used as text or an attribute in HTML | ||
* | ||
* @param {string} string - the string we want to escape | ||
* @returns {string} The escaped string | ||
*/ | ||
module.exports = function (string) { | ||
const htmlChars = { | ||
'&': '&', | ||
'"': '"', | ||
'\'': ''', | ||
'<': '<', | ||
'>': '>' | ||
}; | ||
return string.replace(/[&"'<>]/g, c => htmlChars[c]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
module.exports.stripInvisibleChars = require('./stripInvisibleChars'); | ||
module.exports.slugify = require('./slugify'); | ||
module.exports.escapeHtml = require('./escapeHtml'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require('./utils'); | ||
|
||
const escapeHtml = require('../lib').escapeHtml; | ||
const jsdom = require('jsdom'); | ||
|
||
describe('Escape HTML', function () { | ||
it('escapes all characters', function () { | ||
const result = escapeHtml(`<tag>"Black" & 'White'</tag>`); | ||
result.should.equal(`<tag>"Black" & 'White'</tag>`); | ||
}); | ||
|
||
it('produces valid HTML attributes', function () { | ||
const myAttribute = `><tag>"Black" & 'White'</tag>`; | ||
const htmls = [ | ||
`<input value="${escapeHtml(myAttribute)}" type="text" />`, | ||
`<input value='${escapeHtml(myAttribute)}' type='text' />` | ||
]; | ||
|
||
for (const html of htmls) { | ||
const dom = new jsdom.JSDOM(html); | ||
should(dom.window.document.querySelector('input').getAttribute('value')).equal(myAttribute); | ||
should(dom.window.document.querySelector('input').value).equal(myAttribute); | ||
} | ||
}); | ||
|
||
it('produces valid HTML text', function () { | ||
const texts = [ | ||
`<tag>"Black" & 'White'</tag>`, | ||
`<![CDATA[This is no data]]>`, | ||
`<!--This is no comment-->`, | ||
`</p>This doesn't end<p>` | ||
]; | ||
const htmls = texts.map(text => [ | ||
`<p>${escapeHtml(text)}</p>` | ||
]); | ||
|
||
for (const [index, html] of htmls.entries()) { | ||
const text = texts[index]; | ||
const dom = new jsdom.JSDOM(html); | ||
should(dom.window.document.querySelector('p').textContent).equal(text); | ||
should(dom.window.document.querySelector('p').innerHTML).not.equal(text); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.