+end
+
+local content = map(filters, list_filter)
+
+return
+
+
+
+ {content}
+
+
\ No newline at end of file
diff --git a/examples/web-component/server.lua b/examples/web-component/server.lua
index 9407c59..b68348e 100644
--- a/examples/web-component/server.lua
+++ b/examples/web-component/server.lua
@@ -60,14 +60,6 @@ local function get_headers(client, headers)
end
end
--- local function sanitize_text(str)
--- return (str:gsub("[<>&]", {
--- ["<"] = "<",
--- [">"] = ">",
--- ["&"] = "&"
--- }))
--- end
-
local function render(client, status_code, body, custom_headers)
local header_string = "HTTP/1.1 " .. status_code .. "\r\n"
local headers = {
@@ -97,6 +89,7 @@ local function handler(client, request)
local parsed_url = url.parse(path)
if parsed_url.path == "/" then
+ -- printTable(app)
local html = h(app)
render(client, "200 OK", html)
elseif mimes[parsed_url.path:lower():match("[^.]*$")] and method == "GET" then
diff --git a/h.lua b/h.lua
index a666980..9a02a3d 100644
--- a/h.lua
+++ b/h.lua
@@ -61,9 +61,25 @@ setmetatable(_G, {
end
})
+local function sanitize_value(str)
+ return (str:gsub("[<>&]", {
+ ["<"] = "<",
+ [">"] = ">",
+ ["&"] = "&"
+ }))
+end
+
local function h(element)
if type(element) ~= "table" then return element or "" end
local tkeys = {}
+ -- asume as nodeList
+ if type(element.atts) ~= "table" then
+ local node_list = {}
+ for _, k in ipairs(element) do
+ table.insert(node_list, h(k) or "")
+ end
+ return table.concat(node_list)
+ end
for k in pairs(element.atts) do table.insert(tkeys, k) end
if #tkeys then table.sort(tkeys) end
local atts = ""
@@ -72,7 +88,7 @@ local function h(element)
local v = element.atts[k]
if type(v) ~= "table" then
if k ~= "children" then
- atts = atts .. " " .. k .. "=\"" .. v .. "\""
+ atts = atts .. " " .. k .. "=\"" .. sanitize_value(v) .. "\""
else
children = v
end
@@ -81,8 +97,10 @@ local function h(element)
for _, child in ipairs(element.children) do
if type(child) == "table" then
children = children .. h(child)
- else
+ elseif element.tag == "script" then
children = children .. child
+ else
+ children = children .. sanitize_value(child)
end
end
if element.tag:lower() == "doctype" then
diff --git a/test/17_table.luax b/test/17_table.luax
index 3b01b11..33d2d06 100644
--- a/test/17_table.luax
+++ b/test/17_table.luax
@@ -10,8 +10,8 @@ local function map(a, fcn)
end
function module.Filter(filters)
- local content = table.concat(map(filters, function(filter)
- return h(
+ local content = map(filters, function(filter)
+ return
{content}
diff --git a/test/18_filter.luax b/test/18_filter.luax
index 9b06639..0ba08a7 100644
--- a/test/18_filter.luax
+++ b/test/18_filter.luax
@@ -1,14 +1,6 @@
local h = require('luax')
-local function map(a, fcn)
- local b = {}
- for _, v in ipairs(a) do
- table.insert(b, fcn(v))
- end
- return b
-end
-
local function fcn(filter)
return
end
diff --git a/test/test_spec.lua b/test/test_spec.lua
index 94ce415..1d9a926 100644
--- a/test/test_spec.lua
+++ b/test/test_spec.lua
@@ -95,7 +95,7 @@ describe("LuaX", function()
it("should return a HTML string when given input node", function()
local el = require("test.09_input2")
assert.is.equal(
- '',
+ '',
h(el))
end)
@@ -115,7 +115,7 @@ describe("LuaX", function()
it("should return a HTML string with deep node tree", function()
local el = require("test.12_test")
assert.is.equal(
- [[
todo A
todo A Value
]],
+ [[
todo A
todo A Value
]],
h(el))
end)
@@ -137,16 +137,12 @@ describe("LuaX", function()
it("should return a HTML string when given XML like syntax with table concat", function()
local el = require("test.17_table")
- assert.is.equal([[
]], h(el.Filter(filters)))
end)
it("should return a HTML string when given XML like syntax with table concat", function()
local el = require("test.18_filter")
- assert.is.equal([[