Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Radagaisus committed Apr 28, 2015
1 parent 52bb058 commit 50c0e90
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
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":"&lt;script&gt;alert(&#39;hacked&#39;);&lt;/script&gt;"}'
```
26 changes: 26 additions & 0 deletions index.js
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":"&lt;script&gt;alert(&#39;hacked&#39;);&lt;/script&gt;"}'
*
* @param {String} key
* @param {Mixed} value
* @param {String}
*/
module.exports = function(key, value) {
return typeof value === 'string' ? escape_html(value) : value;
}
28 changes: 28 additions & 0 deletions package.json
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"
}
}

0 comments on commit 50c0e90

Please sign in to comment.