Added missing value escaping
[project/luci.git] / libs / cbi / luasrc / cbi.lua
index 3bb56f36cac517b5a0f93975143be38fc2875d2b..7bcfceb2ef4794ed1dfa2695be7da357db9155ba 100644 (file)
@@ -246,6 +246,15 @@ function Map.stateget(self, section, option)
 end
 
 
+--[[
+Page - A simple node
+]]--
+
+Page = class(Node)
+Page.__init__ = Node.__init__
+Page.parse    = function() end
+
+
 --[[
 SimpleForm - A Simple non-UCI form
 ]]--
@@ -260,13 +269,17 @@ function SimpleForm.__init__(self, config, title, description, data)
 end
 
 function SimpleForm.parse(self, ...)
-       Node.parse(self, 1, ...)
+       if luci.http.formvalue("cbi.submit") then
+               Node.parse(self, 1, ...)
+       end
                
        local valid = true
-       for i, v in ipairs(self.children) do
-               valid = valid 
-                and (not v.tag_missing or not v.tag_missing[1])
-                and (not v.tag_invalid or not v.tag_invalid[1])
+       for k, j in ipairs(self.children) do 
+               for i, v in ipairs(j.children) do
+                       valid = valid 
+                        and (not v.tag_missing or not v.tag_missing[1])
+                        and (not v.tag_invalid or not v.tag_invalid[1])
+               end
        end
        
        local state = 
@@ -274,7 +287,7 @@ function SimpleForm.parse(self, ...)
                or valid and 1
                or -1
 
-       self.dorender = self:handle(state, self.data)
+       self.dorender = self:handle(state, self.data) ~= false
 end
 
 function SimpleForm.render(self, ...)
@@ -283,12 +296,33 @@ function SimpleForm.render(self, ...)
        end
 end
 
--- Creates a child section
+function SimpleForm.section(self, class, ...)
+       if instanceof(class, AbstractSection) then
+               local obj  = class(self, ...)
+               self:append(obj)
+               return obj
+       else
+               error("class must be a descendent of AbstractSection")
+       end
+end
+
+-- Creates a child field
 function SimpleForm.field(self, class, ...)
+       local section
+       for k, v in ipairs(self.children) do
+               if instanceof(v, SimpleSection) then
+                       section = v
+                       break
+               end
+       end
+       if not section then
+               section = self:section(SimpleSection)
+       end
+       
        if instanceof(class, AbstractValue) then
                local obj  = class(self, ...)
                obj.track_missing = true
-               self:append(obj)
+               section:append(obj)
                return obj
        else
                error("class must be a descendent of AbstractValue")
@@ -437,6 +471,38 @@ function AbstractSection.create(self, section)
 end
 
 
+SimpleSection = class(AbstractSection)
+
+function SimpleSection.__init__(self, form, ...)
+       AbstractSection.__init__(self, form, nil, ...)
+       self.template = "cbi/nullsection"
+end
+
+
+Table = class(AbstractSection)
+
+function Table.__init__(self, form, data, ...)
+       local datasource = {}
+       self.data = data
+       
+       function datasource.get(self, section, option)
+               return data[option]
+       end
+       
+       AbstractSection.__init__(self, datasource, nil, ...)
+end
+
+function Table.cfgsections(self)
+       local sections = {}
+       
+       for i, v in pairs(self.data) do
+               table.insert(sections, i)
+       end
+       
+       return sections
+end
+
+
 
 --[[
 NamedSection - A fixed configuration section defined by its name
@@ -665,7 +731,7 @@ function AbstractValue.parse(self, section)
        else                                                    -- Unset the UCI or error
                if self.rmempty or self.optional then
                        self:remove(section)
-               elseif self.track_missing and not fvalue or fvalue ~= cvalue then
+               elseif self.track_missing and (not fvalue or fvalue ~= cvalue) then
                        self.tag_missing[section] = true
                end
        end
@@ -684,10 +750,10 @@ function AbstractValue.render(self, s, scope)
                        if cond then
                                return string.format(
                                        ' %s="%s"', tostring(key),
-                                       tostring( val
+                                       luci.util.pcdata(tostring( val
                                         or scope[key]
                                         or (type(self[key]) ~= "function" and self[key])
-                                        or "" )
+                                        or "" ))
                                )
                        else
                                return ''