luci-base: dispatcher: support raw values in attr() and ifattr()
authorJo-Philipp Wich <jo@mein.io>
Tue, 12 Feb 2019 07:28:21 +0000 (08:28 +0100)
committerJo-Philipp Wich <jo@mein.io>
Tue, 12 Feb 2019 07:34:01 +0000 (08:34 +0100)
Extend the attr() and ifattr() template functions to take an optional
further parameter indicating that the passed value should not be escaped.

This is needed for cases where the input already is escaped through
other means, e.g. when the value was previously filtered through the
striptags() template helper.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit 4141243762aafb7960d67f871c97907307005f87)

modules/luci-base/luasrc/dispatcher.lua

index 6cf2712eb4a862c8771ce68d7d34fe966e9ce68e..80b715906d5e15688a13321de7bec91f7cec202e 100644 (file)
@@ -315,7 +315,7 @@ function dispatch(request)
                        assert(media, "No valid theme found")
                end
 
-               local function _ifattr(cond, key, val)
+               local function _ifattr(cond, key, val, noescape)
                        if cond then
                                local env = getfenv(3)
                                local scope = (type(env.self) == "table") and env.self
@@ -326,13 +326,16 @@ function dispatch(request)
                                                val = util.serialize_json(val)
                                        end
                                end
-                               return string.format(
-                                       ' %s="%s"', tostring(key),
-                                       util.pcdata(tostring( val
-                                        or (type(env[key]) ~= "function" and env[key])
-                                        or (scope and type(scope[key]) ~= "function" and scope[key])
-                                        or "" ))
-                               )
+
+                               val = tostring(val or
+                                       (type(env[key]) ~= "function" and env[key]) or
+                                       (scope and type(scope[key]) ~= "function" and scope[key]) or "")
+
+                               if noescape ~= true then
+                                       val = util.pcdata(val)
+                               end
+
+                               return string.format(' %s="%s"', tostring(key), val)
                        else
                                return ''
                        end