Method: HtmlGen::Element#html

Defined in:
lib/html_gen/element.rb

#html(args = {}) ⇒ Object

Returns the HTML for the element. To avoid indentation and newlines you can use the ‘pretty’-argument:

element.html(pretty: false)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/html_gen/element.rb', line 121

def html(args = {})
  if args[:level]
    level = args[:level]
  else
    level = 0
  end

  if args.key?(:pretty)
    pretty = args[:pretty]
  else
    pretty = true
  end

  # Used for keeping 'pretty'-value and correct indentation according to parent elements.
  pass_args = {level: (level + 1), pretty: pretty, inden: @inden}

  # Clone the attributes-hash since we are going to add stuff to it, and it shouldnt be reflected
  # (if 'html' is called multiple times, it will bug unless we clone).
  attr = @attr.clone

  # Start generating the string with HTML (possible go give a custom 'str'-variable where the content should be pushed to).
  if args[:str]
    str = args[:str]
  else
    str = ""
  end

  str << @inden * level if pretty && level > 0
  str << "<#{@name}"

  # Add content from the 'css'-hash to the 'style'-attribute in the right format.
  unless @css.empty?
    style = ""
    @css.each do |key, val|
      style << "; " unless style.empty?
      style << "#{key}: #{val};"
    end

    if attr[:style] && !attr[:style].empty?
      attr[:style] << "; #{style}"
    else
      attr[:style] = style
    end
  end

  # Add content from the 'classes'-array to the 'class'-attribute in the right format.
  unless @classes.empty?
    class_str = @classes.join(" ")

    if @attr[:class] && !@attr[:class].empty?
      attr[:class] << " #{class_str}"
    else
      attr[:class] = class_str
    end
  end

  # Write out the attributes to the string.
  attr.each do |key, val|
    str << " #{key}=\"#{HtmlGen.escape_html(val)}\""
  end

  str << " #{data_attributes(@data, "data")}" if @data.any?

  forbidden_short = FORBIDDEN_SHORT.include?(@name.to_s)
  skip_pretty = false

  if @eles.empty? && @str.empty? && @str_html.empty? && !forbidden_short
    # If no sub-string, sub-HTML or sub-elements are given, we should end the HTML with " />".
    str << " />"
    str << @nl if pretty
  else
    # Write end-of-element and then all sub-elements.
    str << ">"

    if @eles.empty? && @str.empty? && @str_html.empty? && forbidden_short
      skip_pretty = true
    end

    str << @nl if pretty && !skip_pretty

    unless @str.empty?
      str << @inden * (level + 1) if pretty

      if @str.respond_to?(:html_safe?) && @str.html_safe?
        str << @str
      else
        str << HtmlGen.escape_html(@str)
      end

      str << @nl if pretty
    end

    unless @str_html.empty?
      str << @inden * (level + 1) if pretty
      str << @str_html
      str << @nl if pretty
    end

    @eles.each do |subele|
      str << subele.html(pass_args)
    end

    str << @inden * level if pretty && level > 0 && !skip_pretty
    str << "</#{@name}>"
    str << @nl if pretty
  end

  str = str.html_safe if str.respond_to?(:html_safe)
  str
end