[Pkg-javascript-commits] [node-shell-quote] 106/137: Fix quote() with special chars

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 19:19:43 UTC 2017


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

rouca pushed a commit to branch master
in repository node-shell-quote.

commit 811b5a0aff79f347db245edcf88750977c111844
Author: Daniel Beardsley <daniel at ifixit.com>
Date:   Wed Dec 10 12:30:18 2014 -0700

    Fix quote() with special chars
    
    Add some failing tests and fix them
    
    I noticed that 'echo ' + quote(["'#"]) => echo "\'\#"
    which produces:
    \'\#
    
    which is wrong.
    
    We are escaping lots of things inside double-quotes that we don't need
    to, which ends up including the \ before each escaped char.
---
 index.js      | 4 ++--
 test/quote.js | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/index.js b/index.js
index 6475645..a917871 100644
--- a/index.js
+++ b/index.js
@@ -12,10 +12,10 @@ exports.quote = function (xs) {
             return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
         }
         else if (/["'\s]/.test(s)) {
-            return '"' + s.replace(/(["\\$`(){}!#&*|])/g, '\\$1') + '"';
+            return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
         }
         else {
-            return String(s).replace(/([\\$`(){}!#&*|])/g, '\\$1');
+            return String(s).replace(/([\\$`()!#&*|])/g, '\\$1');
         }
     }).join(' ');
 };
diff --git a/test/quote.js b/test/quote.js
index 2a9c1b0..8f9c1b8 100644
--- a/test/quote.js
+++ b/test/quote.js
@@ -12,6 +12,11 @@ test('quote', function (t) {
         '\\$ \\` "\'"'
     );
     t.equal(quote([]), '');
+    t.equal(quote(["a\nb"]), "'a\nb'");
+    t.equal(quote([' #(){}*|][!']), "' #(){}*|][!'");
+    t.equal(quote(["'#(){}*|][!"]), '"\'#(){}*|][\\!"');
+    t.equal(quote(["X#(){}*|][!"]), "X\\#\\(\\){}\\*\\|][\\!");
+    t.equal(quote(["a\n#\nb"]), "'a\n#\nb'");
     t.equal(quote([ 'a', 1, true, false ]), 'a 1 true false');
     t.equal(quote([ 'a', 1, null, undefined ]), 'a 1 null undefined');
     t.end();

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



More information about the Pkg-javascript-commits mailing list