summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--marigold.lua16
-rwxr-xr-xtest.lua17
2 files changed, 29 insertions, 4 deletions
diff --git a/marigold.lua b/marigold.lua
index 02c9bbe..c01eecd 100644
--- a/marigold.lua
+++ b/marigold.lua
@@ -121,11 +121,25 @@ marigold.html = function(tbl, indent_level)
indent, close)
end
-marigold.percentDecode = function(str)
+marigold.decode_percent = function(str)
return string.gsub(str, "%%(%x%x)", function(digits)
return string.char(tonumber(digits, 16))
end)
end
+marigold.decode_query = function(str)
+ local tbl = {}
+ local cleanString = function(str)
+ return marigold.decode_percent(
+ string.gsub(str, "+", " ")
+ )
+ end
+
+ for k, v in string.gmatch(str, "([^=]-)=([^&]*)&?") do
+ tbl[cleanString(k)] = cleanString(v)
+ end
+
+ return tbl
+end
return marigold
diff --git a/test.lua b/test.lua
index adc166e..e359c7e 100755
--- a/test.lua
+++ b/test.lua
@@ -164,8 +164,19 @@ test("marigold.html correctly renders children", function()
end)
-test("marigold.percentDecode correctly decodes percent-encoded strings", function()
+test("marigold.decode_percent correctly decodes percent-encoded strings", function()
local str = '%20%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D'
- assert(marigold.percentDecode(str) == " !#$%&'()*+,/:;=?@[]")
- assert(marigold.percentDecode('%25+%2b') == '%++')
+ assert(marigold.decode_percent(str) == " !#$%&'()*+,/:;=?@[]")
+ assert(marigold.decode_percent('%25+%2b') == '%++')
+end)
+
+
+test("marigold.decode_query correctly obtains data", function()
+ local tbl
+ tbl = marigold.decode_query('')
+ tbl = marigold.decode_query('name=john')
+ assert(tbl.name == 'john')
+ tbl = marigold.decode_query('string=hello%2C+world%21&name=jane')
+ assert(tbl.string == 'hello, world!')
+ assert(tbl.name == 'jane')
end)