Simplify XMLNode data structure
This commit is contained in:
@@ -6,7 +6,6 @@ XMLNode.new = function(name)
|
||||
node.name = name
|
||||
node.children = {}
|
||||
node.attributes = {}
|
||||
node.computedAttributes = {}
|
||||
|
||||
function node:addChild(child)
|
||||
if self[child.name] ~= nil then
|
||||
@@ -23,22 +22,7 @@ XMLNode.new = function(name)
|
||||
end
|
||||
|
||||
function node:addAttribute(name, value)
|
||||
local lName = "@" .. name
|
||||
if self[lName] ~= nil then
|
||||
if type(self[lName]) == "string" then
|
||||
local tempTable = {}
|
||||
table.insert(tempTable, self[lName])
|
||||
self[lName] = tempTable
|
||||
end
|
||||
table.insert(self[lName], value)
|
||||
else
|
||||
self[lName] = value
|
||||
end
|
||||
table.insert(self.attributes, { name = name, value = self[lName] })
|
||||
end
|
||||
|
||||
function node:addComputedAttribute(name, value)
|
||||
self.computedAttributes[name] = value
|
||||
self.attributes[name] = value
|
||||
end
|
||||
|
||||
return node
|
||||
@@ -98,14 +82,13 @@ function XMLParser:FromXmlString(value)
|
||||
end
|
||||
|
||||
function XMLParser:ParseAttributes(node, s)
|
||||
string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
|
||||
node:addAttribute(w, self:FromXmlString(a))
|
||||
-- Parse "" style attributes
|
||||
local _, _ = string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
|
||||
node:addAttribute(w, "\"" .. self:FromXmlString(a) .. "\"")
|
||||
end)
|
||||
end
|
||||
|
||||
function XMLParser:ParseComputedAttributes(node, s)
|
||||
string.gsub(s, "(%w+)={(.-)}", function(w, a)
|
||||
node:addComputedAttribute(w, a)
|
||||
-- Parse {} style attributes
|
||||
local _, _ = string.gsub(s, "(%w+)={(.-)}", function(w, a)
|
||||
node:addAttribute(w, a)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -126,12 +109,10 @@ function XMLParser:ParseXmlText(xmlText)
|
||||
if empty == "/" then -- empty element tag
|
||||
local lNode = XMLNode.new(label)
|
||||
self:ParseAttributes(lNode, xarg)
|
||||
self:ParseComputedAttributes(lNode, xarg)
|
||||
top:addChild(lNode)
|
||||
elseif c == "" then -- start tag
|
||||
local lNode = XMLNode.new(label)
|
||||
self:ParseAttributes(lNode, xarg)
|
||||
self:ParseComputedAttributes(lNode, xarg)
|
||||
table.insert(stack, lNode)
|
||||
top = lNode
|
||||
else -- end tag
|
||||
|
||||
@@ -27,7 +27,7 @@ end
|
||||
|
||||
local function registerFunctionEvents(self, data, events, renderContext)
|
||||
for _, event in pairs(events) do
|
||||
local expression = data.computedAttributes[event]
|
||||
local expression = data.attributes[event]
|
||||
if (expression ~= nil) then
|
||||
registerFunctionEvent(self, expression .. "()", self[event], renderContext)
|
||||
end
|
||||
@@ -116,16 +116,13 @@ return {
|
||||
local object = {
|
||||
setValuesByXMLData = function(self, data, renderContext)
|
||||
renderContext.env[self:getName()] = self
|
||||
for prop, expression in pairs(data.computedAttributes) do
|
||||
for attribute, expression in pairs(data.attributes) do
|
||||
local update = function()
|
||||
local value = load("return " .. expression, nil, "t", renderContext.env)()
|
||||
self:setProperty(prop, value)
|
||||
self:setProperty(attribute, value)
|
||||
end
|
||||
basalt.effect(update)
|
||||
end
|
||||
for _, prop in ipairs(data.attributes) do
|
||||
self:setProperty(prop.name, prop.value)
|
||||
end
|
||||
registerFunctionEvents(self, data, {
|
||||
"onClick",
|
||||
"onClickUp",
|
||||
@@ -185,16 +182,13 @@ return {
|
||||
end
|
||||
|
||||
local function insertChildLayout(self, layout, node, renderContext)
|
||||
local props = {}
|
||||
for _, prop in ipairs(node.attributes) do
|
||||
props[prop.name] = prop.value
|
||||
end
|
||||
local updateFns = {}
|
||||
for prop, expression in pairs(node.computedAttributes) do
|
||||
for prop, expression in pairs(node.attributes) do
|
||||
updateFns[prop] = basalt.derived(function()
|
||||
return load("return " .. expression, nil, "t", renderContext.env)()
|
||||
end)
|
||||
end
|
||||
local props = {}
|
||||
setmetatable(props, {
|
||||
__index = function(_, k)
|
||||
return updateFns[k]()
|
||||
|
||||
Reference in New Issue
Block a user