webmcp
view framework/env/ui/script.lua @ 75:faedbc8615e0
Allow particular records to be selected and/or disabled in ui.field.select{...}, even if they share the same key, and never select more than one record
| author | jbe | 
|---|---|
| date | Wed Jun 20 00:35:02 2012 +0200 (2012-06-20) | 
| parents | d76a8857ba62 | 
| children | 
 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: If the script contains two closing square brackets directly followed by a greater-than sign, it will be rejected to avoid ambiguity related to HTML vs. XML parsing. Additional space characters can be added within the program code to avoid occurrence of the character sequence. The function encode.json{...} encodes all string literals in a way that the sequence is not contained.
    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     local script_string
    35     if type(script) == "function" then
    36       script_string = slot.use_temporary(script)
    37     else
    38       script_string = script
    39     end
    40     if string.find(script_string, "]]>") then
    41       error('Script contains character sequence "]]>" and is thus rejected to avoid ambiguity. If this sequence occurs as part of program code, please add additional space characters. If this sequence occurs inside a string literal, please encode one of this characters using the \\uNNNN unicode escape sequence.')
    42     end
    43     ui.tag{
    44       tag  = "script",
    45       attr = attr,
    46       content = function()
    47         slot.put("/* <![CDATA[ */")
    48         slot.put(script_string)
    49         slot.put("/* ]]> */")
    50       end
    51     }
    52   end
    53 end
