Class: Hpricot::Elem
- Defined in:
- lib/hpricot/tag.rb,
lib/hpricot/nodes.rb,
lib/hpricot/inspect.rb,
lib/hpricot/modules.rb,
lib/hpricot/modules.rb
Defined Under Namespace
Modules: Trav
Constant Summary collapse
- VOID_MODELS =
Content models that mean "this element takes no children".
:EMPTY is HTML mode's marker, from ElementContent. :VOID is the html_void option's marker -- same nesting behaviour, but serialized the way the source wrote it rather than always with an XHTML self-closing slash.
[:EMPTY, :VOID].freeze
Constants included from Hpricot
AttrCore, AttrEvents, AttrFocus, AttrHAlign, AttrI18n, AttrVAlign, Attrs, ElementContent, ElementExclusions, ElementInclusions, FORM_TAGS, NamedCharacters, NamedCharactersPattern, OmittedAttrName, SELF_CLOSING_TAGS
Instance Attribute Summary
Attributes inherited from ElemNode
#allowed, #children, #etag, #name, #parent, #raw_attributes, #raw_string, #tagno
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes_as_html ⇒ Object
- #clear_raw ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(tag, attrs = nil, children = nil, etag = nil) ⇒ Elem
constructor
A new instance of Elem.
- #inspect_tree(depth = 0) ⇒ Object
- #output(out, opts = {}) ⇒ Object
- #pathname ⇒ Object
- #pretty_print(q) ⇒ Object
- #pretty_print_stag(q) ⇒ Object
- #to_plain_text ⇒ Object
-
#void_written_bare? ⇒ Boolean
True for an html_void element whose source had no self-closing slash.
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
Constructor Details
#initialize(tag, attrs = nil, children = nil, etag = nil) ⇒ Elem
Returns a new instance of Elem.
94 95 96 97 |
# File 'lib/hpricot/tag.rb', line 94 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
#attributes ⇒ Object
99 100 101 |
# File 'lib/hpricot/tag.rb', line 99 def attributes Attributes.new self end |
#attributes_as_html ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/hpricot/tag.rb', line 138 def attributes_as_html if raw_attributes raw_attributes.map do |aname, aval| " #{aname}" + (aval ? "=#{html_quote aval}" : "") end.join end end |
#clear_raw ⇒ Object
146 147 148 |
# File 'lib/hpricot/nodes.rb', line 146 def clear_raw self.raw_string = nil end |
#empty? ⇒ Boolean
98 |
# File 'lib/hpricot/tag.rb', line 98 def empty?; children.nil? or children.empty? end |
#inspect_tree(depth = 0) ⇒ Object
146 147 148 149 |
# File 'lib/hpricot/tag.rb', line 146 def inspect_tree(depth = 0) %{#{" " * depth}} + name + "\n" + (children ? children.map { |x| x.inspect_tree(depth + 1) }.join : "") end |
#output(out, opts = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/hpricot/tag.rb', line 116 def output(out, opts = {}) out << if_output(opts) do "<#{name}#{attributes_as_html}" + ((empty? and not etag and not ) ? " /" : "") + ">" end if children children.each { |n| n.output(out, opts) } end if opts[:preserve] out << etag if etag elsif (etag or !empty?) and not VOID_MODELS.include?(allowed) # A void element never gets an end tag. `allowed` is :EMPTY only for # HTML's void elements -- always in HTML mode, and in XML mode when the # caller passed :html_void -- so this is a no-op for an ordinary XML # document, where a <br> element is just an element and must keep its # </br>. out << "</#{name}>" end out end |
#pathname ⇒ Object
115 |
# File 'lib/hpricot/tag.rb', line 115 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_text ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/hpricot/tag.rb', line 102 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 |
#void_written_bare? ⇒ Boolean
True for an html_void element whose source had no self-closing slash.
These elements sit inside translatable text, so rewriting '
' as
'
' would change the string a translator sees and invalidate
existing translations -- and rewriting '
' as '
' would equally modify a document that was self-closing to begin with. Neither normalisation is acceptable, so the source spelling decides. An element built in Ruby rather than parsed has no raw_string and keeps the default.
90 91 92 |
# File 'lib/hpricot/tag.rb', line 90 def allowed == :VOID && !raw_string.nil? && !raw_string.end_with?('/>') end |