Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Unencode() HTML Function #509

Merged
merged 1 commit into from Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#### HEAD

- Added a `unescape()` HTML function
([#509](https://github.com/chriso/validator.js/pull/509))
- Added a Malaysian locale to `isMobilePhone()`
([#507](https://github.com/chriso/validator.js/pull/507))
- Added Polish locales to `isAlpha()` and `isAlphanumeric()`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Passing anything other than a string is an error.

- **blacklist(input, chars)** - remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`.
- **escape(input)** - replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities.
- **unescape(input)** - replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`.
- **ltrim(input [, chars])** - trim characters from the left-side of the input.
- **normalizeEmail(email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true, remove_dots: true, remove_extension: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of extensions (e.g. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
- **rtrim(input [, chars])** - trim characters from the right-side of the input.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ var _escape = require('./lib/escape');

var _escape2 = _interopRequireDefault(_escape);

var _unescape = require('./lib/unescape');

var _unescape2 = _interopRequireDefault(_unescape);

var _stripLow = require('./lib/stripLow');

var _stripLow2 = _interopRequireDefault(_stripLow);
Expand Down Expand Up @@ -262,7 +266,7 @@ var validator = {
isISO8601: _isISO2.default,
isBase64: _isBase2.default,
ltrim: _ltrim2.default, rtrim: _rtrim2.default, trim: _trim2.default,
escape: _escape2.default, stripLow: _stripLow2.default,
escape: _escape2.default, unescape: _unescape2.default, stripLow: _stripLow2.default,
whitelist: _whitelist2.default, blacklist: _blacklist2.default,
isWhitelisted: _isWhitelisted2.default,
normalizeEmail: _normalizeEmail2.default,
Expand Down
18 changes: 18 additions & 0 deletions lib/unescape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = unescape;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function unescape(str) {
(0, _assertString2.default)(str);
return str.replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&#x2F;/g, '\/').replace(/&#96;/g, '\`');
}
module.exports = exports['default'];
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import ltrim from './lib/ltrim';
import rtrim from './lib/rtrim';
import trim from './lib/trim';
import escape from './lib/escape';
import unescape from './lib/unescape';
import stripLow from './lib/stripLow';
import whitelist from './lib/whitelist';
import blacklist from './lib/blacklist';
Expand Down Expand Up @@ -104,7 +105,7 @@ const validator = {
isISO8601,
isBase64,
ltrim, rtrim, trim,
escape, stripLow,
escape, unescape, stripLow,
whitelist, blacklist,
isWhitelisted,
normalizeEmail,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/unescape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import assertString from './util/assertString';

export default function unescape(str) {
assertString(str);
return (str.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&#x27;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&#x2F;/g, '\/')
.replace(/&#96;/g, '\`'));
}
16 changes: 16 additions & 0 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ describe('Sanitizers', function () {
});
});

it('should unescape HTML', function () {
test({
sanitizer: 'unescape',
expect: {
'&lt;script&gt; alert(&quot;xss&amp;fun&quot;); &lt;&#x2F;script&gt;':
'<script> alert("xss&fun"); </script>',

'&lt;script&gt; alert(&#x27;xss&amp;fun&#x27;); &lt;&#x2F;script&gt;':
"<script> alert('xss&fun'); </script>",

'Backtick: &#96;':
'Backtick: `',
},
});
});

it('should remove control characters (<32 and 127)', function () {
// Check basic functionality
test({
Expand Down
7 changes: 6 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,11 @@
return str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\//g, '&#x2F;').replace(/\`/g, '&#96;');
}

function unescape(str) {
assertString(str);
return str.replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&#x2F;/g, '\/').replace(/&#96;/g, '\`');
}

function blacklist(str, chars) {
assertString(str);
return str.replace(new RegExp('[' + chars + ']+', 'g'), '');
Expand Down Expand Up @@ -1066,7 +1071,7 @@
isISO8601: isISO8601,
isBase64: isBase64,
ltrim: ltrim, rtrim: rtrim, trim: trim,
escape: escape, stripLow: stripLow,
escape: escape, unescape: unescape, stripLow: stripLow,
whitelist: whitelist, blacklist: blacklist,
isWhitelisted: isWhitelisted,
normalizeEmail: normalizeEmail,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.