Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

passing test and implementation for &#xnn hex codes

  • Loading branch information...
commit 4a5213f5e107ce276ce885fe319fc09b4d7cdf1c 1 parent 92fe444
@substack authored
Showing with 28 additions and 6 deletions.
  1. +7 −1 index.js
  2. +1 −1  package.json
  3. +20 −4 test/codes.js
View
8 index.js
@@ -21,7 +21,10 @@ exports.encode = function (str) {
if (e) {
return '&' + (e.match(/;$/) ? e : e + ';');
}
- else if (cc > 127) {
+ else if (c.match(/\s/)) {
+ return c;
+ }
+ else if (cc < 32 || cc >= 127) {
return '&#' + cc + ';';
}
else {
@@ -39,6 +42,9 @@ exports.decode = function (str) {
.replace(/&#(\d+);?/, function (_, code) {
return String.fromCharCode(code);
})
+ .replace(/&#[xX]([A-Fa-f0-9]+);?/, function (_, hex) {
+ return String.fromCharCode(parseInt(hex, 16));
+ })
.replace(/&([^;\W]+;?)/g, function (m, e) {
var ee = e.replace(/;$/, '');
var target = entities[e]
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "ent",
"description": "Encode and decode HTML entities",
- "version": "0.0.2",
+ "version": "0.0.3",
"repository": "https://github.com/substack/node-ent.git",
"author": "James Halliday <mail@substack.net> (http://substack.net)",
"main": "./index.js",
View
24 test/codes.js
@@ -23,10 +23,26 @@ exports.num = function () {
};
exports.hex = function () {
- for (var i = 0; i < 256; i++) {
- var a = String.fromCharCode(i.toString(16));
+ for (var i = 0; i < 32; i++) {
+ var a = String.fromCharCode(i);
+ if (a.match(/\s/)) {
+ assert.equal(ent.decode(a), a);
+ }
+ else {
+ var b = '&#x' + i.toString(16) + ';';
+ assert.equal(ent.decode(b), a);
+ assert.equal(ent.encode(a), '&#' + i + ';');
+ }
+ }
+
+ for (var i = 127; i < 256; i++) {
+ var a = String.fromCharCode(i);
var b = '&#x' + i.toString(16) + ';';
- assert.eql(ent.encode(a), b);
- assert.eql(ent.decode(b), a);
+
+ assert.equal(ent.decode(b), a);
+ var encoded = ent.encode(a);
+ if (!encoded.match(/^&\w+;/)) {
+ assert.equal(encoded, '&#' + i + ';');
+ }
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.