webmcp

view framework/env/ui/script.lua @ 10:e017c47d43b5

Modified encode.json to avoid special CDATA sequences in output
author jbe
date Wed Feb 03 00:57:18 2010 +0100 (2010-02-03)
parents 64f4540ce88c
children d76a8857ba62
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 WARNING: The given script MUST NOT include two closing square brackets directly followed by a greater-than sign, unless the output is interpreted strictly as XHTML. For string literals this is ensured automatically, if being encoded with encode.json{...}.
14 --]]--
16 function ui.script(args)
17 local args = args or {}
18 local noscript_attr = args.noscript_attr
19 local noscript = args.noscript
20 local attr = table.new(args.attr)
21 attr.type = attr.type or args.type or "text/javascript"
22 local script = args.script
23 if args.external then
24 attr.src = encode.url{ external = args.external }
25 elseif args.static then
26 attr.src = encode.url{ static = args.static }
27 end
28 if noscript then
29 ui.tag{ tag = "noscript", attr = attr, content = noscript }
30 end
31 if attr.src then
32 ui.tag{ tag = "script", attr = attr, content = "" }
33 elseif script then
34 ui.tag{
35 tag = "script",
36 attr = attr,
37 content = function()
38 slot.put("/* <![CDATA[ */")
39 local script_string
40 if type(script) == "function" then
41 script_string = slot.use_temporary(script)
42 else
43 script_string = script
44 end
45 -- Using double parenthesis in following command is important
46 slot.put((string.gsub(script_string, "]]>", "]]]]><![CDATA[>")))
47 slot.put("/* ]]> */")
48 end
49 }
50 end
51 end

Impressum / About Us