class Arbre::HTML::Tag
Constants
- INDENT_SIZE
- SELF_CLOSING_ELEMENTS
See: www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
Attributes
attributes[R]
Public Class Methods
new(*)
click to toggle source
Calls superclass method
Arbre::Element::new
# File lib/arbre/html/tag.rb, line 13 def initialize(*) super @attributes = Attributes.new end
Public Instance Methods
add_class(class_names)
click to toggle source
# File lib/arbre/html/tag.rb, line 73 def add_class(class_names) class_list.add class_names end
build(*args)
click to toggle source
Calls superclass method
Arbre::Element#build
# File lib/arbre/html/tag.rb, line 18 def build(*args) super attributes = extract_arguments(args) self.content = args.first if args.first for_value = attributes[:for] unless for_value.is_a?(String) || for_value.is_a?(Symbol) set_for_attribute(attributes.delete(:for)) end attributes.each do |key, value| set_attribute(key, value) end end
class_list()
click to toggle source
# File lib/arbre/html/tag.rb, line 86 def class_list list = get_attribute(:class) case list when ClassList list when String set_attribute(:class, ClassList.build_from_string(list)) else set_attribute(:class, ClassList.new) end end
class_names()
click to toggle source
Returns a string of classes
# File lib/arbre/html/tag.rb, line 82 def class_names class_list.to_s end
extract_arguments(args)
click to toggle source
# File lib/arbre/html/tag.rb, line 33 def extract_arguments(args) if args.last.is_a?(Hash) args.pop else {} end end
get_attribute(name)
click to toggle source
# File lib/arbre/html/tag.rb, line 45 def get_attribute(name) @attributes[name.to_sym] end
Also aliased as: attr
has_attribute?(name)
click to toggle source
# File lib/arbre/html/tag.rb, line 50 def has_attribute?(name) @attributes.has_key?(name.to_sym) end
id()
click to toggle source
# File lib/arbre/html/tag.rb, line 58 def id get_attribute(:id) end
id!()
click to toggle source
Generates and id for the object if it doesn't exist already
# File lib/arbre/html/tag.rb, line 63 def id! return id if id self.id = object_id.to_s id end
id=(id)
click to toggle source
# File lib/arbre/html/tag.rb, line 69 def id=(id) set_attribute(:id, id) end
remove_attribute(name)
click to toggle source
# File lib/arbre/html/tag.rb, line 54 def remove_attribute(name) @attributes.delete(name.to_sym) end
remove_class(class_names)
click to toggle source
# File lib/arbre/html/tag.rb, line 77 def remove_class(class_names) class_list.delete(class_names) end
set_attribute(name, value)
click to toggle source
# File lib/arbre/html/tag.rb, line 41 def set_attribute(name, value) @attributes[name.to_sym] = value end
to_s()
click to toggle source
# File lib/arbre/html/tag.rb, line 99 def to_s indent(opening_tag, content, closing_tag).html_safe end
Private Instance Methods
attributes_html()
click to toggle source
# File lib/arbre/html/tag.rb, line 151 def attributes_html " #{attributes}" if attributes.any? end
child_is_text?()
click to toggle source
# File lib/arbre/html/tag.rb, line 147 def child_is_text? children.size == 1 && children.first.is_a?(TextNode) end
closing_tag()
click to toggle source
# File lib/arbre/html/tag.rb, line 109 def closing_tag "</#{tag_name}>" end
default_id_for_prefix()
click to toggle source
# File lib/arbre/html/tag.rb, line 183 def default_id_for_prefix nil end
dom_class_name_for(record)
click to toggle source
# File lib/arbre/html/tag.rb, line 163 def dom_class_name_for(record) if record.class.respond_to?(:model_name) record.class.model_name.singular else record.class.name.underscore.gsub("/", "_") end end
dom_id_for(record)
click to toggle source
# File lib/arbre/html/tag.rb, line 171 def dom_id_for(record) id = if record.respond_to?(:to_key) record.to_key elsif record.respond_to?(:id) record.id else record.object_id end [default_id_for_prefix, dom_class_name_for(record), id].compact.join("_") end
indent(open_tag, child_content, close_tag)
click to toggle source
# File lib/arbre/html/tag.rb, line 115 def indent(open_tag, child_content, close_tag) spaces = ' ' * indent_level * INDENT_SIZE html = "" if no_child? || child_is_text? if self_closing_tag? html << spaces << open_tag.sub( />$/, '/>' ) else # one line html << spaces << open_tag << child_content << close_tag end else # multiple lines html << spaces << open_tag << "\n" html << child_content # the child takes care of its own spaces html << spaces << close_tag end html << "\n" html end
no_child?()
click to toggle source
# File lib/arbre/html/tag.rb, line 143 def no_child? children.empty? end
opening_tag()
click to toggle source
# File lib/arbre/html/tag.rb, line 105 def opening_tag "<#{tag_name}#{attributes_html}>" end
self_closing_tag?()
click to toggle source
# File lib/arbre/html/tag.rb, line 139 def self_closing_tag? SELF_CLOSING_ELEMENTS.include?(tag_name.to_sym) end
set_for_attribute(record)
click to toggle source
# File lib/arbre/html/tag.rb, line 155 def set_for_attribute(record) return unless record # set_attribute :id, ActionController::RecordIdentifier.dom_id(record, default_id_for_prefix) # add_class ActionController::RecordIdentifier.dom_class(record) set_attribute :id, dom_id_for(record) add_class dom_class_name_for(record) end