Skip to content

Commit 59df9af

Browse files
author
Brandon Marick
committed
Adding Unencode() HTML Function
1 parent 2e3c7d4 commit 59df9af

File tree

9 files changed

+63
-4
lines changed

9 files changed

+63
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#### HEAD
22

3+
- Added a `unescape()` HTML function
4+
([#509](https://github.com/chriso/validator.js/pull/509))
35
- Added a Malaysian locale to `isMobilePhone()`
46
([#507](https://github.com/chriso/validator.js/pull/507))
57
- Added Polish locales to `isAlpha()` and `isAlphanumeric()`

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Passing anything other than a string is an error.
101101

102102
- **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, '\\[\\]')`.
103103
- **escape(input)** - replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities.
104+
- **unescape(input)** - replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`.
104105
- **ltrim(input [, chars])** - trim characters from the left-side of the input.
105106
- **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. `some.one+extension@gmail.com` becomes `someone@gmail.com`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
106107
- **rtrim(input [, chars])** - trim characters from the right-side of the input.

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ var _escape = require('./lib/escape');
208208

209209
var _escape2 = _interopRequireDefault(_escape);
210210

211+
var _unescape = require('./lib/unescape');
212+
213+
var _unescape2 = _interopRequireDefault(_unescape);
214+
211215
var _stripLow = require('./lib/stripLow');
212216

213217
var _stripLow2 = _interopRequireDefault(_stripLow);
@@ -262,7 +266,7 @@ var validator = {
262266
isISO8601: _isISO2.default,
263267
isBase64: _isBase2.default,
264268
ltrim: _ltrim2.default, rtrim: _rtrim2.default, trim: _trim2.default,
265-
escape: _escape2.default, stripLow: _stripLow2.default,
269+
escape: _escape2.default, unescape: _unescape2.default, stripLow: _stripLow2.default,
266270
whitelist: _whitelist2.default, blacklist: _blacklist2.default,
267271
isWhitelisted: _isWhitelisted2.default,
268272
normalizeEmail: _normalizeEmail2.default,

lib/unescape.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = unescape;
7+
8+
var _assertString = require('./util/assertString');
9+
10+
var _assertString2 = _interopRequireDefault(_assertString);
11+
12+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13+
14+
function unescape(str) {
15+
(0, _assertString2.default)(str);
16+
return str.replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&#x2F;/g, '\/').replace(/&#96;/g, '\`');
17+
}
18+
module.exports = exports['default'];

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import ltrim from './lib/ltrim';
6969
import rtrim from './lib/rtrim';
7070
import trim from './lib/trim';
7171
import escape from './lib/escape';
72+
import unescape from './lib/unescape';
7273
import stripLow from './lib/stripLow';
7374
import whitelist from './lib/whitelist';
7475
import blacklist from './lib/blacklist';
@@ -104,7 +105,7 @@ const validator = {
104105
isISO8601,
105106
isBase64,
106107
ltrim, rtrim, trim,
107-
escape, stripLow,
108+
escape, unescape, stripLow,
108109
whitelist, blacklist,
109110
isWhitelisted,
110111
normalizeEmail,

src/lib/unescape.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import assertString from './util/assertString';
2+
3+
export default function unescape(str) {
4+
assertString(str);
5+
return (str.replace(/&amp;/g, '&')
6+
.replace(/&quot;/g, '"')
7+
.replace(/&#x27;/g, "'")
8+
.replace(/&lt;/g, '<')
9+
.replace(/&gt;/g, '>')
10+
.replace(/&#x2F;/g, '\/')
11+
.replace(/&#96;/g, '\`'));
12+
}

test/sanitizers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ describe('Sanitizers', function () {
134134
});
135135
});
136136

137+
it('should unescape HTML', function () {
138+
test({
139+
sanitizer: 'unescape',
140+
expect: {
141+
'&lt;script&gt; alert(&quot;xss&amp;fun&quot;); &lt;&#x2F;script&gt;':
142+
'<script> alert("xss&fun"); </script>',
143+
144+
'&lt;script&gt; alert(&#x27;xss&amp;fun&#x27;); &lt;&#x2F;script&gt;':
145+
"<script> alert('xss&fun'); </script>",
146+
147+
'Backtick: &#96;':
148+
'Backtick: `',
149+
},
150+
});
151+
});
152+
137153
it('should remove control characters (<32 and 127)', function () {
138154
// Check basic functionality
139155
test({

validator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,11 @@
981981
return str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\//g, '&#x2F;').replace(/\`/g, '&#96;');
982982
}
983983

984+
function unescape(str) {
985+
assertString(str);
986+
return str.replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&#x2F;/g, '\/').replace(/&#96;/g, '\`');
987+
}
988+
984989
function blacklist(str, chars) {
985990
assertString(str);
986991
return str.replace(new RegExp('[' + chars + ']+', 'g'), '');
@@ -1066,7 +1071,7 @@
10661071
isISO8601: isISO8601,
10671072
isBase64: isBase64,
10681073
ltrim: ltrim, rtrim: rtrim, trim: trim,
1069-
escape: escape, stripLow: stripLow,
1074+
escape: escape, unescape: unescape, stripLow: stripLow,
10701075
whitelist: whitelist, blacklist: blacklist,
10711076
isWhitelisted: isWhitelisted,
10721077
normalizeEmail: normalizeEmail,

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)