Comparing version 0.5.0 to 1.0.0
106
index.js
@@ -1,99 +0,31 @@ | ||
var compiled = require("./compile.js"), | ||
modes = ["XML", "HTML4", "HTML5"]; | ||
var encode = require("./lib/encode.js"), | ||
decode = require("./lib/decode.js"); | ||
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); | ||
exports["decode" + name + "Strict"] = strict; | ||
exports["decode" + name] = normal; | ||
exports["encode" + name] = inverse; | ||
return { | ||
strict: strict, | ||
normal: normal, | ||
inverse: inverse | ||
}; | ||
}); | ||
var decode = levels.map(function(l){ return l.normal; }), | ||
decodeStrict = levels.map(function(l){ return l.strict; }), | ||
encode = levels.map(function(l){ return l.inverse; }); | ||
exports.decode = function(data, level){ | ||
if(!(level >= 0 && level < 3)) level = 0; | ||
return decode[level](data); | ||
return (!level || level <= 0 ? decode.XML : decode.HTML)(data); | ||
}; | ||
exports.decodeStrict = function(data, level){ | ||
if(!(level >= 0 && level < 3)) level = 0; | ||
return decodeStrict[level](data); | ||
return (!level || level <= 0 ? decode.XML : decode.HTMLStrict)(data); | ||
}; | ||
exports.encode = function(data, level){ | ||
if(!(level >= 0 && level < 3)) level = 0; | ||
return encode[level](data); | ||
return (!level || level <= 0 ? encode.XML : encode.HTML)(data); | ||
}; | ||
function getReplacer(obj){ | ||
return function normalReplacer(name){ | ||
if(name.charAt(1) === "#"){ | ||
if(name.charAt(2).toLowerCase() === "x"){ | ||
return codePointToSymbol(parseInt(name.substr(3), 16)); | ||
} | ||
return codePointToSymbol(parseInt(name.substr(2), 10)); | ||
} | ||
return obj[name.substr(1)]; | ||
}; | ||
} | ||
exports.encodeXML = encode.XML; | ||
function codePointToSymbol(entity){ | ||
return String.fromCharCode(entity); //TODO | ||
} | ||
exports.encodeHTML4 = | ||
exports.encodeHTML5 = | ||
exports.encodeHTML = encode.HTML; | ||
function getStrictReplacer(obj){ | ||
return function strictReplacer(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 obj[name.substr(1)]; | ||
}; | ||
} | ||
exports.decodeXML = | ||
exports.decodeXMLStrict = decode.XML; | ||
var re_nonASCII = /[^\0-\x7F]/g, | ||
re_astralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
exports.decodeHTML4 = | ||
exports.decodeHTML5 = | ||
exports.decodeHTML = decode.HTML; | ||
function nonUTF8Replacer(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 getInverse(inverse, re){ | ||
function func(name){ | ||
return "&" + inverse[name]; | ||
} | ||
return function(data){ | ||
return data | ||
.replace(re, func) | ||
.replace(re_astralSymbols, astralReplacer) | ||
.replace(re_nonASCII, nonUTF8Replacer); | ||
}; | ||
} | ||
function genReplaceFunc(regex, func){ | ||
return function(data){ | ||
return data.replace(regex, func); | ||
}; | ||
} | ||
exports.decodeHTML4Strict = | ||
exports.decodeHTML5Strict = | ||
exports.decodeHTMLStrict = decode.HTMLStrict; |
{ | ||
"name": "entities", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Encode & decode XML/HTML entities with ease", | ||
@@ -17,6 +17,13 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"devDependencies": { | ||
"mocha": "~1.9.0" | ||
"mocha": "1", | ||
"mocha-lcov-reporter": "*", | ||
"coveralls": "*", | ||
"istanbul": "*", | ||
"jshint": "2" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha && npm run lint", | ||
"lint": "jshint index.js lib/*.js test/*.js", | ||
"lcov": "istanbul cover _mocha --report lcovonly -- -R spec", | ||
"coveralls": "npm run lint && npm run lcov && (cat coverage/lcov.info | coveralls || exit 0)" | ||
}, | ||
@@ -27,3 +34,22 @@ "repository": { | ||
}, | ||
"license": "BSD-like" | ||
"license": "BSD-like", | ||
"jshintConfig": { | ||
"eqeqeq": true, | ||
"freeze": true, | ||
"latedef": "nofunc", | ||
"noarg": true, | ||
"nonbsp": true, | ||
"quotmark": "double", | ||
"undef": true, | ||
"unused": true, | ||
"trailing": true, | ||
"eqnull": true, | ||
"proto": true, | ||
"smarttabs": true, | ||
"node": true, | ||
"globals": { | ||
"describe": true, | ||
"it": true | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
#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/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) | ||
@@ -28,1 +28,5 @@ En- & decoder for XML/HTML entities. | ||
There are also methods to access the level directly. Just append the name of the level to the action and you're ready to go (e.g. `encodeHTML4(data)`, `decodeXML(data)`). | ||
--- | ||
License: BSD-like |
var assert = require("assert"), | ||
path = require("path"), | ||
entities = require('../'); | ||
entities = require("../"); | ||
@@ -10,9 +10,7 @@ describe("Encode->decode test", function(){ | ||
xml: "asdf & ÿ ü '", | ||
html4: "asdf & ÿ ü '", | ||
html5: "asdf & ÿ ü '" | ||
html: "asdf & ÿ ü '" | ||
}, { | ||
input: "&", | ||
xml: "&#38;", | ||
html4: "&#38;", | ||
html5: "&#38;" | ||
html: "&#38;" | ||
}, | ||
@@ -38,16 +36,8 @@ ]; | ||
var encodedHTML4 = entities.encodeHTML4(tc.input); | ||
it("should HTML4 encode " + tc.input, function(){ | ||
assert.equal(encodedHTML4, tc.html4); | ||
}); | ||
it("should HTML4 decode " + encodedHTML4, function(){ | ||
assert.equal(entities.decodeHTML4(encodedHTML4), tc.input); | ||
}); | ||
var encodedHTML5 = entities.encodeHTML5(tc.input); | ||
it("should HTML5 encode " + tc.input, function(){ | ||
assert.equal(encodedHTML5, tc.html5); | ||
assert.equal(encodedHTML5, tc.html); | ||
}); | ||
it("should HTML5 decode " + encodedHTML5, function(){ | ||
assert.equal(entities.decodeHTML5(encodedHTML5), tc.input); | ||
assert.equal(entities.decodeHTML(encodedHTML5), tc.input); | ||
}); | ||
@@ -76,6 +66,6 @@ }); | ||
it("should HTML4 decode " + tc.input, function(){ | ||
assert.equal(entities.decodeHTML4(tc.input), tc.output); | ||
assert.equal(entities.decodeHTML(tc.input), tc.output); | ||
}); | ||
it("should HTML5 decode " + tc.input, function(){ | ||
assert.equal(entities.decodeHTML5(tc.input), tc.output); | ||
assert.equal(entities.decodeHTML(tc.input), tc.output); | ||
}); | ||
@@ -85,7 +75,7 @@ }); | ||
var levels = ["xml", "html4", "html5"]; | ||
var levels = ["xml", "entities"]; | ||
describe("Documents", function(){ | ||
levels | ||
.map(function(n){ return path.join("..", "entities", n); }) | ||
.map(function(n){ return path.join("..", "maps", n); }) | ||
.map(require) | ||
@@ -97,3 +87,3 @@ .forEach(function(doc, i){ | ||
for(var l = i; l < levels.length; l++){ | ||
assert.equal(entities.decode("&" + e, l), doc[e]); | ||
assert.equal(entities.decode("&" + e + ";", l), doc[e]); | ||
} | ||
@@ -107,8 +97,4 @@ }); | ||
Object.keys(doc).forEach(function(e){ | ||
if(e.substr(-1) !== ";"){ | ||
assert.equal(entities.decodeStrict("&" + e, i), "&" + e); | ||
return; | ||
} | ||
for(var l = i; l < levels.length; l++){ | ||
assert.equal(entities.decodeStrict("&" + e, l), doc[e]); | ||
assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]); | ||
} | ||
@@ -122,3 +108,2 @@ }); | ||
Object.keys(doc).forEach(function(e){ | ||
if(e.substr(-1) !== ";") return; | ||
for(var l = i; l < levels.length; l++){ | ||
@@ -131,2 +116,14 @@ assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]); | ||
}); | ||
var legacy = require("../maps/legacy.json"); | ||
describe("Legacy", function(){ | ||
it("should decode", runLegacy); | ||
}); | ||
function runLegacy(){ | ||
Object.keys(legacy).forEach(function(e){ | ||
assert.equal(entities.decodeHTML("&" + e), legacy[e]); | ||
}); | ||
} | ||
}); | ||
@@ -139,7 +136,14 @@ | ||
var astralSpecial = { | ||
"80": "\u20AC", | ||
"110000": "\uFFFD" | ||
}; | ||
describe("Astral entities", function(){ | ||
Object.keys(astral).forEach(function(c){ | ||
/*it("should decode " + astral[c], function(){ | ||
it("should decode " + astral[c], function(){ | ||
assert.equal(entities.decode("&#x" + c + ";"), astral[c]); | ||
});*/ | ||
}); | ||
it("should encode " + astral[c], function(){ | ||
@@ -149,2 +153,8 @@ assert.equal(entities.encode(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]); | ||
}); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14
271
1
32
0
54893
5
1