Debian Codegraph @

lib/erb/util.rb

raw
1 # frozen_string_literal: true
2 #--
3 # ERB::Escape
4 #
5 # A subset of ERB::Util. Unlike ERB::Util#html_escape, we expect/hope
6 # Rails will not monkey-patch ERB::Escape#html_escape.
7 begin
8 # We don't build the C extension for JRuby, TruffleRuby, and WASM
9 if $LOAD_PATH.resolve_feature_path('erb/escape')
10 require 'erb/escape'
11 end
12 rescue LoadError # resolve_feature_path raises LoadError on TruffleRuby 22.3.0
13 end
14 unless defined?(ERB::Escape)
15 module ERB::Escape
16 def html_escape(s)
17 CGI.escapeHTML(s.to_s)
18 end
19 module_function :html_escape
20 end
21 end
22
23 #--
24 # ERB::Util
25 #
26 # A utility module for conversion routines, often handy in HTML generation.
27 module ERB::Util
28 #
29 # A utility method for escaping HTML tag characters in _s_.
30 #
31 # require "erb"
32 # include ERB::Util
33 #
34 # puts html_escape("is a > 0 & a < 10?")
35 #
36 # _Generates_
37 #
38 # is a &gt; 0 &amp; a &lt; 10?
39 #
40 include ERB::Escape # html_escape
41 module_function :html_escape
42 alias h html_escape
43 module_function :h
44
45 #
46 # A utility method for encoding the String _s_ as a URL.
47 #
48 # require "erb"
49 # include ERB::Util
50 #
51 # puts url_encode("Programming Ruby: The Pragmatic Programmer's Guide")
52 #
53 # _Generates_
54 #
55 # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
56 #
57 def url_encode(s)
58 CGI.escapeURIComponent(s.to_s)
59 end
60 alias u url_encode
61 module_function :u
62 module_function :url_encode
63 end