[Pkg-javascript-commits] [ltx] 434/469: Speedup xml escaping and unescaping
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:03:35 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 ec3dbef6ebe46e0d215bf05610b550445b6f18e8
Author: Matt Lavin <matt.lavin at inin.com>
Date: Wed Mar 30 09:37:50 2016 -0400
Speedup xml escaping and unescaping
---
lib/escape.js | 51 +++++++++++++++++++++++++++++++++------------------
1 file changed, 33 insertions(+), 18 deletions(-)
diff --git a/lib/escape.js b/lib/escape.js
index 7407a26..2143916 100644
--- a/lib/escape.js
+++ b/lib/escape.js
@@ -1,33 +1,48 @@
'use strict'
+var escapeXMLTable = {
+ '&': '&',
+ '<': '<',
+ '>': '>',
+ '"': '"',
+ '\'': '''
+}
+
+function escapeXMLReplace (match) {
+ return escapeXMLTable[match]
+}
+
+var unescapeXMLTable = {
+ '&': '&',
+ '&': '&',
+ '<': '<',
+ '<': '<',
+ '>': '>',
+ '>': '>',
+ '"': '"',
+ '"': '"',
+ ''': "'",
+ ''': "'"
+}
+
+function unescapeXMLReplace (match) {
+ return unescapeXMLTable[match]
+}
+
exports.escapeXML = function escapeXML (s) {
- return s
- .replace(/\&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/"/g, '"')
- .replace(/'/g, ''')
+ return s.replace(/\&|<|>|"|'/g, escapeXMLReplace)
}
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, "'")
+ .replace(/\&(amp|#38|lt|#60|gt|#62|quot|#34|apos|#39);/g, unescapeXMLReplace)
}
exports.escapeXMLText = function escapeXMLText (s) {
- return s
- .replace(/\&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
+ return s.replace(/\&|<|>/g, escapeXMLReplace)
}
exports.unescapeXMLText = function unescapeXMLText (s) {
return s
- .replace(/\&(amp|#38);/g, '&')
- .replace(/\&(lt|#60);/g, '<')
- .replace(/\&(gt|#62);/g, '>')
+ .replace(/\&(amp|#38|lt|#60|gt|#62);/g, unescapeXMLReplace)
}
--
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