webmcp

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

Impressum / About Us