webmcp

annotate framework/env/ui/script.lua @ 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 5e32ef998acf
children e017c47d43b5
rev   line source
jbe/bsw@0 1 --[[--
jbe/bsw@0 2 ui.script{
jbe/bsw@0 3 noscript_attr = noscript_attr, -- HTML attributes for noscript tag
jbe/bsw@0 4 noscript = noscript, -- string or function for noscript content
jbe/bsw@0 5 attr = attr, -- extra HTML attributes for script tag
jbe/bsw@0 6 type = type, -- type of script, defaults to "text/javascript"
jbe/bsw@0 7 script = script, -- string or function for script content
jbe/bsw@0 8 }
jbe/bsw@0 9
jbe/bsw@9 10 This function is used to insert a script into the active slot.
jbe/bsw@0 11
jbe/bsw@0 12 --]]--
jbe/bsw@0 13
jbe/bsw@0 14 function ui.script(args)
jbe/bsw@0 15 local args = args or {}
jbe/bsw@0 16 local noscript_attr = args.noscript_attr
jbe/bsw@0 17 local noscript = args.noscript
jbe/bsw@0 18 local attr = table.new(args.attr)
jbe/bsw@0 19 attr.type = attr.type or args.type or "text/javascript"
jbe/bsw@0 20 local script = args.script
jbe/bsw@4 21 if args.external then
jbe/bsw@4 22 attr.src = encode.url{ external = args.external }
jbe/bsw@4 23 elseif args.static then
jbe/bsw@4 24 attr.src = encode.url{ static = args.static }
jbe/bsw@4 25 end
jbe/bsw@0 26 if noscript then
jbe/bsw@0 27 ui.tag{ tag = "noscript", attr = attr, content = noscript }
jbe/bsw@0 28 end
jbe/bsw@4 29 if attr.src then
jbe/bsw@4 30 ui.tag{ tag = "script", attr = attr, content = "" }
jbe/bsw@4 31 elseif script then
jbe/bsw@9 32 ui.tag{
jbe/bsw@9 33 tag = "script",
jbe/bsw@9 34 attr = attr,
jbe/bsw@9 35 content = function()
jbe/bsw@9 36 slot.put("/* <![CDATA[ */")
jbe/bsw@9 37 local script_string
jbe/bsw@9 38 if type(script) == "function" then
jbe/bsw@9 39 script_string = slot.use_temporary(script)
jbe/bsw@9 40 else
jbe/bsw@9 41 script_string = script
jbe/bsw@9 42 end
jbe/bsw@9 43 -- Using double parenthesis in following command is important
jbe/bsw@9 44 slot.put((string.gsub(script_string, "]]>", "]]]]><![CDATA[>")))
jbe/bsw@9 45 slot.put("/* ]]> */")
jbe/bsw@9 46 end
jbe/bsw@9 47 }
jbe/bsw@0 48 end
jbe/bsw@0 49 end

Impressum / About Us