webmcp

diff framework/env/encode/html.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/encode/html.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,32 @@
     1.4 +--[[--
     1.5 +result =      -- encoded string
     1.6 +encode.html(
     1.7 +  str         -- original string
     1.8 +)
     1.9 +
    1.10 +This function replaces the special characters '<', '>', '&' and '"' by their HTML entities '&lt;', '&rt;', '&amp;' and '&quot;'.
    1.11 +
    1.12 +NOTE: ACCELERATED FUNCTION
    1.13 +Do not change unless also you also update webmcp_accelerator.c
    1.14 +
    1.15 +--]]--
    1.16 +
    1.17 +function encode.html(text)
    1.18 +  -- TODO: perhaps filter evil control characters?
    1.19 +  return (
    1.20 +    string.gsub(
    1.21 +      text, '[<>&"]',
    1.22 +      function(char)
    1.23 +        if char == '<' then
    1.24 +          return "&lt;"
    1.25 +        elseif char == '>' then
    1.26 +          return "&gt;"
    1.27 +        elseif char == '&' then
    1.28 +          return "&amp;"
    1.29 +        elseif char == '"' then
    1.30 +          return "&quot;"
    1.31 +        end
    1.32 +      end
    1.33 +    )
    1.34 +  )
    1.35 +end

Impressum / About Us