[Pkg-javascript-commits] [ltx] 420/469: fix escaping

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:33 UTC 2016


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

js pushed a commit to branch master
in repository ltx.

commit 052348cde7fa76fc796674b5c6db9782a9ac59d4
Author: Sonny Piers <sonny at fastmail.net>
Date:   Sat Jan 9 04:30:24 2016 +0100

    fix escaping
---
 index.js            |  2 ++
 lib/escape.js       | 16 +++++++----
 test/escape-test.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 5 deletions(-)

diff --git a/index.js b/index.js
index 011bd7f..fc2b8f6 100644
--- a/index.js
+++ b/index.js
@@ -17,7 +17,9 @@ exports.childrenEqual = equal.children
 exports.createElement = createElement
 
 exports.escapeXML = escape.escapeXML
+exports.unescapeXML = escape.unescapeXML
 exports.escapeXMLText = escape.escapeXMLText
+exports.unescapeXMLText = escape.unescapeXMLText
 
 exports.Parser = Parser
 exports.parse = parse
diff --git a/lib/escape.js b/lib/escape.js
index 7fe2165..7407a26 100644
--- a/lib/escape.js
+++ b/lib/escape.js
@@ -6,7 +6,16 @@ exports.escapeXML = function escapeXML (s) {
     .replace(/</g, '<')
     .replace(/>/g, '>')
     .replace(/"/g, '"')
-    .replace(/"/g, ''')
+    .replace(/'/g, ''')
+}
+
+exports.unescapeXML = function unescapeXML (s) {
+  return s
+    .replace(/\&(amp|#38);/g, '&')
+    .replace(/\&(lt|#60);/g, '<')
+    .replace(/\&(gt|#62);/g, '>')
+    .replace(/\&(quot|#34);/g, '"')
+    .replace(/\&(apos|#39);/g, "'")
 }
 
 exports.escapeXMLText = function escapeXMLText (s) {
@@ -16,12 +25,9 @@ exports.escapeXMLText = function escapeXMLText (s) {
     .replace(/>/g, '>')
 }
 
-exports.unescapeXML = function unescapeXML (s) {
+exports.unescapeXMLText = function unescapeXMLText (s) {
   return s
     .replace(/\&(amp|#38);/g, '&')
     .replace(/\&(lt|#60);/g, '<')
     .replace(/\&(gt|#62);/g, '>')
-    .replace(/\&(quot|#34);/g, '"')
-    .replace(/\&(apos|#39);/g, "'")
-    .replace(/\&(nbsp|#160);/g, '\n')
 }
diff --git a/test/escape-test.js b/test/escape-test.js
new file mode 100644
index 0000000..51f0c14
--- /dev/null
+++ b/test/escape-test.js
@@ -0,0 +1,81 @@
+'use strict'
+
+var vows = require('vows')
+var assert = require('assert')
+var ltx = require('..')
+var escape = require('../lib/escape')
+var escapeXML = escape.escapeXML
+var unescapeXML = escape.unescapeXML
+var escapeXMLText = escape.escapeXMLText
+var unescapeXMLText = escape.unescapeXMLText
+
+vows.describe('escape').addBatch({
+  'escapeXML': {
+    'exported correctly': function () {
+      assert.equal(ltx.escapeXML, escapeXML)
+    },
+    'escapes &': function () {
+      assert.equal(escapeXML('&'), '&')
+    },
+    'escapes <': function () {
+      assert.equal(escapeXML('<'), '<')
+    },
+    'escapes >': function () {
+      assert.equal(escapeXML('>'), '>')
+    },
+    'escapes "': function () {
+      assert.equal(escapeXML('"'), '"')
+    },
+    'escapes \'': function () {
+      assert.equal(escapeXML('\''), ''')
+    }
+  },
+  'unescapeXML': {
+    'exported correctly': function () {
+      assert.equal(ltx.unescapeXML, unescapeXML)
+    },
+    'unescapes &': function () {
+      assert.equal(unescapeXML('&'), '&')
+    },
+    'unescapes <': function () {
+      assert.equal(unescapeXML('<'), '<')
+    },
+    'unescapes >': function () {
+      assert.equal(unescapeXML('>'), '>')
+    },
+    'unescapes "': function () {
+      assert.equal(unescapeXML('"'), '"')
+    },
+    'unescapes \'': function () {
+      assert.equal(unescapeXML('''), '\'')
+    }
+  },
+  'escapeXMLText': {
+    'exported correctly': function () {
+      assert.equal(ltx.escapeXMLText, escapeXMLText)
+    },
+    'escapes &': function () {
+      assert.equal(escapeXMLText('&'), '&')
+    },
+    'escapes <': function () {
+      assert.equal(escapeXMLText('<'), '<')
+    },
+    'escapes >': function () {
+      assert.equal(escapeXMLText('>'), '>')
+    }
+  },
+  'unescapeXMLText': {
+    'exported correctly': function () {
+      assert.equal(ltx.unescapeXMLText, unescapeXMLText)
+    },
+    'unescapes &': function () {
+      assert.equal(unescapeXMLText('&'), '&')
+    },
+    'unescapes <': function () {
+      assert.equal(unescapeXMLText('<'), '<')
+    },
+    'unescapes >': function () {
+      assert.equal(unescapeXMLText('>'), '>')
+    }
+  }
+}).export(module)

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



More information about the Pkg-javascript-commits mailing list