~ubuntu-branches/ubuntu/saucy/ruby-erubis/saucy

« back to all changes in this revision

Viewing changes to lib/erubis/helper.rb

  • Committer: Package Import Robot
  • Author(s): Laurent Bigonville
  • Date: 2012-01-26 15:15:58 UTC
  • Revision ID: package-import@ubuntu.com-20120126151558-9u7mnf9ooqnw3bwz
Tags: upstream-2.7.0
ImportĀ upstreamĀ versionĀ 2.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
## $Release: 2.7.0 $
 
3
## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
 
4
##
 
5
 
 
6
 
 
7
module Erubis
 
8
 
 
9
  ##
 
10
  ## helper for xml
 
11
  ##
 
12
  module XmlHelper
 
13
 
 
14
    module_function
 
15
 
 
16
    ESCAPE_TABLE = {
 
17
      '&' => '&',
 
18
      '<' => '&lt;',
 
19
      '>' => '&gt;',
 
20
      '"' => '&quot;',
 
21
      "'" => '&#039;',
 
22
    }
 
23
 
 
24
    def escape_xml(value)
 
25
      value.to_s.gsub(/[&<>"]/) { |s| ESCAPE_TABLE[s] }   # or /[&<>"']/
 
26
      #value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
 
27
    end
 
28
 
 
29
    def escape_xml2(value)
 
30
      return value.to_s.gsub(/\&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
 
31
    end
 
32
 
 
33
    alias h escape_xml
 
34
    alias html_escape escape_xml
 
35
 
 
36
    def url_encode(str)
 
37
      return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
 
38
        s.unpack('C*').collect { |i| "%%%02X" % i }.join
 
39
      }
 
40
    end
 
41
 
 
42
    alias u url_encode
 
43
 
 
44
  end
 
45
 
 
46
 
 
47
end