slim 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- module Slim
2
- # Slim helper functions
3
- #
4
- # @api public
5
- module Helpers
6
- # Iterate over `Enumerable` object
7
- # yielding each element to a Slim block
8
- # and putting the result into `<li>` elements.
9
- # For example:
10
- #
11
- # = list_of([1,2]) do |i|
12
- # = i
13
- #
14
- # Produces:
15
- #
16
- # <li>1</li>
17
- # <li>2</li>
18
- #
19
- # @param enum [Enumerable] The enumerable objects to iterate over
20
- # @yield [item] A block which contains Slim code that goes within list items
21
- # @yieldparam item An element of `enum`
22
- # @api public
23
- def list_of(enum, &block)
24
- list = enum.map do |i|
25
- "<li>#{yield(i)}</li>"
26
- end.join("\n")
27
- list.respond_to?(:html_safe) ? list.html_safe : list
28
- end
29
-
30
- # Returns an escaped copy of `html`.
31
- # Strings which are declared as html_safe are not escaped.
32
- #
33
- # @param html [String] The string to escape
34
- # @return [String] The escaped string
35
- # @api public
36
- def escape_html_safe(html)
37
- html.html_safe? ? html : escape_html(html)
38
- end
39
-
40
- if defined?(EscapeUtils)
41
- # Returns an escaped copy of `html`.
42
- #
43
- # @param html [String] The string to escape
44
- # @return [String] The escaped string
45
- # @api public
46
- def escape_html(html)
47
- EscapeUtils.escape_html(html.to_s)
48
- end
49
- elsif RUBY_VERSION > '1.9'
50
- # Used by escape_html
51
- # @api private
52
- ESCAPE_HTML = {
53
- '&' => '&amp;',
54
- '"' => '&quot;',
55
- '<' => '&lt;',
56
- '>' => '&gt;',
57
- '/' => '&#47;',
58
- }.freeze
59
-
60
- # Returns an escaped copy of `html`.
61
- #
62
- # @param html [String] The string to escape
63
- # @return [String] The escaped string
64
- # @api public
65
- def escape_html(html)
66
- html.to_s.gsub(/[&\"<>\/]/, ESCAPE_HTML)
67
- end
68
- else
69
- # Returns an escaped copy of `html`.
70
- #
71
- # @param html [String] The string to escape
72
- # @return [String] The escaped string
73
- # @api public
74
- def escape_html(html)
75
- html.to_s.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;').gsub(/\//, '&#47;')
76
- end
77
- end
78
-
79
- module_function :escape_html, :escape_html_safe
80
- end
81
- end
@@ -1,30 +0,0 @@
1
- require 'helper'
2
-
3
- class TestSlimHelpers < TestSlim
4
- class HtmlSafeString < String
5
- def html_safe?
6
- true
7
- end
8
- end
9
-
10
- def test_list_of
11
- source = %q{
12
- == list_of([1, 2, 3]) do |i|
13
- = i
14
- }
15
-
16
- assert_html "<li>1</li>\n<li>2</li>\n<li>3</li>", source, :helpers => true
17
- end
18
-
19
- def test_list_of_with_html_safe
20
- Object.send(:define_method, :html_safe?) { false }
21
- String.send(:define_method, :html_safe) { HtmlSafeString.new(self) }
22
-
23
- source = %q{
24
- = list_of([1, 2, 3]) do |i|
25
- = i
26
- }
27
-
28
- html = Slim::Template.new(:helpers => true, :use_html_safe => true) { source }.render(@env)
29
- end
30
- end