Class: Html::Element
Constant Summary
collapse
- SINGLETON_SET =
['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source'].freeze
- INLINE_SET =
['a','abbr','b','button','em','i','input','img','label','li','option','span','strong','title','textarea','u'].freeze
- BLANK_ATTRS =
Attributes that should render even if blank
['value','alt'].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tag, text = nil, attrs = {}) {|_self| ... } ⇒ Element
Returns a new instance of Element.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/iron/web/html/element.rb', line 42
def initialize(tag, text=nil, attrs={})
@tag = tag.to_s
@force_end = !SINGLETON_SET.include?(@tag)
@skip_newline = INLINE_SET.include?(@tag)
if text.is_a?(Hash)
@attrs = text
text = nil
else
@attrs = attrs || {}
end
if text.is_a?(String)
html << ((!text.respond_to?(:html_safe?) || !text.html_safe?) ? Html.escape_once(text) : text)
elsif text.is_a?(Html::Element)
html << text
elsif text.is_a?(Html)
@html = text
end
yield self if block_given?
self
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Set/get attrs on any method missing calls
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/iron/web/html/element.rb', line 97
def method_missing(method, *args)
parts = method.to_s.match(/^([a-z0-9_]+)(=?)$/i)
if parts
key = parts[1].to_sym
if parts[2] && args.length == 1
@attrs[key] = args[0]
return args[0]
else
raise "I think you meant <#{@tag}>.html.#{method} instead of <#{@tag}>.#{method}" if block_given?
return @attrs[key]
end
else
super
end
end
|
Instance Attribute Details
Add back in our accessors
26
27
28
|
# File 'lib/iron/web/html/element.rb', line 26
def attrs
@attrs
end
|
Add back in our accessors
26
27
28
|
# File 'lib/iron/web/html/element.rb', line 26
def tag
@tag
end
|
Class Method Details
.build(*args) {|el| ... } ⇒ Object
One-stop shop for building content
36
37
38
39
40
|
# File 'lib/iron/web/html/element.rb', line 36
def self.build(*args)
el = self.new(*args)
yield el if block_given?
el.render
end
|
Instance Method Details
115
116
117
|
# File 'lib/iron/web/html/element.rb', line 115
def [](key)
@attrs[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
119
120
121
|
# File 'lib/iron/web/html/element.rb', line 119
def []=(key, value)
@attrs[key.to_sym] = value
end
|
#force_end! ⇒ Object
67
68
69
|
# File 'lib/iron/web/html/element.rb', line 67
def force_end!
@force_end = true
end
|
75
76
77
78
|
# File 'lib/iron/web/html/element.rb', line 75
def html
@html ||= Html.new
@html
end
|
#html=(arg) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/iron/web/html/element.rb', line 80
def html=(arg)
if arg.is_a? String
@html = Html.new
@html.text! arg
elsif arg.is_a?(Html)
@html = arg
elsif arg.is_a?(Array)
@html = Html.new
arg.each do |el|
@html << el
end
else
raise 'Invalid input'
end
end
|
127
128
129
|
# File 'lib/iron/web/html/element.rb', line 127
def inspect
render
end
|
#is_a?(other) ⇒ Boolean
131
132
133
|
# File 'lib/iron/web/html/element.rb', line 131
def is_a?(other)
return other == Html::Element
end
|
#render(depth = 0, inblock = false) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/iron/web/html/element.rb', line 135
def render(depth = 0, inblock = false)
attrStr = @attrs.collect do |k,v|
if v.nil?
nil
elsif v.blank? && ! BLANK_ATTRS.include?(k.to_s)
" #{k}"
else
v = Html.escape_once(v) unless v.html_safe?
" #{k}=\"#{v}\""
end
end.compact.join('')
val = ''
val += "\n" if !inblock && !@skip_newline
val += ' ' * depth unless !inblock && @skip_newline
val += "<#{@tag}#{attrStr}>"
unless @html.nil?
val += "\n" unless @skip_newline
val += @html.render(depth+1, !@skip_newline)
val += "\n" unless @skip_newline || val.ends_with?("\n")
val += ' ' * depth unless @skip_newline
end
val += "</#{@tag}>" if (@force_end || !@html.blank?)
val += "\n" unless @skip_newline
val
end
|
#skip_newline! ⇒ Object
71
72
73
|
# File 'lib/iron/web/html/element.rb', line 71
def skip_newline!
@skip_newline = true
end
|
123
124
125
|
# File 'lib/iron/web/html/element.rb', line 123
def to_s
render
end
|