From 2770f736e3dc25cde8c80c9a7b5806e65dc19674 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 16 Mar 2013 13:52:23 -0400 Subject: [PATCH] Improve escapes with a `bs.js` library I resisted this, but it makes BS more useful, so what can you do? --- BullScript.JSON-tmLanguage | 4 ++-- bs.js | 26 ++++++++++++++++++++++++++ bsc | 13 ++++--------- tests | 21 +++++++++++---------- 4 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 bs.js diff --git a/BullScript.JSON-tmLanguage b/BullScript.JSON-tmLanguage index eb60bfd..8b83ef8 100644 --- a/BullScript.JSON-tmLanguage +++ b/BullScript.JSON-tmLanguage @@ -8,7 +8,7 @@ "0": { "name": "string.quoted.double.js" } }, "patterns": [ - { "begin": "#[nxX]?{", + { "begin": "#[nNxX]?{", "end": "}", "patterns": [ { "include": "$self" } @@ -28,7 +28,7 @@ "1": { "name": "entity.name.tag.html" } }, "patterns": [ - { "begin": "#[nxX]?{", + { "begin": "#[nNxX]?{", "end": "}", "patterns": [ { "include": "$self" } diff --git a/bs.js b/bs.js new file mode 100644 index 0000000..a6951d1 --- /dev/null +++ b/bs.js @@ -0,0 +1,26 @@ +(function() { + bs = { + x: function(y) { + if (y === null || y === undefined) + return ''; + return encodeURIComponent(y).replace('%20', '+'); + }, + X: function(y) { + if (y === null || y === undefined) + return ''; + return encodeURIComponent(y).replace(/[!'()]/g, escape).replace(/\*/g, '%2A'); + }, + n: function(y) { + if (y === null || y === undefined) + return ''; + return y; + }, + N: function(y) { + if (y instanceof Array && y.length <= 0) + return ''; + if (y instanceof Object && Object.keys(y).length <= 0) + return ''; + return y || ''; + } + }; +})(); diff --git a/bsc b/bsc index 0946c60..c09b2ce 100755 --- a/bsc +++ b/bsc @@ -141,16 +141,11 @@ end class String def bsubst sep = '"' - self.gsub(/#([nxX]?)\{(.*?)\}/) do - inner = case $1 - when 'n' - "((#{$2}) || '')" - when 'X' - "encodeURIComponent(#{$2}).replace(/[!'()]/g, escape).replace(/\\*/g, '%2A')" - when 'x' - "encodeURIComponent(#{$2}).replace('%20', '+')" + self.gsub(/#([nNxX]?)\{(.*?)\}/) do + inner = if $1 and $1.length > 0 + "bs.#$1(#$2)" else - "(#{$2})" + "(#$2)" end %Q{#{sep} + #{inner} + #{sep}} end diff --git a/tests b/tests index 88c8841..da8ea54 100755 --- a/tests +++ b/tests @@ -62,11 +62,11 @@ class BullTests < Test::Unit::TestCase end def test_falsy_subst - assert_equal %q{"foo #n{bar} foo"}.to_js, %q{"foo " + ((bar) || '') + " foo"} + assert_equal %q{"foo #n{bar} foo"}.to_js, %q{"foo " + bs.n(bar) + " foo"} end def test_URI_encoded_subst - assert_equal %q{"foo #x{bar} foo"}.to_js, %q{"foo " + encodeURIComponent(bar).replace('%20', '+') + " foo"} + assert_equal %q{"foo #x{bar} foo"}.to_js, %q{"foo " + bs.x(bar) + " foo"} end def test_html1 @@ -197,15 +197,16 @@ class BullTests < Test::Unit::TestCase def test_multiline_with_internal_quotes input = <<-end.to_js - """foo - bar \#{jee || ''} haha - mooface - """ + """foo + bar \#{jee || ''} haha + mooface + """ + end output = <<-end.to_js - 'foo\n'+ - 'bar ' + (jee || '') + ' haha\n'+ - 'mooface\n'+ - '' + 'foo\\n'+ + 'bar ' + (jee || '') + ' haha\\n'+ + 'mooface\\n'+ + '' end assert_equal output, input end