Skip to content

Commit

Permalink
HTML-escape closing '>' as well as opening -- for a few edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Apr 27, 2020
1 parent ac67270 commit 99aedf2
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions dist/browser/eta.dev.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/eta.dev.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser/eta.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/eta.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/eta.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/eta.cjs.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/eta.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/eta.es.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EtaConfig } from './config'
interface EscapeMap {
'&': '&'
'<': '&lt;'
'>': '&gt;'
'"': '&quot;'
"'": '&#39;'
[index: string]: string
Expand Down Expand Up @@ -98,6 +99,7 @@ function trimWS (
var escMap: EscapeMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}
Expand All @@ -109,8 +111,8 @@ function replaceChar (s: string): string {
function XMLEscape (str: any) {
// To deal with XSS. Based on Escape implementations of Mustache.JS and Marko, then customized.
var newStr = String(str)
if (/[&<"']/.test(newStr)) {
return newStr.replace(/[&<"']/g, replaceChar)
if (/[&<>"']/.test(newStr)) {
return newStr.replace(/[&<>"']/g, replaceChar)
} else {
return newStr
}
Expand Down
2 changes: 1 addition & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ describe('Whitespace trim', () => {

describe('HTML Escape', () => {
it('properly escapes HTML characters', () => {
expect(XMLEscape('<p>HTML</p>')).toBe('&lt;p>HTML&lt;/p>')
expect(XMLEscape('<p>HTML</p>')).toBe('&lt;p&gt;HTML&lt;/p&gt;')
})
})

0 comments on commit 99aedf2

Please sign in to comment.