Class: Hpricot::Elem

Inherits:
Object
  • Object
show all
Includes:
Container, Trav
Defined in:
lib/hpricot/tag.rb,
lib/hpricot/inspect.rb,
lib/hpricot/modules.rb,
lib/hpricot/modules.rb

Defined Under Namespace

Modules: Trav

Constant Summary

Constants included from Hpricot

ElementContent, ElementExclusions, ElementInclusions, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Trav

#get_attribute, #has_attribute?, #remove_attribute, #set_attribute, #traverse_all_element, #traverse_some_element

Methods included from Container::Trav

#classes, #containers, #each_child, #each_child_with_index, #each_hyperlink, #each_hyperlink_uri, #each_uri, #filter, #find_element, #get_element_by_id, #get_elements_by_tag_name, #insert_after, #insert_before, #next_sibling, #previous_sibling, #replace_child, #siblings_at, #traverse_text_internal

Methods included from Traverse

#after, #at, #before, #bogusetag?, #children_of_type, #clean_path, #comment?, #css_path, #doc?, #doctype?, #elem?, filter, #get_subnode, #inner_html, #inner_html=, #inner_text, #next_node, #node_position, #nodes_at, #position, #previous_node, #procins?, #search, #swap, #text?, #to_html, #to_original_html, #traverse_element, #traverse_text, #xmldecl?, #xpath

Methods included from Hpricot

XML, build_node, make, parse, scan

Constructor Details

#initialize(stag, children = nil, etag = nil) ⇒ Elem

Returns a new instance of Elem.



47
48
49
50
# File 'lib/hpricot/tag.rb', line 47

def initialize(stag, children=nil, etag=nil)
  @stag, @etag = stag, etag
  @children = children ? children.each { |c| c.parent = self }  : []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



46
47
48
# File 'lib/hpricot/tag.rb', line 46

def children
  @children
end

#etagObject

Returns the value of attribute etag.



46
47
48
# File 'lib/hpricot/tag.rb', line 46

def etag
  @etag
end

#stagObject

Returns the value of attribute stag.



46
47
48
# File 'lib/hpricot/tag.rb', line 46

def stag
  @stag
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


51
# File 'lib/hpricot/tag.rb', line 51

def empty?; @children.empty? end

#output(out, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hpricot/tag.rb', line 69

def output(out, opts = {})
  if empty? and ElementContent[@stag.name] == :EMPTY
    @stag.output(out, opts.merge(:style => :empty))
  else
    @stag.output(out, opts)
    @children.each { |n| n.output(out, opts) }
    if @etag
      @etag.output(out, opts)
    elsif !opts[:preserve]
      ETag.new(@stag.name).output(out, opts)
    end
  end
  out
end

#pathnameObject



68
# File 'lib/hpricot/tag.rb', line 68

def pathname; self.name end

#pretty_print(q) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hpricot/inspect.rb', line 20

def pretty_print(q)
  if empty?
    q.group(1, '{emptyelem', '}') {
      q.breakable; q.pp @stag
    }
  else
    q.group(1, "{elem", "}") {
      q.breakable; q.pp @stag
      if @children
        @children.each {|elt| q.breakable; q.pp elt }
      end
      if @etag
        q.breakable; q.pp @etag
      end
    }
  end
end

#to_plain_textObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hpricot/tag.rb', line 55

def to_plain_text
  if self.name == 'br'
    "\n"
  elsif self.name == 'p'
    "\n\n" + super + "\n\n"
  elsif self.name == 'a' and self.has_attribute?('href')
    "#{super} [#{self['href']}]"
  elsif self.name == 'img' and self.has_attribute?('src')
    "[img:#{self['src']}]"
  else
    super
  end
end