Module: Tzispa::Helpers::Html
- Included in:
- Pattern
- Defined in:
- lib/tzispa/helpers/html.rb
Instance Method Summary collapse
- #html_checked(value) ⇒ Object
- #html_escape(str) ⇒ Object
-
#html_sanitize(html, okTags = 'a href, b, br, i, p') ⇒ Object
$Id: sanitize.rb 3 2005-04-05 12:51:14Z dwight $.
- #html_selected(value) ⇒ Object
- #html_strip(source) ⇒ Object
- #html_unescape(str) ⇒ Object
- #html_urldecode(str) ⇒ Object
- #html_urlencode(str) ⇒ Object
Instance Method Details
#html_checked(value) ⇒ Object
10 11 12 |
# File 'lib/tzispa/helpers/html.rb', line 10 def html_checked(value) value ? ' checked="checked"' : String.new.freeze end |
#html_escape(str) ⇒ Object
18 19 20 |
# File 'lib/tzispa/helpers/html.rb', line 18 def html_escape(str) CGI::escapeHTML str end |
#html_sanitize(html, okTags = 'a href, b, br, i, p') ⇒ Object
$Id: sanitize.rb 3 2005-04-05 12:51:14Z dwight $
Copyright © 2005 Dwight Shih A derived work of the Perl version: Copyright © 2002 Brad Choate, bradchoate.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tzispa/helpers/html.rb', line 71 def html_sanitize( html, okTags='a href, b, br, i, p' ) # no closing tag necessary for these soloTags = ["br","hr"] # Build hash of allowed tags with allowed attributes = okTags.downcase().split(',').collect!{ |s| s.split(' ') } allowed = Hash.new .each do |s| key = s.shift allowed[key] = s end # Analyze all <> elements stack = Array.new result = html.gsub( /(<.*?>)/m ) do | element | if element =~ /\A<\/(\w+)/ then # </tag> tag = $1.downcase if allowed.include?(tag) && stack.include?(tag) then # If allowed and on the stack # Then pop down the stack top = stack.pop out = "</#{top}>" until top == tag do top = stack.pop out << "</#{top}>" end out end elsif element =~ /\A<(\w+)\s*\/>/ # <tag /> tag = $1.downcase if allowed.include?(tag) then "<#{tag} />" end elsif element =~ /\A<(\w+)/ then # <tag ...> tag = $1.downcase if allowed.include?(tag) then if ! soloTags.include?(tag) then stack.push(tag) end if allowed[tag].length == 0 then # no allowed attributes "<#{tag}>" else # allowed attributes? out = "<#{tag}" while ( $' =~ /(\w+)=("[^"]+")/ ) attr = $1.downcase valu = $2 if allowed[tag].include?(attr) then out << " #{attr}=#{valu}" end end out << ">" end end end end # eat up unmatched leading > while result.sub!(/\A([^<]*)>/m) { $1 } do end # eat up unmatched trailing < while result.sub!(/<([^>]*)\Z/m) { $1 } do end # clean up the stack if stack.length > 0 then result << "</#{stack.reverse.join('></')}>" end result end |
#html_selected(value) ⇒ Object
14 15 16 |
# File 'lib/tzispa/helpers/html.rb', line 14 def html_selected(value) value ? ' selected="selected"' : String.new.freeze end |
#html_strip(source) ⇒ Object
34 35 36 |
# File 'lib/tzispa/helpers/html.rb', line 34 def html_strip(source) source.gsub(/<\/?[^>]*>/, '') end |
#html_unescape(str) ⇒ Object
22 23 24 |
# File 'lib/tzispa/helpers/html.rb', line 22 def html_unescape(str) CGI::unescapeHTML str end |
#html_urldecode(str) ⇒ Object
30 31 32 |
# File 'lib/tzispa/helpers/html.rb', line 30 def html_urldecode(str) URI.unescape str end |
#html_urlencode(str) ⇒ Object
26 27 28 |
# File 'lib/tzispa/helpers/html.rb', line 26 def html_urlencode(str) URI.escape str end |