Skip to content

Commit

Permalink
handle break line
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Oct 4, 2024
1 parent 1bc66a0 commit fead59f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,3 @@ You'll get,
```html
<div class="container" id="hello">Hello, world!</div>
```

### Caveats
What are limited now
- only can process single line jsx, not multi line
16 changes: 16 additions & 0 deletions h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@

-- print(h(myElement))

function printTable(t, indent)
indent = indent or 0
local tab = string.rep(" ", indent)

for key, value in pairs(t) do
if type(value) == "table" then
print(tab .. tostring(key) .. ": ")
printTable(value, indent + 1)
else
print(tab .. tostring(key) .. ": " .. tostring(value))
end
end
end



local voidTags = {
"area", "base", "basefont", "br", "col",
"frame", "hr", "img", "input", "link",
Expand Down
6 changes: 4 additions & 2 deletions luax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local function decentParserAST(input)
end
if isTextNode == 2 then
isTextNode = 0
output = output .. "\")"
output = output .. "]])"
else
output = output .. ")"
end
Expand Down Expand Up @@ -81,7 +81,9 @@ local function decentParserAST(input)
isTextNode = 3
elseif isTextNode == 1 then
isTextNode = 2
output = output .. "\""
if char ~= '\n' then
output = output .. "[["
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/input.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local todo = { id = "0", title = "foo" }
return <input class="edit" name="title" value={todo.title or ' '} todo-id={todo.id} _="install TodoEdit"/>
2 changes: 2 additions & 0 deletions test/input_with_con.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local todo = { editing = false, title = "foo", id = "0" }
return <input class="edit" name="title" value={todo.editing and todo.title or nil} todo-id={todo.id} _="install TodoEdit"/>
3 changes: 3 additions & 0 deletions test/line_break.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return <div>
<p color="red">foobar!</p>
</div>
8 changes: 8 additions & 0 deletions test/test_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ print(h(content))
local input = require('test.input')

print(h(input))

local input_with_con = require('test.input_with_con')

print(h(input_with_con))

local linebreak = require('test.line_break')

print(h(linebreak))
14 changes: 14 additions & 0 deletions test/test_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ describe("LuaX", function()
'<input _="install TodoEdit" class="edit" name="title" todo-id="0" value="foo">',
h(el))
end)

it("should return a HTML string when have conditional statement", function()
local el = require("test.input_with_con")
assert.is.equal(
'<input _="install TodoEdit" class="edit" name="title" todo-id="0">',
h(el))
end)

it("should return a HTML string with multi breakline", function()
local el = require("test.line_break")
assert.is.equal(
'<div><p color="red">foobar!</p></div>',
h(el))
end)
end)

0 comments on commit fead59f

Please sign in to comment.