diff --git a/lib/comicxml.lua b/lib/comicxml.lua index 6f478ec..a9f5581 100644 --- a/lib/comicxml.lua +++ b/lib/comicxml.lua @@ -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 diff --git a/test/xml_spec.lua b/test/xml_spec.lua index 0b9c3df..4f622ff 100644 --- a/test/xml_spec.lua +++ b/test/xml_spec.lua @@ -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 = [[ + + + HelloWorld + + ]] + + local parser = XmlObject:new() + local root = parser:parse(xml) + local obj = parser:toTable(root) + + it(" 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)