Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

entities

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

entities - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

27

index.js
var encode = require("./lib/encode.js"),
decode = require("./lib/decode.js");
exports.decode = function(data, level){
return (!level || level <= 0 ? decode.XML : decode.HTML)(data);
exports.decode = function(data, level) {
return (!level || level <= 0 ? decode.XML : decode.HTML)(data);
};
exports.decodeStrict = function(data, level){
return (!level || level <= 0 ? decode.XML : decode.HTMLStrict)(data);
exports.decodeStrict = function(data, level) {
return (!level || level <= 0 ? decode.XML : decode.HTMLStrict)(data);
};
exports.encode = function(data, level){
return (!level || level <= 0 ? encode.XML : encode.HTML)(data);
exports.encode = function(data, level) {
return (!level || level <= 0 ? encode.XML : encode.HTML)(data);
};

@@ -18,17 +18,10 @@

exports.encodeHTML4 =
exports.encodeHTML5 =
exports.encodeHTML = encode.HTML;
exports.encodeHTML4 = exports.encodeHTML5 = exports.encodeHTML = encode.HTML;
exports.decodeXML =
exports.decodeXMLStrict = decode.XML;
exports.decodeXML = exports.decodeXMLStrict = decode.XML;
exports.decodeHTML4 =
exports.decodeHTML5 =
exports.decodeHTML = decode.HTML;
exports.decodeHTML4 = exports.decodeHTML5 = exports.decodeHTML = decode.HTML;
exports.decodeHTML4Strict =
exports.decodeHTML5Strict =
exports.decodeHTMLStrict = decode.HTMLStrict;
exports.decodeHTML4Strict = exports.decodeHTML5Strict = exports.decodeHTMLStrict = decode.HTMLStrict;
exports.escape = encode.escape;

@@ -6,22 +6,21 @@ var decodeMap = require("../maps/decode.json");

// modified version of https://github.com/mathiasbynens/he/blob/master/src/he.js#L94-L119
function decodeCodePoint(codePoint){
function decodeCodePoint(codePoint) {
if ((codePoint >= 0xd800 && codePoint <= 0xdfff) || codePoint > 0x10ffff) {
return "\uFFFD";
}
if((codePoint >= 0xD800 && codePoint <= 0xDFFF) || codePoint > 0x10FFFF){
return "\uFFFD";
}
if (codePoint in decodeMap) {
codePoint = decodeMap[codePoint];
}
if(codePoint in decodeMap){
codePoint = decodeMap[codePoint];
}
var output = "";
var output = "";
if (codePoint > 0xffff) {
codePoint -= 0x10000;
output += String.fromCharCode(((codePoint >>> 10) & 0x3ff) | 0xd800);
codePoint = 0xdc00 | (codePoint & 0x3ff);
}
if(codePoint > 0xFFFF){
codePoint -= 0x10000;
output += String.fromCharCode(codePoint >>> 10 & 0x3FF | 0xD800);
codePoint = 0xDC00 | codePoint & 0x3FF;
}
output += String.fromCharCode(codePoint);
return output;
output += String.fromCharCode(codePoint);
return output;
}
var entityMap = require("../maps/entities.json"),
legacyMap = require("../maps/legacy.json"),
xmlMap = require("../maps/xml.json"),
xmlMap = require("../maps/xml.json"),
decodeCodePoint = require("./decode_codepoint.js");
var decodeXMLStrict = getStrictDecoder(xmlMap),
var decodeXMLStrict = getStrictDecoder(xmlMap),
decodeHTMLStrict = getStrictDecoder(entityMap);
function getStrictDecoder(map){
var keys = Object.keys(map).join("|"),
replace = getReplacer(map);
function getStrictDecoder(map) {
var keys = Object.keys(map).join("|"),
replace = getReplacer(map);
keys += "|#[xX][\\da-fA-F]+|#\\d+";
keys += "|#[xX][\\da-fA-F]+|#\\d+";
var re = new RegExp("&(?:" + keys + ");", "g");
var re = new RegExp("&(?:" + keys + ");", "g");
return function(str){
return String(str).replace(re, replace);
};
return function(str) {
return String(str).replace(re, replace);
};
}
var decodeHTML = (function(){
var legacy = Object.keys(legacyMap)
.sort(sorter);
var decodeHTML = (function() {
var legacy = Object.keys(legacyMap).sort(sorter);
var keys = Object.keys(entityMap)
.sort(sorter);
var keys = Object.keys(entityMap).sort(sorter);
for(var i = 0, j = 0; i < keys.length; i++){
if(legacy[j] === keys[i]){
keys[i] += ";?";
j++;
} else {
keys[i] += ";";
}
}
for (var i = 0, j = 0; i < keys.length; i++) {
if (legacy[j] === keys[i]) {
keys[i] += ";?";
j++;
} else {
keys[i] += ";";
}
}
var re = new RegExp("&(?:" + keys.join("|") + "|#[xX][\\da-fA-F]+;?|#\\d+;?)", "g"),
replace = getReplacer(entityMap);
var re = new RegExp("&(?:" + keys.join("|") + "|#[xX][\\da-fA-F]+;?|#\\d+;?)", "g"),
replace = getReplacer(entityMap);
function replacer(str){
if(str.substr(-1) !== ";") str += ";";
return replace(str);
}
function replacer(str) {
if (str.substr(-1) !== ";") str += ";";
return replace(str);
}
//TODO consider creating a merged map
return function(str){
return String(str).replace(re, replacer);
};
}());
//TODO consider creating a merged map
return function(str) {
return String(str).replace(re, replacer);
};
})();
function sorter(a, b){
return a < b ? 1 : -1;
function sorter(a, b) {
return a < b ? 1 : -1;
}
function getReplacer(map){
return function replace(str){
if(str.charAt(1) === "#"){
if(str.charAt(2) === "X" || str.charAt(2) === "x"){
return decodeCodePoint(parseInt(str.substr(3), 16));
}
return decodeCodePoint(parseInt(str.substr(2), 10));
}
return map[str.slice(1, -1)];
};
function getReplacer(map) {
return function replace(str) {
if (str.charAt(1) === "#") {
if (str.charAt(2) === "X" || str.charAt(2) === "x") {
return decodeCodePoint(parseInt(str.substr(3), 16));
}
return decodeCodePoint(parseInt(str.substr(2), 10));
}
return map[str.slice(1, -1)];
};
}
module.exports = {
XML: decodeXMLStrict,
HTML: decodeHTML,
HTMLStrict: decodeHTMLStrict
};
XML: decodeXMLStrict,
HTML: decodeHTML,
HTMLStrict: decodeHTMLStrict
};

@@ -11,25 +11,27 @@ var inverseXML = getInverseObj(require("../maps/xml.json")),

function getInverseObj(obj){
return Object.keys(obj).sort().reduce(function(inverse, name){
inverse[obj[name]] = "&" + name + ";";
return inverse;
}, {});
function getInverseObj(obj) {
return Object.keys(obj)
.sort()
.reduce(function(inverse, name) {
inverse[obj[name]] = "&" + name + ";";
return inverse;
}, {});
}
function getInverseReplacer(inverse){
var single = [],
multiple = [];
function getInverseReplacer(inverse) {
var single = [],
multiple = [];
Object.keys(inverse).forEach(function(k){
if(k.length === 1){
single.push("\\" + k);
} else {
multiple.push(k);
}
});
Object.keys(inverse).forEach(function(k) {
if (k.length === 1) {
single.push("\\" + k);
} else {
multiple.push(k);
}
});
//TODO add ranges
multiple.unshift("[" + single.join("") + "]");
//TODO add ranges
multiple.unshift("[" + single.join("") + "]");
return new RegExp(multiple.join("|"), "g");
return new RegExp(multiple.join("|"), "g");
}

@@ -40,25 +42,32 @@

function singleCharReplacer(c){
return "&#x" + c.charCodeAt(0).toString(16).toUpperCase() + ";";
function singleCharReplacer(c) {
return (
"&#x" +
c
.charCodeAt(0)
.toString(16)
.toUpperCase() +
";"
);
}
function astralReplacer(c){
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
var high = c.charCodeAt(0);
var low = c.charCodeAt(1);
var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
return "&#x" + codePoint.toString(16).toUpperCase() + ";";
function astralReplacer(c) {
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
var high = c.charCodeAt(0);
var low = c.charCodeAt(1);
var codePoint = (high - 0xd800) * 0x400 + low - 0xdc00 + 0x10000;
return "&#x" + codePoint.toString(16).toUpperCase() + ";";
}
function getInverse(inverse, re){
function func(name){
return inverse[name];
}
function getInverse(inverse, re) {
function func(name) {
return inverse[name];
}
return function(data){
return data
.replace(re, func)
.replace(re_astralSymbols, astralReplacer)
.replace(re_nonASCII, singleCharReplacer);
};
return function(data) {
return data
.replace(re, func)
.replace(re_astralSymbols, astralReplacer)
.replace(re_nonASCII, singleCharReplacer);
};
}

@@ -68,9 +77,9 @@

function escapeXML(data){
return data
.replace(re_xmlChars, singleCharReplacer)
.replace(re_astralSymbols, astralReplacer)
.replace(re_nonASCII, singleCharReplacer);
function escapeXML(data) {
return data
.replace(re_xmlChars, singleCharReplacer)
.replace(re_astralSymbols, astralReplacer)
.replace(re_nonASCII, singleCharReplacer);
}
exports.escape = escapeXML;
{
"name": "entities",
"version": "1.1.1",
"version": "1.1.2",
"description": "Encode & decode XML/HTML entities with ease",

@@ -10,2 +10,3 @@ "author": "Felix Boehm <me@feedic.com>",

"entity",
"decoding",
"encoding"

@@ -18,3 +19,3 @@ ],

"devDependencies": {
"mocha": "1",
"mocha": "^5.0.1",
"mocha-lcov-reporter": "*",

@@ -33,5 +34,5 @@ "coveralls": "*",

"type": "git",
"url": "git://github.com/fb55/node-entities.git"
"url": "git://github.com/fb55/entities.git"
},
"license": "BSD-like",
"license": "BSD-2-Clause",
"jshintConfig": {

@@ -55,3 +56,6 @@ "eqeqeq": true,

}
},
"prettier": {
"tabWidth": 4
}
}

@@ -1,12 +0,12 @@

#entities [![NPM version](http://img.shields.io/npm/v/entities.svg)](https://npmjs.org/package/entities) [![Downloads](https://img.shields.io/npm/dm/entities.svg)](https://npmjs.org/package/entities) [![Build Status](http://img.shields.io/travis/fb55/node-entities.svg)](http://travis-ci.org/fb55/node-entities) [![Coverage](http://img.shields.io/coveralls/fb55/node-entities.svg)](https://coveralls.io/r/fb55/node-entities)
# entities [![NPM version](http://img.shields.io/npm/v/entities.svg)](https://npmjs.org/package/entities) [![Downloads](https://img.shields.io/npm/dm/entities.svg)](https://npmjs.org/package/entities) [![Build Status](http://img.shields.io/travis/fb55/entities.svg)](http://travis-ci.org/fb55/entities) [![Coverage](http://img.shields.io/coveralls/fb55/entities.svg)](https://coveralls.io/r/fb55/entities)
En- & decoder for XML/HTML entities.
##How to…
## How to…
###…install `entities`
### …install `entities`
npm i entities
###…use `entities`
### …use `entities`

@@ -27,2 +27,2 @@ ```javascript

License: BSD-like
License: BSD-2-Clause

@@ -5,72 +5,73 @@ var assert = require("assert"),

describe("Encode->decode test", function(){
var testcases = [
{
input: "asdf & ÿ ü '",
xml: "asdf &amp; &#xFF; &#xFC; &apos;",
html: "asdf &amp; &yuml; &uuml; &apos;"
}, {
input: "&#38;",
xml: "&amp;#38;",
html: "&amp;&num;38&semi;"
},
];
testcases.forEach(function(tc) {
var encodedXML = entities.encodeXML(tc.input);
it("should XML encode " + tc.input, function(){
assert.equal(encodedXML, tc.xml);
});
it("should default to XML encode " + tc.input, function(){
assert.equal(entities.encode(tc.input), tc.xml);
});
it("should XML decode " + encodedXML, function(){
assert.equal(entities.decodeXML(encodedXML), tc.input);
});
it("should default to XML encode " + encodedXML, function(){
assert.equal(entities.decode(encodedXML), tc.input);
});
it("should default strict to XML encode " + encodedXML, function(){
assert.equal(entities.decodeStrict(encodedXML), tc.input);
});
describe("Encode->decode test", function() {
var testcases = [
{
input: "asdf & ÿ ü '",
xml: "asdf &amp; &#xFF; &#xFC; &apos;",
html: "asdf &amp; &yuml; &uuml; &apos;"
},
{
input: "&#38;",
xml: "&amp;#38;",
html: "&amp;&num;38&semi;"
}
];
testcases.forEach(function(tc) {
var encodedXML = entities.encodeXML(tc.input);
it("should XML encode " + tc.input, function() {
assert.equal(encodedXML, tc.xml);
});
it("should default to XML encode " + tc.input, function() {
assert.equal(entities.encode(tc.input), tc.xml);
});
it("should XML decode " + encodedXML, function() {
assert.equal(entities.decodeXML(encodedXML), tc.input);
});
it("should default to XML encode " + encodedXML, function() {
assert.equal(entities.decode(encodedXML), tc.input);
});
it("should default strict to XML encode " + encodedXML, function() {
assert.equal(entities.decodeStrict(encodedXML), tc.input);
});
var encodedHTML5 = entities.encodeHTML5(tc.input);
it("should HTML5 encode " + tc.input, function(){
assert.equal(encodedHTML5, tc.html);
});
it("should HTML5 decode " + encodedHTML5, function(){
assert.equal(entities.decodeHTML(encodedHTML5), tc.input);
});
});
var encodedHTML5 = entities.encodeHTML5(tc.input);
it("should HTML5 encode " + tc.input, function() {
assert.equal(encodedHTML5, tc.html);
});
it("should HTML5 decode " + encodedHTML5, function() {
assert.equal(entities.decodeHTML(encodedHTML5), tc.input);
});
});
it("should encode data URIs (issue 16)", function(){
var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";
assert.equal(entities.decode(entities.encode(data)), data);
});
it("should encode data URIs (issue 16)", function() {
var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";
assert.equal(entities.decode(entities.encode(data)), data);
});
});
describe("Decode test", function(){
var testcases = [
{ input: "&amp;amp;", output: "&amp;" },
{ input: "&amp;#38;", output: "&#38;" },
{ input: "&amp;#x26;", output: "&#x26;" },
{ input: "&amp;#X26;", output: "&#X26;" },
{ input: "&#38;#38;", output: "&#38;" },
{ input: "&#x26;#38;", output: "&#38;" },
{ input: "&#X26;#38;", output: "&#38;" },
{ input: "&#x3a;", output: ":" },
{ input: "&#x3A;", output: ":" },
{ input: "&#X3a;", output: ":" },
{ input: "&#X3A;", output: ":" }
];
testcases.forEach(function(tc) {
it("should XML decode " + tc.input, function(){
assert.equal(entities.decodeXML(tc.input), tc.output);
});
it("should HTML4 decode " + tc.input, function(){
assert.equal(entities.decodeHTML(tc.input), tc.output);
});
it("should HTML5 decode " + tc.input, function(){
assert.equal(entities.decodeHTML(tc.input), tc.output);
});
});
describe("Decode test", function() {
var testcases = [
{ input: "&amp;amp;", output: "&amp;" },
{ input: "&amp;#38;", output: "&#38;" },
{ input: "&amp;#x26;", output: "&#x26;" },
{ input: "&amp;#X26;", output: "&#X26;" },
{ input: "&#38;#38;", output: "&#38;" },
{ input: "&#x26;#38;", output: "&#38;" },
{ input: "&#X26;#38;", output: "&#38;" },
{ input: "&#x3a;", output: ":" },
{ input: "&#x3A;", output: ":" },
{ input: "&#X3a;", output: ":" },
{ input: "&#X3A;", output: ":" }
];
testcases.forEach(function(tc) {
it("should XML decode " + tc.input, function() {
assert.equal(entities.decodeXML(tc.input), tc.output);
});
it("should HTML4 decode " + tc.input, function() {
assert.equal(entities.decodeHTML(tc.input), tc.output);
});
it("should HTML5 decode " + tc.input, function() {
assert.equal(entities.decodeHTML(tc.input), tc.output);
});
});
});

@@ -80,91 +81,92 @@

describe("Documents", function(){
levels
.map(function(n){ return path.join("..", "maps", n); })
.map(require)
.forEach(function(doc, i){
describe("Decode", function(){
it(levels[i], function(){
Object.keys(doc).forEach(function(e){
for(var l = i; l < levels.length; l++){
assert.equal(entities.decode("&" + e + ";", l), doc[e]);
}
});
});
});
describe("Documents", function() {
levels
.map(function(n) {
return path.join("..", "maps", n);
})
.map(require)
.forEach(function(doc, i) {
describe("Decode", function() {
it(levels[i], function() {
Object.keys(doc).forEach(function(e) {
for (var l = i; l < levels.length; l++) {
assert.equal(entities.decode("&" + e + ";", l), doc[e]);
}
});
});
});
describe("Decode strict", function(){
it(levels[i], function(){
Object.keys(doc).forEach(function(e){
for(var l = i; l < levels.length; l++){
assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
}
});
});
});
describe("Decode strict", function() {
it(levels[i], function() {
Object.keys(doc).forEach(function(e) {
for (var l = i; l < levels.length; l++) {
assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
}
});
});
});
describe("Encode", function(){
it(levels[i], function(){
Object.keys(doc).forEach(function(e){
for(var l = i; l < levels.length; l++){
assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
}
});
});
});
});
describe("Encode", function() {
it(levels[i], function() {
Object.keys(doc).forEach(function(e) {
for (var l = i; l < levels.length; l++) {
assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
}
});
});
});
});
var legacy = require("../maps/legacy.json");
var legacy = require("../maps/legacy.json");
describe("Legacy", function(){
it("should decode", runLegacy);
});
describe("Legacy", function() {
it("should decode", runLegacy);
});
function runLegacy(){
Object.keys(legacy).forEach(function(e){
assert.equal(entities.decodeHTML("&" + e), legacy[e]);
});
}
function runLegacy() {
Object.keys(legacy).forEach(function(e) {
assert.equal(entities.decodeHTML("&" + e), legacy[e]);
});
}
});
var astral = {
"1D306": "\uD834\uDF06",
"1D11E": "\uD834\uDD1E"
"1D306": "\uD834\uDF06",
"1D11E": "\uD834\uDD1E"
};
var astralSpecial = {
"80": "\u20AC",
"110000": "\uFFFD"
"80": "\u20AC",
"110000": "\uFFFD"
};
describe("Astral entities", function() {
Object.keys(astral).forEach(function(c) {
it("should decode " + astral[c], function() {
assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
});
describe("Astral entities", function(){
Object.keys(astral).forEach(function(c){
it("should decode " + astral[c], function(){
assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
});
it("should encode " + astral[c], function() {
assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
});
it("should encode " + astral[c], function(){
assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
});
it("should escape " + astral[c], function() {
assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
});
});
it("should escape " + astral[c], function(){
assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
});
});
Object.keys(astralSpecial).forEach(function(c){
it("special should decode \\u" + c, function(){
assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
});
});
Object.keys(astralSpecial).forEach(function(c) {
it("special should decode \\u" + c, function() {
assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
});
});
});
describe("Escape", function(){
it("should always decode ASCII chars", function(){
for(var i = 0; i < 0x7F; i++){
var c = String.fromCharCode(i);
assert.equal(entities.decodeXML(entities.escape(c)), c);
}
});
describe("Escape", function() {
it("should always decode ASCII chars", function() {
for (var i = 0; i < 0x7f; i++) {
var c = String.fromCharCode(i);
assert.equal(entities.decodeXML(entities.escape(c)), c);
}
});
});

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