class Arboretum::DocTree::Elements::TaggedElement

A tagged element of a doctree Only TaggedElements have tags (duh!) and attributes, and TaggedElements hold no direct reference to text Ex: Both <p></p> or <br /> are considered tagged elements with no children

Attributes

attrs[RW]
namespace[RW]
tag[RW]

Public Class Methods

new(namespace=nil, tag=nil, attrs={}) click to toggle source
# File lib/arboretum/doctree.rb, line 1012
def initialize(namespace=nil, tag=nil, attrs={})
  super()

  # Element tag and attributes
  @namespace = namespace  # Symbol
  @tag = tag              # Symbol
  @attrs = attrs          # Hash: Symbol => Array of Strings
end
paired?(tag) click to toggle source
# File lib/arboretum/doctree.rb, line 1021
def self.paired?(tag)
  !@@unpaired_tags.include?(tag)
end

Public Instance Methods

add_attr_value(attr_name, attr_value) click to toggle source
# File lib/arboretum/doctree.rb, line 1107
def add_attr_value(attr_name, attr_value)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  attr_value = attr_value.split if attr_value.is_a?(String)

  # Ensure value is arrays of strings
  raise TypeError.new("Attribute value must be a String or Array of Strings") if !attr_value.is_a?(Array)
  attr_value.each{|sub_val| raise TypeError.new("Each attribute value in an array must be a String") if !sub_val.is_a?(String)}
  if self.has_attr?(attr_name)
    self.attrs[attr_name] += attr_value
  else
    self.attrs[attr_name] = attr_value
  end
  # Update tree id cache
  self.tree_residence&.id_cache_add(self.attr_value_str(:id), self) if attr_name.eql?(:id)
end
attr_value_str(attr_name) click to toggle source

Returns the string value for an attribute of the given name

# File lib/arboretum/doctree.rb, line 1058
def attr_value_str(attr_name)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  return nil if !self.has_attr?(attr_name)
  return self.attrs[attr_name].join(' ')
end
can_have_children?() click to toggle source
# File lib/arboretum/doctree.rb, line 1166
def can_have_children?
  true
end
contains_attr_val?(attr_name, attr_value) click to toggle source
# File lib/arboretum/doctree.rb, line 1147
def contains_attr_val?(attr_name, attr_value)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  self.has_attr?(attr_name) and self.attrs[attr_name].include?(attr_value)
end
copy() click to toggle source

TaggedElement deep copy method

# File lib/arboretum/doctree.rb, line 1026
def copy
  element_copy = TaggedElement.new(@namespace, @tag, @attrs.clone)
  element_copy.set_children!(@children.map {|child| child.copy})
  element_copy
end
del_attr(attr_name) click to toggle source
# File lib/arboretum/doctree.rb, line 1064
def del_attr(attr_name)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)

  # Update tree id cache
  self.tree_residence&.id_cache_remove(self.attr_value_str(:id)) if attr_name.eql?(:id)

  self.attrs.delete(attr_name)
end
del_attr_value(attr_name, attr_value) click to toggle source
# File lib/arboretum/doctree.rb, line 1090
def del_attr_value(attr_name, attr_value)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  attr_value = attr_value.split if attr_value.is_a?(String)

  # Ensure value is arrays of strings
  raise TypeError.new("Attribute value must be a String or Array of Strings") if !attr_value.is_a?(Array)
  attr_value.each{|sub_val| raise TypeError.new("Each attribute value in an array must be a String") if !sub_val.is_a?(String)}

  # Update tree id cache
  self.tree_residence&.id_cache_remove(self.attr_value_str(:id)) if attr_name.eql?(:id)
  
  self.attrs[attr_name].delete_if {|sub_val| attr_value.include?(sub_val)}

  # Update tree id cache
  self.tree_residence&.id_cache_add(self.attr_value_str(:id), self) if (attr_name.eql?(:id) and !self.attr_value_str.nil?)
end
del_namespace() click to toggle source
# File lib/arboretum/doctree.rb, line 1133
def del_namespace
  @namespace = nil
end
dump_markup(type=:xml) click to toggle source
# File lib/arboretum/doctree.rb, line 1170
def dump_markup(type=:xml)
  element_string = "<#{self.namespaced_tag}"
  self.attrs.each do |key, values|
    element_string << " #{key}"
    element_string << "=\""
    values.each do |v|
      element_string << "#{v.gsub('&','&amp;').gsub('<', '&lt;').gsub('>','&gt;')} "
    end
    element_string = element_string.chop unless values.empty?
    element_string << "\""
  end
  # Close the tag if document must have valid xml
  if self.paired? or type == :html
    element_string << ">"
  else
    element_string << " />"
  end
  element_string
