-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(util): add reusable escape util
- Loading branch information
1 parent
9668e84
commit 0e52034
Showing
3 changed files
with
38 additions
and
2 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
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,14 @@ | ||
export { | ||
default as escapeCSS | ||
} from 'css.escape'; | ||
|
||
var HTML_ESCAPE_MAP = { | ||
'<': '<', | ||
'>': '>' | ||
}; | ||
|
||
export function escapeHTML(str) { | ||
return str.replace(/[<>]/g, function(match) { | ||
return HTML_ESCAPE_MAP[match]; | ||
}); | ||
} |
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,20 @@ | ||
import { | ||
escapeCSS, | ||
escapeHTML | ||
} from 'lib/util/EscapeUtil'; | ||
|
||
|
||
describe('util/EscapeUtil', function() { | ||
|
||
it('escapeCSS', function() { | ||
expect(escapeCSS('..ab')).to.eql('\\.\\.ab'); | ||
}); | ||
|
||
|
||
it('escapeHTML', function() { | ||
var htmlStr = '<video src=1 onerror=alert(\'hueh\')>'; | ||
|
||
expect(escapeHTML(htmlStr)).to.eql('<video src=1 onerror=alert(\'hueh\')>'); | ||
}); | ||
|
||
}); |