Class: Hpricot::Elem

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

Defined Under Namespace

Modules: Trav

Constant Summary collapse

TITLES =
{:title => :h1, :subtitle => :h2, :tagline => :h3, :caption => :h4}

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!, #clear_raw, #html_quote, #if_output

Methods included from Hpricot

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

Constructor Details

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

Returns a new instance of Elem.



43
44
45
46
# File 'lib/ext/hpricot/tag.rb', line 43

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/ext/hpricot/tag.rb', line 48

def attributes
  if raw_attributes
    raw_attributes.inject({}) do |hsh, (k, v)|
      hsh[k] = Hpricot.uxs(v)
      hsh
    end
  else
    {}
  end
end

#attributes_as_htmlObject



89
90
91
92
93
94
95
96
# File 'lib/ext/hpricot/tag.rb', line 89

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#inspect_tree(depth = 0) ⇒ Object



97
98
99
100
# File 'lib/ext/hpricot/tag.rb', line 97

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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ext/hpricot/tag.rb', line 72

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



71
# File 'lib/ext/hpricot/tag.rb', line 71

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/ext/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/ext/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



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ext/hpricot/tag.rb', line 58

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