Class: HTMLTree::Element

Inherits:
Object show all
Includes:
TreeElement
Defined in:
lib/web/htmltools/element.rb,
lib/web/htmltools/xpath.rb

Overview

This is a TreeElement that represents tagged items in an HTML document.

Instance Method Summary collapse

Methods included from TreeElement

#add_child, #children, #content, #dump, #each, #get_elements, #has_children?, #parent, #parent=, #remove_child, #rexml_match, #root

Instance Method Details

#[](name) ⇒ Object

Return the value of a single attribute (a String or Array).



218
# File 'lib/web/htmltools/element.rb', line 218

def [](name); attribute(name); end

#[]=(name, *values) ⇒ Object

Replace an attribute.



221
222
223
224
225
# File 'lib/web/htmltools/element.rb', line 221

def []=(name, *values)
  @_attributes[name] = values.size > 1 ? values : values[0]
  @_attribute_order.delete(name)
  self
end

#add_attribute(name, *values) ⇒ Object

Append an attribute. values are first flattened into an Array, then converted into strings.

If there is a single attribute value, it will appear as a String, otherwise it will be an Array of Strings.

Example:

element.add_attribute("width", "123")
element.add_attribute("value", [ "a", "b" ])


183
184
185
186
187
188
189
190
191
192
193
# File 'lib/web/htmltools/element.rb', line 183

def add_attribute(name, *values)
  values = values.flatten.collect { |ea| ea.to_s.strip }
  name = name.downcase
  if @_attributes.include?(name)
    @_attributes[name] = @_attributes[name].to_a + values
  else
    @_attributes[name] = values.size > 1 ? values : values[0]
  end
  @_attribute_order << name
  self
end

#as_rexml_document(rparent = nil, context = {}) ⇒ Object

convert the given HTMLTree::Element (or HTMLTree::Document) into a REXML::Element or REXML::Document, ready to use REXML::XPath on. Note that this caches the tree; further changes to my tree will not be reflected in subsequent calls



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/web/htmltools/xpath.rb', line 29

def as_rexml_document(rparent = nil, context = {})
  return @_rexml_tree if @_rexml_tree
  node = REXML::Element.new( tag, rparent, context )
  attribute_order().each { |attr|
    node.add_attribute(attr, attribute(attr).to_s)
  }
  children().each { |child|
    childNode = child.as_rexml_document(node, context)
  }
  @_rexml_tree = node
end

#attribute(name) ⇒ Object

Return the value of a single attribute (a String or Array).



215
# File 'lib/web/htmltools/element.rb', line 215

def attribute(name); @_attributes[name]; end

#attribute_orderObject

Return the order of my attributes



212
# File 'lib/web/htmltools/element.rb', line 212

def attribute_order; @_attribute_order; end

#attributesObject

Return my attributes Hash.



209
# File 'lib/web/htmltools/element.rb', line 209

def attributes; @_attributes; end

#can_have_children?Boolean

Returns:

  • (Boolean)


159
# File 'lib/web/htmltools/element.rb', line 159

def can_have_children?; true; end

#data?Boolean

Return true if I’m data instead of a tag

Returns:

  • (Boolean)


162
# File 'lib/web/htmltools/element.rb', line 162

def data?; false; end

#tagObject

Return my tag (should be a String)



196
# File 'lib/web/htmltools/element.rb', line 196

def tag; @_tag; end

#tag_infoObject

Return an HTML::Tag for further information, or nil if this is an unknown tag.



200
201
202
203
204
205
206
# File 'lib/web/htmltools/element.rb', line 200

def tag_info
  begin
    HTML::Tag.named(@_tag)
  rescue NoSuchHTMLTagError
    nil
  end
end

#to_sObject



164
165
166
167
168
169
170
171
172
# File 'lib/web/htmltools/element.rb', line 164

def to_s
  a = [ "<", tag ]
  @_attribute_order.each { |k|
    v = @_attributes[k]
    a << " #{k.to_s}=\"#{v.to_s}\""
  }
  a << ">"
  a.join('')
end

#write(io) ⇒ Object

Print me (and my descendents) on the given IO stream.



228
229
230
231
232
233
234
# File 'lib/web/htmltools/element.rb', line 228

def write(io)
  io << self
  children.each { |t| t.write(io) }
  unless tag_info.is_empty_element
    io.puts( "</#{tag()}>" )
  end
end