Class: HTMLTree::Element
- Inherits:
-
Object
- Object
- HTMLTree::Element
- Includes:
- TreeElement
- Defined in:
- lib/html/element.rb,
lib/html/xpath.rb
Overview
This is a TreeElement that represents tagged items in an HTML document.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return the value of a single attribute (a String or Array).
-
#[]=(name, *values) ⇒ Object
Replace an attribute.
-
#add_attribute(name, *values) ⇒ Object
Append an attribute.
-
#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.
-
#attribute(name) ⇒ Object
Return the value of a single attribute (a String or Array).
-
#attribute_order ⇒ Object
Return the order of my attributes.
-
#attributes ⇒ Object
Return my attributes Hash.
- #can_have_children? ⇒ Boolean
-
#data? ⇒ Boolean
Return true if I’m data instead of a tag.
-
#elements ⇒ Object
Return the children of this node that are elements (not data).
-
#path ⇒ Object
Return the path to this element from the root.
- #show_structure(indent = 0) ⇒ Object
-
#tag ⇒ Object
Return my tag (should be a String).
-
#tag_info ⇒ Object
Return an HTML::Tag for further information, or nil if this is an unknown tag.
- #to_s ⇒ Object
-
#write(io) ⇒ Object
Print me (and my descendents) on the given IO stream.
Methods included from TreeElement
#add_child, #children, #content, #dump, #each, #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).
245 |
# File 'lib/html/element.rb', line 245 def [](name); attribute(name); end |
#[]=(name, *values) ⇒ Object
Replace an attribute.
248 249 250 251 252 |
# File 'lib/html/element.rb', line 248 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" ])
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/html/element.rb', line 188 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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/html/xpath.rb', line 30 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).
242 |
# File 'lib/html/element.rb', line 242 def attribute(name); @_attributes[name]; end |
#attribute_order ⇒ Object
Return the order of my attributes
239 |
# File 'lib/html/element.rb', line 239 def attribute_order; @_attribute_order; end |
#attributes ⇒ Object
Return my attributes Hash.
236 |
# File 'lib/html/element.rb', line 236 def attributes; @_attributes; end |
#can_have_children? ⇒ Boolean
164 |
# File 'lib/html/element.rb', line 164 def can_have_children?; true; end |
#data? ⇒ Boolean
Return true if I’m data instead of a tag
167 |
# File 'lib/html/element.rb', line 167 def data?; false; end |
#elements ⇒ Object
Return the children of this node that are elements (not data)
231 232 233 |
# File 'lib/html/element.rb', line 231 def elements children.select { |node| node.is_a? Element } end |
#path ⇒ Object
Return the path to this element from the root
214 215 216 217 218 219 220 221 222 |
# File 'lib/html/element.rb', line 214 def path path = [] node = self while node do path.unshift node.tag node = node.parent end path.join(".") end |
#show_structure(indent = 0) ⇒ Object
224 225 226 227 228 |
# File 'lib/html/element.rb', line 224 def show_structure(indent = 0) puts(' ' * indent) + path elements.each { |node| node.show_structure(indent + 2) } nil end |
#tag ⇒ Object
Return my tag (should be a String)
201 |
# File 'lib/html/element.rb', line 201 def tag; @_tag; end |
#tag_info ⇒ Object
Return an HTML::Tag for further information, or nil if this is an unknown tag.
205 206 207 208 209 210 211 |
# File 'lib/html/element.rb', line 205 def tag_info begin HTML::Tag.named(@_tag) rescue NoSuchHTMLTagError nil end end |
#to_s ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/html/element.rb', line 169 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.
255 256 257 258 259 260 261 |
# File 'lib/html/element.rb', line 255 def write(io) io << self children.each { |t| t.write(io) } unless tag_info.is_empty_element io.puts( "</#{tag()}>" ) end end |