[Pkg-javascript-commits] [node-entities] 17/63: Only invoke String.replace() once during decode. (fixes gh #8)

Wolfgang Borgert debacle at moszumanska.debian.org
Mon Sep 22 08:15:45 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository node-entities.

commit ab41f93ef43bdaa3e7d2683a86d02b26270eba4e
Author: C. Scott Ananian <cscott at cscott.net>
Date:   Thu Apr 18 19:00:21 2013 -0400

    Only invoke String.replace() once during decode. (fixes gh #8)
---
 index.js     | 14 ++++++++++----
 test/test.js | 29 ++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/index.js b/index.js
index 9f09edb..b2a74e9 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
-var re_hex = /&#x[\da-f]+;?/gi,
+var re_hex = /&#[xX][\da-fA-F]+;?/g,
 	re_strictHex = /&#x[\da-f]+;/gi,
 	re_charCode = /&#\d+;?/g,
 	re_strictCharCode = /&#\d+;/g,
@@ -16,9 +16,17 @@ var fetch = function(filename, inherits){
 	if(inherits) for(var name in inherits) obj[name] = inherits[name];
 	
 	var re = Object.keys(obj).sort().join("|").replace(/(\w+)\|\1;/g, "$1;?");
+	// add regex for hex and char codes
+	re += '|' + re_hex.source.substr(1) + '|' + re_charCode.source.substr(1);
 
 	return {
 		func: function(name){
+			if (name.charAt(1) === '#') {
+				if (name.charAt(2).toLowerCase() === 'x') {
+					return hex_func(name);
+				}
+				return num_func(name);
+			}
 			return obj[name.substr(1)];
 		},
 		re: new RegExp("&(?:" +re +")", "g"),
@@ -62,9 +70,7 @@ modes.forEach(function(name){
 	
 	module.exports["decode" +name] = function(data){
 		return data
-			.replace(regex, func)
-			.replace(re_hex, hex_func)
-			.replace(re_charCode, num_func);
+			.replace(regex, func);
 	};
 	
 	var reverse = getReverse(obj.obj),
diff --git a/test/test.js b/test/test.js
index f3b0bd9..c73d0ed 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,7 +1,7 @@
 var assert = require('assert');
 var entities = require('../');
 
-describe("Encode/decode test", function() {
+describe("Encode->decode test", function() {
     var testcases = [
         { input: "asdf & ÿ ü '",
           xml: "asdf & ÿ ü '",
@@ -36,3 +36,30 @@ describe("Encode/decode test", function() {
         });
     });
 });
+
+describe("Decode test", function() {
+    var testcases = [
+	{ input: '&amp;',  output: '&' },
+	{ input: '&#38;',  output: '&' },
+	{ input: '&#x26;', output: '&#x26;' },
+	{ input: '&#X26;', output: '&#X26;' },
+	{ input: '&#38;',  output: '&' },
+	{ input: '&#x26;#38;', output: '&' },
+	{ input: '&#X26;#38;', output: '&' },
+	{ input: '&#x3a;',     output: ':' },
+	{ input: '&#x3A;',     output: ':' },
+	{ input: '&#X3a;',     output: ':' },
+	{ input: '&#X3A;',     output: ':' }
+    ];
+    testcases.forEach(function(tc) {
+        it('should XML decode '+tc.input, function() {
+            assert.equal(entities.decodeXML(tc.input), tc.output);
+        });
+        it('should HTML4 decode '+tc.input, function() {
+            assert.equal(entities.decodeHTML4(tc.input), tc.output);
+        });
+        it('should HTML5 decode '+tc.input, function() {
+            assert.equal(entities.decodeHTML5(tc.input), tc.output);
+        });
+    });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-entities.git



More information about the Pkg-javascript-commits mailing list