Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/comicxml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ function XmlObject:toTable(node)

-- Add text if present and no children or attributes
local text = node._text:match("^%s*(.-)%s*$")
if next(obj) == nil then
return text
end

if text ~= "" then
if next(obj) == nil then
return text
else
obj["text"] = text
end
obj["text"] = text
end

return obj
Expand Down
23 changes: 23 additions & 0 deletions test/xml_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,26 @@ describe("xml object with array", function()
end)
end)
end)

describe("empty xml object with self closing tags", function()
describe("parse sample", function()
local xml = [[
<root>
<item/>
<item2>HelloWorld</item2>
</root>
]]

local parser = XmlObject:new()
local root = parser:parse(xml)
local obj = parser:toTable(root)

it("<item/> should be null, no a table", function()
assert.equals("",obj.item)
assert.is_not.table(obj.item)
end)
it("item2 should exist and have value", function()
assert.equals( "HelloWorld", obj.item2)
end)
end)
end)