Comparing version 2.0.0 to 2.1.0
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 = '𝌆'; | ||
var b = '𝌆'; | ||
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 = '&'; | ||
var b = '&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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97027
14
3719
61