Skip to content

Commit

Permalink
added self closing tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Oct 3, 2024
1 parent bffd3aa commit e16e644
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ 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
22 changes: 21 additions & 1 deletion h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@

-- print(h(myElement))

local voidTags = {
"area", "base", "basefont", "br", "col",
"frame", "hr", "img", "input", "link",
"meta", "param", "embed", "command", "keygen",
"source", "track", "wbr"
}

local function isVoidTag(tag)
for _, voidTag in ipairs(voidTags) do
if voidTag == tag then
return true
end
end
return false
end

local function createElement(tag, atts, children)
return {
tag = tag,
Expand Down Expand Up @@ -69,7 +85,11 @@ local function h(element)
children = children .. child
end
end
return "<" .. element.tag .. atts .. ">" .. children .. "</" .. element.tag .. ">"
if isVoidTag(element.tag) then
return "<" .. element.tag .. atts .. ">"
else
return "<" .. element.tag .. atts .. ">" .. children .. "</" .. element.tag .. ">"
end
end

return h
6 changes: 3 additions & 3 deletions luax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ local function decentParserAST(input)

if isTag ~= 0 then
-- add bracket to all attributes key
if input:match("%=") then
output = output:gsub('([%w%-_]+)%=(".-")', '["%1"]=%2')
end
if isTextNode == 1 and char == "{" or char == "}" then
skip = true
isTextNode = 3
Expand All @@ -91,6 +88,9 @@ local function decentParserAST(input)
if skip == false then
output = output .. char
end
if char:match("%=") then
output = output:gsub('([%w%-_]+)%=', '["%1"]=')
end
pos = pos + 1
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/test_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ local node_value = require('test.2_node_value')

print(h(node_value))

-- local comment = require('test.3_comment')

-- print(h(comment))

local element = require('test.element')

print(h(element))
Expand All @@ -47,3 +43,7 @@ print(h(foo))
local content = require('test.content')

print(h(content))

local input = require('test.input')

print(h(input))

0 comments on commit e16e644

Please sign in to comment.