class Html::Element

Constants

BLANK_ATTRS

Attributes that should render even if blank

INLINE_SET

Inline formatting tags

SINGLETON_SET

Commonly empty tags

Attributes

attrs[RW]

Add back in our accessors

tag[RW]

Add back in our accessors

Public Class Methods

build(*args) { |el| ... } click to toggle source

One-stop shop for building content

# 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
new(tag, text=nil, attrs={}) { |self| ... } click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source
# File lib/iron/web/html/element.rb, line 115
def [](key)
  @attrs[key.to_sym]
end
[]=(key, value) click to toggle source
# File lib/iron/web/html/element.rb, line 119
def []=(key, value)
  @attrs[key.to_sym] = value
end
force_end!() click to toggle source
# File lib/iron/web/html/element.rb, line 67
def force_end!
  @force_end = true
end
html() click to toggle source
# File lib/iron/web/html/element.rb, line 75
def html
  @html ||= Html.new
  @html
end
html=(arg) click to toggle source
# 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
inspect() click to toggle source
# File lib/iron/web/html/element.rb, line 127
def inspect
  render
end
is_a?(other) click to toggle source
# File lib/iron/web/html/element.rb, line 131
def is_a?(other)
  return other == Html::Element
end
method_missing(method, *args) click to toggle source

Set/get attrs on any method missing calls

Calls superclass method
# 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
      # We have an attempt to set a missing field...
      @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
    # There really is no method...
    super
  end
end
render(depth = 0, inblock = false) click to toggle source
# File lib/iron/web/html/element.rb, line 135
def render(depth = 0, inblock = false)
  # Convert attrs to strings
  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('')

  # Build start tag
  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!() click to toggle source
# File lib/iron/web/html/element.rb, line 71
def skip_newline!
  @skip_newline = true
end
to_s() click to toggle source
# File lib/iron/web/html/element.rb, line 123
def to_s
  render
end