webmcp

changeset 9:64f4540ce88c

CDATA-escaping with JavaScript comments for ui.script
author jbe/bsw
date Sun Jan 31 18:37:38 2010 +0100 (2010-01-31)
parents f02e14d1e69e
children e017c47d43b5
files framework/env/ui/script.lua
line diff
     1.1 --- a/framework/env/ui/script.lua	Sun Jan 31 18:36:35 2010 +0100
     1.2 +++ b/framework/env/ui/script.lua	Sun Jan 31 18:37:38 2010 +0100
     1.3 @@ -7,12 +7,10 @@
     1.4    script        = script,         -- string or function for script content
     1.5  }
     1.6  
     1.7 -This function is used to insert a script into the active slot. It is currently not XML compliant and the script must not contain a closing script tag.
     1.8 +This function is used to insert a script into the active slot.
     1.9  
    1.10  --]]--
    1.11  
    1.12 --- TODO: CDATA or SGML comment?
    1.13 -
    1.14  function ui.script(args)
    1.15    local args = args or {}
    1.16    local noscript_attr = args.noscript_attr
    1.17 @@ -20,12 +18,6 @@
    1.18    local attr = table.new(args.attr)
    1.19    attr.type = attr.type or args.type or "text/javascript"
    1.20    local script = args.script
    1.21 -  if script and type(script) ~= "function" then
    1.22 -    -- disable HTML entity escaping
    1.23 -    script = function()
    1.24 -      slot.put(args.script)
    1.25 -    end
    1.26 -  end
    1.27    if args.external then
    1.28      attr.src = encode.url{ external = args.external }
    1.29    elseif args.static then
    1.30 @@ -37,6 +29,21 @@
    1.31    if attr.src then
    1.32      ui.tag{ tag = "script", attr = attr, content = "" }
    1.33    elseif script then
    1.34 -    ui.tag{ tag = "script", attr = attr, content = script }
    1.35 +    ui.tag{
    1.36 +      tag  = "script",
    1.37 +      attr = attr,
    1.38 +      content = function()
    1.39 +        slot.put("/* <![CDATA[ */")
    1.40 +        local script_string
    1.41 +        if type(script) == "function" then
    1.42 +          script_string = slot.use_temporary(script)
    1.43 +        else
    1.44 +          script_string = script
    1.45 +        end
    1.46 +        -- Using double parenthesis in following command is important
    1.47 +        slot.put((string.gsub(script_string, "]]>", "]]]]><![CDATA[>")))
    1.48 +        slot.put("/* ]]> */")
    1.49 +      end
    1.50 +    }
    1.51    end
    1.52  end

Impressum / About Us