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