entities 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/compile.js CHANGED
@@ -23,9 +23,13 @@ modes.reduce(function(prev, name, i){
23
23
  return obj;
24
24
  }, null);
25
25
 
26
+ function sortDesc(a, b){
27
+ return a < b ? 1 : -1;
28
+ }
29
+
26
30
  function getReplacer(obj){
27
- var keys = Object.keys(obj).sort();
28
- var re = keys.join("|").replace(/(\w+)\|\1;/g, "$1;?");
31
+ var keys = Object.keys(obj).sort(sortDesc);
32
+ var re = keys.join("|")//.replace(/(\w+);\|\1/g, "$1;?");
29
33
 
30
34
  // also match hex and char codes
31
35
  re += "|#[xX][\\da-fA-F]+;?|#\\d+;?";
@@ -34,7 +38,7 @@ function getReplacer(obj){
34
38
  }
35
39
 
36
40
  function getStrictReplacer(obj){
37
- var keys = Object.keys(obj).sort().filter(RegExp.prototype.test, /;$/);
41
+ var keys = Object.keys(obj).sort(sortDesc).filter(RegExp.prototype.test, /;$/);
38
42
  var re = keys.map(function(name){
39
43
  return name.slice(0, -1); //remove trailing semicolon
40
44
  }).join("|");
package/index.js CHANGED
@@ -40,14 +40,18 @@ function getReplacer(obj){
40
40
  return function normalReplacer(name){
41
41
  if(name.charAt(1) === "#"){
42
42
  if(name.charAt(2).toLowerCase() === "x"){
43
- return String.fromCharCode(parseInt(name.substr(3), 16));
43
+ return codePointToSymbol(parseInt(name.substr(3), 16));
44
44
  }
45
- return String.fromCharCode(parseInt(name.substr(2), 10));
45
+ return codePointToSymbol(parseInt(name.substr(2), 10));
46
46
  }
47
47
  return obj[name.substr(1)];
48
48
  };
49
49
  }
50
50
 
51
+ function codePointToSymbol(entity){
52
+ return String.fromCharCode(entity); //TODO
53
+ }
54
+
51
55
  function getStrictReplacer(obj){
52
56
  return function strictReplacer(name){
53
57
  if(name.charAt(1) === "#"){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "entities",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Encode & decode XML/HTML entities with ease",
5
5
  "author": "Felix Boehm <me@feedic.com>",
6
6
  "keywords": [
package/test/test.js CHANGED
@@ -1,7 +1,8 @@
1
- var assert = require("assert");
2
- var entities = require('../');
1
+ var assert = require("assert"),
2
+ path = require("path"),
3
+ entities = require('../');
3
4
 
4
- describe("Encode->decode test", function() {
5
+ describe("Encode->decode test", function(){
5
6
  var testcases = [
6
7
  {
7
8
  input: "asdf & ÿ ü '",
@@ -17,30 +18,41 @@ describe("Encode->decode test", function() {
17
18
  ];
18
19
  testcases.forEach(function(tc) {
19
20
  var encodedXML = entities.encodeXML(tc.input);
20
- it("should XML encode " + tc.input, function() {
21
+ it("should XML encode " + tc.input, function(){
21
22
  assert.equal(encodedXML, tc.xml);
22
23
  });
23
- it("should XML decode " + encodedXML, function() {
24
+ it("should default to XML encode " + tc.input, function(){
25
+ assert.equal(entities.encode(tc.input), tc.xml);
26
+ });
27
+ it("should XML decode " + encodedXML, function(){
24
28
  assert.equal(entities.decodeXML(encodedXML), tc.input);
25
29
  });
30
+ it("should default to XML encode " + encodedXML, function(){
31
+ assert.equal(entities.decode(encodedXML), tc.input);
32
+ });
33
+ it("should default strict to XML encode " + encodedXML, function(){
34
+ assert.equal(entities.decodeStrict(encodedXML), tc.input);
35
+ });
36
+
26
37
  var encodedHTML4 = entities.encodeHTML4(tc.input);
27
- it("should HTML4 encode " + tc.input, function() {
38
+ it("should HTML4 encode " + tc.input, function(){
28
39
  assert.equal(encodedHTML4, tc.html4);
29
40
  });
30
- it("should HTML4 decode " + encodedHTML4, function() {
41
+ it("should HTML4 decode " + encodedHTML4, function(){
31
42
  assert.equal(entities.decodeHTML4(encodedHTML4), tc.input);
32
43
  });
44
+
33
45
  var encodedHTML5 = entities.encodeHTML5(tc.input);
34
- it("should HTML5 encode " + tc.input, function() {
46
+ it("should HTML5 encode " + tc.input, function(){
35
47
  assert.equal(encodedHTML5, tc.html5);
36
48
  });
37
- it("should HTML5 decode " + encodedHTML5, function() {
49
+ it("should HTML5 decode " + encodedHTML5, function(){
38
50
  assert.equal(entities.decodeHTML5(encodedHTML5), tc.input);
39
51
  });
40
52
  });
41
53
  });
42
54
 
43
- describe("Decode test", function() {
55
+ describe("Decode test", function(){
44
56
  var testcases = [
45
57
  { input: "&amp;amp;", output: "&amp;" },
46
58
  { input: "&amp;#38;", output: "&#38;" },
@@ -55,14 +67,74 @@ describe("Decode test", function() {
55
67
  { input: "&#X3A;", output: ":" }
56
68
  ];
57
69
  testcases.forEach(function(tc) {
58
- it("should XML decode " + tc.input, function() {
70
+ it("should XML decode " + tc.input, function(){
59
71
  assert.equal(entities.decodeXML(tc.input), tc.output);
60
72
  });
61
- it("should HTML4 decode " + tc.input, function() {
73
+ it("should HTML4 decode " + tc.input, function(){
62
74
  assert.equal(entities.decodeHTML4(tc.input), tc.output);
63
75
  });
64
- it("should HTML5 decode " + tc.input, function() {
76
+ it("should HTML5 decode " + tc.input, function(){
65
77
  assert.equal(entities.decodeHTML5(tc.input), tc.output);
66
78
  });
67
79
  });
68
80
  });
81
+
82
+ var levels = ["xml", "html4", "html5"];
83
+
84
+ describe("Documents", function(){
85
+ levels
86
+ .map(function(n){ return path.join("..", "entities", n); })
87
+ .map(require)
88
+ .forEach(function(doc, i){
89
+ describe("Decode", function(){
90
+ it(levels[i], function(){
91
+ Object.keys(doc).forEach(function(e){
92
+ for(var l = i; l < levels.length; l++){
93
+ assert.equal(entities.decode("&" + e, l), doc[e]);
94
+ }
95
+ });
96
+ });
97
+ });
98
+
99
+ describe("Decode strict", function(){
100
+ it(levels[i], function(){
101
+ Object.keys(doc).forEach(function(e){
102
+ if(e.substr(-1) !== ";"){
103
+ assert.equal(entities.decodeStrict("&" + e, i), "&" + e);
104
+ return;
105
+ }
106
+ for(var l = i; l < levels.length; l++){
107
+ assert.equal(entities.decodeStrict("&" + e, l), doc[e]);
108
+ }
109
+ });
110
+ });
111
+ });
112
+
113
+ describe("Encode", function(){
114
+ it(levels[i], function(){
115
+ Object.keys(doc).forEach(function(e){
116
+ if(e.substr(-1) !== ";") return;
117
+ for(var l = i; l < levels.length; l++){
118
+ assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
119
+ }
120
+ });
121
+ });
122
+ });
123
+ });
124
+ });
125
+
126
+ var astral = {
127
+ "1D306": "\uD834\uDF06",
128
+ "1D11E": "\uD834\uDD1E"
129
+ };
130
+
131
+ describe("Astral entities", function(){
132
+ Object.keys(astral).forEach(function(c){
133
+ /*it("should decode " + astral[c], function(){
134
+ assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
135
+ });*/
136
+ it("should encode " + astral[c], function(){
137
+ assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
138
+ });
139
+ });
140
+ });