Class: Hpricot::Elem

Inherits:
Elem show all
Includes:
Container, Trav
Defined in:
lib/hpricot/tag.rb,
lib/hpricot/inspect.rb,
lib/hpricot/modules.rb,
lib/hpricot/modules.rb,
ext/hpricot_scan/hpricot_scan.c

Defined Under Namespace

Modules: Trav

Constant Summary

Constants included from Hpricot

AttrCore, AttrEvents, AttrFocus, AttrHAlign, AttrI18n, AttrVAlign, Attrs, ElementContent, ElementExclusions, ElementInclusions, FORM_TAGS, NamedCharacters, NamedCharactersPattern, OmittedAttrName, SELF_CLOSING_TAGS

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, #following_siblings, #get_element_by_id, #get_elements_by_tag_name, #insert_after, #insert_before, #next_sibling, #preceding_siblings, #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, #following, #get_subnode, #html, #index, #inner_html=, #inner_text, #make, #next, #node_position, #nodes_at, #position, #preceding, #previous, #procins?, #search, #swap, #text?, #to_html, #to_original_html, #traverse_element, #traverse_text, #xmldecl?, #xpath

Methods included from Node

#altered!, #html_quote, #if_output

Methods included from Hpricot

XML, build, css, make, parse, scan, uxs, xchr, xs

Constructor Details

#initialize(tag, attrs = nil, children = nil, etag = nil) ⇒ Elem

Returns a new instance of Elem.



71
72
73
74
# File 'lib/hpricot/tag.rb', line 71

def initialize tag, attrs = nil, children = nil, etag = nil
  self.name, self.raw_attributes, self.children, self.etag =
    tag, attrs, children, etag
end

Instance Method Details

#attributesObject



76
77
78
# File 'lib/hpricot/tag.rb', line 76

def attributes
  Attributes.new self
end

#attributes_as_htmlObject



110
111
112
113
114
115
116
117
# File 'lib/hpricot/tag.rb', line 110

def attributes_as_html
  if raw_attributes
    raw_attributes.map do |aname, aval|
      " #{aname}" +
        (aval ? "=#{html_quote aval}" : "")
    end.join
  end
end

#clear_rawObject

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?; children.nil? or children.empty? end

#inspect_tree(depth = 0) ⇒ Object



118
119
120
121
# File 'lib/hpricot/tag.rb', line 118

def inspect_tree(depth = 0)
  %{#{" " * depth}} + name + "\n" +
    (children ? children.map { |x| x.inspect_tree(depth + 1) }.join : "")
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hpricot/tag.rb', line 93

def output(out, opts = {})
  out <<
    if_output(opts) do
      "<#{name}#{attributes_as_html}" +
        ((empty? and not etag) ? " /" : "") +
        ">"
    end
  if children
    children.each { |n| n.output(out, opts) }
  end
  if opts[:preserve]
    out << etag if etag
  elsif etag or !empty?
    out << "</#{name}>"
  end
  out
end

#pathnameObject



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

def pathname; self.name end

#pretty_print(q) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hpricot/inspect.rb', line 38

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

#pretty_print_stag(q) ⇒ Object



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

def pretty_print_stag(q)
  q.group(1, '<', '>') {
    q.text name

    if raw_attributes
      raw_attributes.each {|n, t|
        q.breakable
        if t
          q.text "#{n}=\"#{Hpricot.uxs(t)}\""
        else
          q.text n
        end
      }
    end
  }
end

#to_plain_textObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hpricot/tag.rb', line 79

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