entities
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -32,1 +32,3 @@ var encode = require("./lib/encode.js"), | ||
exports.decodeHTMLStrict = decode.HTMLStrict; | ||
exports.escape = encode.escape; |
@@ -19,3 +19,17 @@ var inverseXML = getInverseObj(require("../maps/xml.json")), | ||
function getInverseReplacer(inverse){ | ||
return new RegExp("\\" + Object.keys(inverse).sort().join("|\\"), "g"); | ||
var single = [], | ||
multiple = []; | ||
Object.keys(inverse).forEach(function(k){ | ||
if(k.length === 1){ | ||
single.push("\\" + k); | ||
} else { | ||
multiple.push(k); | ||
} | ||
}); | ||
//TODO add ranges | ||
multiple.unshift("[" + single.join() + "]"); | ||
return new RegExp(multiple.join("|"), "g"); | ||
} | ||
@@ -26,3 +40,3 @@ | ||
function nonUTF8Replacer(c){ | ||
function singleCharReplacer(c){ | ||
return "&#x" + c.charCodeAt(0).toString(16).toUpperCase() + ";"; | ||
@@ -48,4 +62,15 @@ } | ||
.replace(re_astralSymbols, astralReplacer) | ||
.replace(re_nonASCII, nonUTF8Replacer); | ||
.replace(re_nonASCII, singleCharReplacer); | ||
}; | ||
} | ||
} | ||
var re_xmlChars = getInverseReplacer(inverseXML); | ||
function escapeXML(data){ | ||
return data | ||
.replace(re_xmlChars, singleCharReplacer) | ||
.replace(re_astralSymbols, astralReplacer) | ||
.replace(re_nonASCII, singleCharReplacer); | ||
} | ||
exports.escape = escapeXML; |
{ | ||
"name": "entities", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Encode & decode XML/HTML entities with ease", | ||
@@ -5,0 +5,0 @@ "author": "Felix Boehm <me@feedic.com>", |
@@ -5,7 +5,2 @@ #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) | ||
####Features: | ||
* Focussed on ___speed___ | ||
* Supports three levels of entities: __XML__, __HTML4__ & __HTML5__ | ||
* Supports _char code_ entities (eg. `U`) | ||
##How to… | ||
@@ -20,14 +15,15 @@ | ||
```javascript | ||
var entities = require("entities"); | ||
//encoding | ||
require("entities").encode(<str> data[, <int> level]); | ||
entities.encodeXML("&"); // "&#38;" | ||
entities.encodeHTML("&"); // "&#38;" | ||
//decoding | ||
require("entities").decode(<str> data[, <int> level]); | ||
entities.decodeXML("asdf & ÿ ü '"); // "asdf & ÿ ü '" | ||
entities.decodeHTML("asdf & ÿ ü '"); // "asdf & ÿ ü '" | ||
``` | ||
The `level` attribute indicates what level of entities should be decoded (0 = XML, 1 = HTML4 and 2 = HTML5). The default is 0 (read: XML). | ||
<!-- TODO extend API --> | ||
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 |
@@ -143,2 +143,6 @@ var assert = require("assert"), | ||
}); | ||
it("should escape " + astral[c], function(){ | ||
assert.equal(entities.escape(astral[c]), "&#x" + c + ";"); | ||
}); | ||
}); | ||
@@ -152,1 +156,10 @@ | ||
}); | ||
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); | ||
} | ||
}); | ||
}); |
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
55474
303
0
28