end
dump_markup_close() click to toggle source
# File lib/arboretum/doctree.rb, line 1190
def dump_markup_close
  "</#{self.namespaced_tag}>"
end
equals_attr_val?(attr_name, attr_value) click to toggle source
# File lib/arboretum/doctree.rb, line 1152
def equals_attr_val?(attr_name, attr_value)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  self.has_attr?(attr_name) and (self.attrs[attr_name]-attr_value).empty?
end
has_attr?(attr_name) click to toggle source
# File lib/arboretum/doctree.rb, line 1142
def has_attr?(attr_name)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  self.attrs.has_key?(attr_name)
end
href() click to toggle source

Returns the '#'-prefixed id of this element, or an auto-assigned one if none exists

# File lib/arboretum/doctree.rb, line 1053
def href
  "#" << self.ref
end
matches_attr_val?(attr_name, attr_regex) click to toggle source
# File lib/arboretum/doctree.rb, line 1157
def matches_attr_val?(attr_name, attr_regex)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  self.has_attr?(attr_name) and !self.attrs[attr_name].grep(attr_regex).empty?
end
namespaced_tag() click to toggle source
# File lib/arboretum/doctree.rb, line 1162
def namespaced_tag
  self.namespace.nil? ? "#{self.tag}" : "#{self.namespace}:#{self.tag}"
end
paired?() click to toggle source

Returns whether or not the element is a paired element

# File lib/arboretum/doctree.rb, line 1138
def paired?
  not @@unpaired_tags.include? self.tag
end
ref() click to toggle source

Returns the id of this element, or an auto-assigned one if none exists

# File lib/arboretum/doctree.rb, line 1042
def ref
  if !self.has_attr?(:id) or self.attr_value_str(:id).nil?
    auto_id = "auto_#{SecureRandom.uuid}"
    self.attrs[:id] = [auto_id]
    auto_id
  else
    self.attrs[:id].join.gsub(' ', '')
  end
end
set_attr_value(attr_name, attr_value) click to toggle source
# File lib/arboretum/doctree.rb, line 1073
def set_attr_value(attr_name, attr_value)
  attr_name = attr_name.to_sym if !attr_name.is_a?(Symbol)
  attr_value = attr_value.split if attr_value.is_a?(String)

  # Ensure value is arrays of strings
  raise TypeError.new("Attribute value must be a String or Array of Strings") if !attr_value.is_a?(Array)
  attr_value.each{|sub_val| raise TypeError.new("Each attribute value in an array must be a String") if !sub_val.is_a?(String)}

  # Update tree id cache
  self.tree_residence&.id_cache_remove(self.attr_value_str(:id)) if attr_name.eql?(:id)

  self.attrs[attr_name] = attr_value

  # Update tree id cache
  self.tree_residence&.id_cache_add(self.attr_value_str(:id), self) if attr_name.eql?(:id)
end
set_namespace(new_ns) click to toggle source
# File lib/arboretum/doctree.rb, line 1128
def set_namespace(new_ns)
  new_ns = new_tag.to_sym if !new_tag.is_a?(Symbol)
  @namespace = new_ns
end
set_tag(new_tag) click to toggle source
# File lib/arboretum/doctree.rb, line 1123
def set_tag(new_tag)
  new_tag = new_tag.to_sym if !new_tag.is_a?(Symbol)
  @tag = new_tag
end
to_s() click to toggle source
# File lib/arboretum/doctree.rb, line 1194
def to_s
  element_string = "<#{self.namespaced_tag}"
  self.attrs.each do |key, values|
    element_string << " #{key}"
    element_string << "=\""
    values.each {|v| element_string << "#{v} "}
    element_string = element_string.chop unless values.empty?
    element_string << "\""
  end
  element_string << (self.paired? ? ">" : "/>")
end
to_s_close() click to toggle source
# File lib/arboretum/doctree.rb, line 1206
def to_s_close
  "</#{self.namespaced_tag}>"
end
update_tree_residence(update_tree) click to toggle source
# File lib/arboretum/doctree.rb, line 1032
def update_tree_residence(update_tree)
  # Update old and new tree id cache
  if self.has_attr?(:id)
    self.tree_residence&.id_cache_remove(self.attr_value_str(:id))
    update_tree&.id_cache_add(self.attr_value_str(:id), self)
  end
  super(update_tree)
end