class Forma::Html::Element

Element class.

Attributes

attrs[R]
id[R]
tag[R]
text[RW]

Public Class Methods

new(tag, h) click to toggle source
# File lib/forma/html.rb, line 104
def initialize(tag, h)
  @tag = tag.to_s
  if h[:text]; @text = h[:text]
  elsif h[:html]; @text = h[:html].html_safe
  end
  @attrs = h[:attrs] || []
  @children = []
  h[:children].each { |c| @children << c } if h[:children]
  ids = @attrs.select { |x| x.is_a?(SimpleAttr) and x.name == "id" }.map { |x| x.value }
  @id = ids[0] if ids.length > 0
  @classes = @attrs.select { |x| x.is_a?(ClassAttr) }.map{ |x| x.values }.flatten
end

Public Instance Methods

attrs_by_name(name) click to toggle source
# File lib/forma/html.rb, line 125
def attrs_by_name(name)
  if name.to_s == 'class' then attrs.select { |x| x.is_a?(ClassAttr) }
  elsif name.to_s == 'style' then attrs.select { |x| x.is_a?(SimpleAttr) }
  else attrs.select { |x| (x.respond_to?(:name) and x.name == name.to_s) }
  end
end
klass() click to toggle source
# File lib/forma/html.rb, line 117
def klass
  @classes
end
to_s() click to toggle source
# File lib/forma/html.rb, line 121
def to_s
  generate_html.html_safe
end

Private Instance Methods

generate_children() click to toggle source
# File lib/forma/html.rb, line 158
def generate_children
  h = ''
  @children.each { |c| h << c.to_s }
  h
end
generate_html() click to toggle source
# File lib/forma/html.rb, line 134
def generate_html
  h = ''
  h << '<' << @tag << generate_tag_and_attributes.to_s << '>'
  h << generate_inner_html
  h << '</' << @tag << '>'
  h
end
generate_inner_html() click to toggle source
# File lib/forma/html.rb, line 147
def generate_inner_html
  h = ''
  if @text.html_safe?
    h << @text
  else
    h << ERB::Util.html_escape(@text)
  end
  h << generate_children unless @children.blank?
  h
end
generate_tag_and_attributes() click to toggle source
# File lib/forma/html.rb, line 142
def generate_tag_and_attributes
  attrs = @attrs.map{|a| a.to_s}.reject{|s| s.blank? }.join(" ")
  ' ' << attrs unless attrs.blank?
end