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 0.3.0 to 0.4.0

compile.js

144

index.js

@@ -1,104 +0,81 @@

var modes = ["XML", "HTML4", "HTML5"];
var compiled = require("./compile.js"),
modes = ["XML", "HTML4", "HTML5"];
module.exports = {
decode: function(data, level){
if(!modes[level]) level = 0;
return module.exports["decode" + modes[level]](data);
},
decodeStrict: function(data, level){
if(!modes[level]) level = 0;
return module.exports["decode" + modes[level] + "Strict"](data);
},
encode: function(data, level){
if(!modes[level]) level = 0;
return module.exports["encode" + modes[level]](data);
}
};
var levels = modes.map(function(name, i){
var obj = compiled[name],
strict = genReplaceFunc(obj.strict, getStrictReplacer(obj.obj)),
//there is no non-strict mode for XML
normal = i === 0 ? strict : genReplaceFunc(obj.normal, getReplacer(obj.obj)),
inverse = getInverse(obj.inverseObj, obj.inverse);
modes.reduce(function(prev, name){
var obj = require("./entities/" + name.toLowerCase() + ".json");
exports["decode" + name + "Strict"] = strict;
exports["decode" + name] = normal;
exports["encode" + name] = inverse;
if(prev){
Object.keys(prev).forEach(function(name){
obj[name] = prev[name];
});
}
return {
strict: strict,
normal: normal,
inverse: inverse
};
});
module.exports["decode" + name + "Strict"] = getStrictReplacer(obj);
var decode = levels.map(function(l){ return l.normal; }),
decodeStrict = levels.map(function(l){ return l.strict; }),
inverse = levels.map(function(l){ return l.inverse; });
if(name === "XML"){
//there is no non-strict mode for XML
module.exports.decodeXML = module.exports.decodeXMLStrict;
} else {
module.exports["decode" + name] = getReplacer(obj);
}
exports.decode = function(data, level){
if(!(level >= 0 && level < 3)) level = 0;
return decode[level](data);
};
exports.decodeStrict = function(data, level){
if(!(level >= 0 && level < 3)) level = 0;
return decodeStrict[level](data);
};
exports.encode = function(data, level){
if(!(level >= 0 && level < 3)) level = 0;
return encode[level](data);
};
module.exports["encode" + name] = getReverse(obj);
return obj;
}, null);
function getReplacer(obj){
var keys = Object.keys(obj).sort();
var re = keys.join("|").replace(/(\w+)\|\1;/g, "$1;?");
// also match hex and char codes
re += "|#[xX][\\da-fA-F]+;?|#\\d+;?";
return genReplaceFunc(
new RegExp("&(?:" + re + ")", "g"),
function func(name){
if(name.charAt(1) === "#"){
if(name.charAt(2).toLowerCase() === "x"){
return String.fromCharCode(parseInt(name.substr(3), 16));
}
return String.fromCharCode(parseInt(name.substr(2), 10));
return function normalReplacer(name){
if(name.charAt(1) === "#"){
if(name.charAt(2).toLowerCase() === "x"){
return String.fromCharCode(parseInt(name.substr(3), 16));
}
return obj[name.substr(1)];
return String.fromCharCode(parseInt(name.substr(2), 10));
}
);
return obj[name.substr(1)];
};
}
function getStrictReplacer(obj){
var keys = Object.keys(obj).sort().filter(RegExp.prototype.test, /;$/);
var re = keys.map(function(name){
return name.slice(0, -1); //remove trailing semicolon
}).join("|");
// also match hex and char codes
re += "|#[xX][\\da-fA-F]+|#\\d+";
var expr = new RegExp("&(?:" + re + ");", "g");
return genReplaceFunc(expr, func);
function func(name){
if(name.charAt(1) === "#"){
if(name.charAt(2).toLowerCase() === "x"){
return String.fromCharCode(parseInt(name.substr(3), 16));
}
return String.fromCharCode(parseInt(name.substr(2), 10));
return function strictReplacer(name){
if(name.charAt(1) === "#"){
if(name.charAt(2).toLowerCase() === "x"){
return String.fromCharCode(parseInt(name.substr(3), 16));
}
return obj[name.substr(1)];
return String.fromCharCode(parseInt(name.substr(2), 10));
}
return obj[name.substr(1)];
};
}
var re_nonUTF8 = /[\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]/g;
var re_nonASCII = /[^\0-\x7F]/g,
re_astralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
function nonUTF8Replacer(c){
return "&#" + c.charCodeAt(0) + ";";
return "&#x" + c.charCodeAt(0).toString(16).toUpperCase() + ";";
}
function getReverse(obj){
var reverse = Object.keys(obj).filter(function(name){
//prefer identifiers with a semicolon
return name.substr(-1) === ";" || obj[name + ";"] !== obj[name];
}).reduce(function(reverse, name){
reverse[obj[name]] = name;
return reverse;
}, {});
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() + ";";
}
var regex = new RegExp("\\" + Object.keys(reverse).sort().join("|\\"), "g");
function getInverse(inverse, re){
function func(name){
return "&" + reverse[name];
return "&" + inverse[name];
}

@@ -108,4 +85,5 @@

return data
.replace(regex, func)
.replace(re_nonUTF8, nonUTF8Replacer);
.replace(re, func)
.replace(re_astralSymbols, astralReplacer)
.replace(re_nonASCII, nonUTF8Replacer);
};

@@ -112,0 +90,0 @@ }

{
"name": "entities",
"version": "0.3.0",
"description": "Encode & decode XML/HTML entities with ease",
"author": "Felix Boehm <me@feedic.com>",
"keywords": ["html", "xml", "entity", "encoding"],
"main": "./index.js",
"directories": {
"test": "test"
},
"devDependencies": {
"mocha": "~1.9.0"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git"
, "url": "git://github.com/fb55/node-entities.git"
},
"license": "BSD-like"
"name": "entities",
"version": "0.4.0",
"description": "Encode & decode XML/HTML entities with ease",
"author": "Felix Boehm <me@feedic.com>",
"keywords": [
"html",
"xml",
"entity",
"encoding"
],
"main": "./index.js",
"directories": {
"test": "test"
},
"devDependencies": {
"mocha": "~1.9.0"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/fb55/node-entities.git"
},
"license": "BSD-like"
}

@@ -8,3 +8,3 @@ var assert = require("assert");

input: "asdf & ÿ ü '",
xml: "asdf &amp; &#255; &#252; &apos;",
xml: "asdf &amp; &#xFF; &#xFC; &apos;",
html4: "asdf &amp; &yuml; &uuml; &apos;",

@@ -11,0 +11,0 @@ html5: "asdf &amp; &yuml; &uuml; &apos;"

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