-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52bb058
commit 50c0e90
Showing
3 changed files
with
65 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# escape-html-in-json | ||
Escape HTML entities in JSON.stringify | ||
|
||
Escape HTML entities in `JSON.stringify`, using a replacer method. | ||
|
||
```javascript | ||
> var escape_html_entities = require('escape-html-in-json') | ||
> var object = {name: "<script>alert('hacked');</script>"} | ||
> JSON.stringify(object) | ||
'{"name":"<script>alert(\'hacked\');</script>"}' | ||
> JSON.stringify(object, escape_html_entities) | ||
'{"name":"<script>alert('hacked');</script>"}' | ||
``` |
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,26 @@ | ||
// Require the `escape_html` helper. | ||
var escape_html = require('escape-html'); | ||
|
||
/** | ||
* A `JSON.stringify()` replacer method that escapes HTML entities. | ||
* | ||
* See: http://mzl.la/1HTcvVM | ||
* | ||
* Example Usage: | ||
* | ||
* > var escape_html_entities = require('escape-html-in-json') | ||
* undefined | ||
* > var object = {name: "<script>alert('hacked');</script>"} | ||
* undefined | ||
* > JSON.stringify(object) | ||
* '{"name":"<script>alert(\'hacked\');</script>"}' | ||
* > JSON.stringify(object, escape_html_entities) | ||
* '{"name":"<script>alert('hacked');</script>"}' | ||
* | ||
* @param {String} key | ||
* @param {Mixed} value | ||
* @param {String} | ||
*/ | ||
module.exports = function(key, value) { | ||
return typeof value === 'string' ? escape_html(value) : value; | ||
} |
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,28 @@ | ||
{ | ||
"name": "escape-html-in-json", | ||
"version": "1.0.0", | ||
"description": "Escape HTML entities in JSON.stringify()", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Radagaisus/escape-html-in-json.git" | ||
}, | ||
"keywords": [ | ||
"escape", | ||
"html", | ||
"entities", | ||
"json" | ||
], | ||
"author": "Almog Melamed <[email protected]> (http://github.com/radagaisus/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Radagaisus/escape-html-in-json/issues" | ||
}, | ||
"homepage": "https://github.com/Radagaisus/escape-html-in-json", | ||
"dependencies": { | ||
"escape-html": "1.0.1" | ||
} | ||
} |