Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

passing test and implementation for &#xnn hex codes

  • Loading branch information...
commit 4a5213f5e107ce276ce885fe319fc09b4d7cdf1c 1 parent 92fe444
James Halliday authored

Showing 3 changed files with 28 additions and 6 deletions. Show diff stats Hide diff stats

  1. +7 1 index.js
  2. +1 1  package.json
  3. +20 4 test/codes.js
8 index.js
@@ -21,7 +21,10 @@ exports.encode = function (str) {
21 21 if (e) {
22 22 return '&' + (e.match(/;$/) ? e : e + ';');
23 23 }
24   - else if (cc > 127) {
  24 + else if (c.match(/\s/)) {
  25 + return c;
  26 + }
  27 + else if (cc < 32 || cc >= 127) {
25 28 return '&#' + cc + ';';
26 29 }
27 30 else {
@@ -39,6 +42,9 @@ exports.decode = function (str) {
39 42 .replace(/&#(\d+);?/, function (_, code) {
40 43 return String.fromCharCode(code);
41 44 })
  45 + .replace(/&#[xX]([A-Fa-f0-9]+);?/, function (_, hex) {
  46 + return String.fromCharCode(parseInt(hex, 16));
  47 + })
42 48 .replace(/&([^;\W]+;?)/g, function (m, e) {
43 49 var ee = e.replace(/;$/, '');
44 50 var target = entities[e]
2  package.json
... ... @@ -1,7 +1,7 @@
1 1 {
2 2 "name": "ent",
3 3 "description": "Encode and decode HTML entities",
4   - "version": "0.0.2",
  4 + "version": "0.0.3",
5 5 "repository": "https://github.com/substack/node-ent.git",
6 6 "author": "James Halliday <mail@substack.net> (http://substack.net)",
7 7 "main": "./index.js",
24 test/codes.js
@@ -23,10 +23,26 @@ exports.num = function () {
23 23 };
24 24
25 25 exports.hex = function () {
26   - for (var i = 0; i < 256; i++) {
27   - var a = String.fromCharCode(i.toString(16));
  26 + for (var i = 0; i < 32; i++) {
  27 + var a = String.fromCharCode(i);
  28 + if (a.match(/\s/)) {
  29 + assert.equal(ent.decode(a), a);
  30 + }
  31 + else {
  32 + var b = '&#x' + i.toString(16) + ';';
  33 + assert.equal(ent.decode(b), a);
  34 + assert.equal(ent.encode(a), '&#' + i + ';');
  35 + }
  36 + }
  37 +
  38 + for (var i = 127; i < 256; i++) {
  39 + var a = String.fromCharCode(i);
28 40 var b = '&#x' + i.toString(16) + ';';
29   - assert.eql(ent.encode(a), b);
30   - assert.eql(ent.decode(b), a);
  41 +
  42 + assert.equal(ent.decode(b), a);
  43 + var encoded = ent.encode(a);
  44 + if (!encoded.match(/^&\w+;/)) {
  45 + assert.equal(encoded, '&#' + i + ';');
  46 + }
31 47 }
32 48 };

0 comments on commit 4a5213f

Please sign in to comment.
Something went wrong with that request. Please try again.