Socket
Socket
Sign inDemoInstall

ent

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ent - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

decode.js

69

index.js

@@ -1,67 +0,2 @@

var punycode = require('punycode');
var entities = require('./entities.json');
var revEntities = require('./reversed.json');
exports.encode = function (str, opts) {
if (typeof str !== 'string') {
throw new TypeError('Expected a String');
}
var special = {
'"': true, "'": true,
'<': true, '>': true,
'&': true
};
if (!opts) opts = {};
var numeric = true;
if (opts.named) numeric = false;
if (opts.numeric !== undefined) numeric = opts.numeric;
return str.split('').map(function (c) {
var cc = c.charCodeAt(0);
var e = revEntities[cc];
if (e && (cc >= 127 || special[c]) && !numeric) {
return '&' + (e.match(/;$/) ? e : e + ';');
}
else if (cc < 32 || cc >= 127 || special[c]) {
return '&#' + cc + ';';
}
else if (/\s/.test(c)) {
return c;
}
else {
return c;
}
}).join('');
};
exports.decode = function (str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a String');
}
return str
.replace(/&#(\d+);?/g, function (_, code) {
return punycode.ucs2.encode([code]);
})
.replace(/&#[xX]([A-Fa-f0-9]+);?/g, function (_, hex) {
return punycode.ucs2.encode([parseInt(hex, 16)]);
})
.replace(/&([^;\W]+;?)/g, function (m, e) {
var ee = e.replace(/;$/, '');
var target = entities[e]
|| (e.match(/;$/) && entities[ee])
;
if (typeof target === 'number') {
return punycode.ucs2.encode([target]);
}
else if (typeof target === 'string') {
return target;
}
else {
return m;
}
})
;
};
exports.encode = require('./encode');
exports.decode = require('./decode');
{
"name" : "ent",
"description" : "Encode and decode HTML entities",
"version" : "2.0.0",
"version" : "2.1.0",
"repository" : "https://github.com/substack/node-ent.git",

@@ -6,0 +6,0 @@ "author" : "James Halliday <mail@substack.net> (http://substack.net)",

@@ -50,3 +50,3 @@ var test = require('tape');

t.equal(ent.decode(b), a);
t.equal(ent.encode(a + a), b + b);

@@ -59,6 +59,20 @@ t.equal(ent.decode(b + b), a + a);

var a = punycode.ucs2.encode([0x1d306]);
var b = '&#x1d306;';
var b = '&#119558;';
t.equal(ent.encode(a), b);
t.equal(ent.decode(b), a);
t.equal(ent.encode(a + a), b + b);
t.equal(ent.decode(b + b), a + a);
t.end();
});
test('nested escapes', function (t) {
var a = '&amp;';
var b = '&#38;amp;';
t.equal(ent.encode(a), b);
t.equal(ent.decode(b), a);
t.equal(ent.encode(a + a), b + b);
t.equal(ent.decode(b + b), a + a);
t.end();
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc