Module: Hpricot::Container::Trav
- Includes:
- Traverse
- Included in:
- Doc::Trav, Elem::Trav
- Defined in:
- lib/hpricot/traverse.rb,
lib/hpricot/modules.rb,
lib/hpricot/traverse.rb,
lib/hpricot/traverse.rb
Overview
:startdoc:
Instance Method Summary collapse
-
#classes ⇒ Object
Returns a list of CSS classes to which this element belongs.
-
#containers ⇒ Object
Return all children of this node which can contain other nodes.
-
#each_child(&block) ⇒ Object
each_child
iterates over each child. -
#each_child_with_index(&block) ⇒ Object
each_child_with_index
iterates over each child. -
#each_hyperlink ⇒ Object
each_hyperlink
traverses hyperlinks such as HTML href attribute of A element. -
#each_hyperlink_uri(base_uri = nil) ⇒ Object
each_hyperlink_uri
traverses hyperlinks such as HTML href attribute of A element. -
#each_uri(base_uri = nil) ⇒ Object
each_uri
traverses hyperlinks such as HTML href attribute of A element. -
#filter(&block) ⇒ Object
filter
rebuilds the tree without some components. -
#find_element(*names) ⇒ Object
find_element
searches an element which universal name is specified by the arguments. - #get_element_by_id(id) ⇒ Object
- #get_elements_by_tag_name(*a) ⇒ Object
-
#insert_after(nodes, ele) ⇒ Object
Insert
nodes
, an array of HTML elements or a single element, after the nodeele
, a child of the current node. -
#insert_before(nodes, ele) ⇒ Object
Insert
nodes
, an array of HTML elements or a single element, before the nodeele
, a child of the current node. -
#next_sibling ⇒ Object
Returns the container node neighboring this node to the south: just below it.
-
#previous_sibling ⇒ Object
Returns the container node neighboring this node to the north: just above it.
-
#replace_child(old, new) ⇒ Object
Replace
old
, a child of the current node, withnew
node. -
#siblings_at(*pos) ⇒ Object
Puts together an array of neighboring sibling elements based on their proximity to this element.
- #traverse_text_internal(&block) ⇒ Object
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, #to_plain_text, #traverse_element, #traverse_text, #xmldecl?, #xpath
Instance Method Details
#classes ⇒ Object
Returns a list of CSS classes to which this element belongs.
444 445 446 |
# File 'lib/hpricot/traverse.rb', line 444 def classes get_attribute('class').to_s.strip.split(/\s+/) end |
#containers ⇒ Object
Return all children of this node which can contain other nodes. This is a good way to get all HTML elements which aren’t text, comment, doctype or processing instruction nodes.
346 347 348 |
# File 'lib/hpricot/traverse.rb', line 346 def containers children.grep(Container::Trav) end |
#each_child(&block) ⇒ Object
each_child
iterates over each child.
424 425 426 427 |
# File 'lib/hpricot/traverse.rb', line 424 def each_child(&block) # :yields: child_node children.each(&block) nil end |
#each_child_with_index(&block) ⇒ Object
each_child_with_index
iterates over each child.
430 431 432 433 |
# File 'lib/hpricot/traverse.rb', line 430 def each_child_with_index(&block) # :yields: child_node, index children.each_with_index(&block) nil end |
#each_hyperlink ⇒ Object
each_hyperlink
traverses hyperlinks such as HTML href attribute of A element.
It yields Hpricot::Text or Hpricot::Loc.
Note that each_hyperlink
yields HTML href attribute of BASE element.
539 540 541 542 543 544 |
# File 'lib/hpricot/traverse.rb', line 539 def each_hyperlink # :yields: text links = [] each_hyperlink_attribute {|elem, attr, hyperlink| yield hyperlink } end |
#each_hyperlink_uri(base_uri = nil) ⇒ Object
each_hyperlink_uri
traverses hyperlinks such as HTML href attribute of A element.
It yields Hpricot::Text (or Hpricot::Loc) and URI for each hyperlink.
The URI objects are created with a base URI which is given by HTML BASE element or the argument ((|base_uri|)). each_hyperlink_uri
doesn’t yields href of the BASE element.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/hpricot/traverse.rb', line 516 def each_hyperlink_uri(base_uri=nil) # :yields: hyperlink, uri base_uri = URI.parse(base_uri) if String === base_uri links = [] each_hyperlink_attribute {|elem, attr, hyperlink| if %r{\{http://www.w3.org/1999/xhtml\}(?:base)\z}i =~ elem.name base_uri = URI.parse(hyperlink.to_s) else links << hyperlink end } if base_uri links.each {|hyperlink| yield hyperlink, base_uri + hyperlink.to_s } else links.each {|hyperlink| yield hyperlink, URI.parse(hyperlink.to_s) } end end |
#each_uri(base_uri = nil) ⇒ Object
each_uri
traverses hyperlinks such as HTML href attribute of A element.
It yields URI for each hyperlink.
The URI objects are created with a base URI which is given by HTML BASE element or the argument ((|base_uri|)).
553 554 555 |
# File 'lib/hpricot/traverse.rb', line 553 def each_uri(base_uri=nil) # :yields: URI each_hyperlink_uri(base_uri) {|hyperlink, uri| yield uri } end |
#filter(&block) ⇒ Object
filter
rebuilds the tree without some components.
node.filter {|descendant_node| predicate } -> node
loc.filter {|descendant_loc| predicate } -> node
filter
yields each node except top node. If given block returns false, corresponding node is dropped. If given block returns true, corresponding node is retained and inner nodes are examined.
filter
returns an node. It doesn’t return location object even if self is location object.
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
# File 'lib/hpricot/traverse.rb', line 644 def filter(&block) subst = {} each_child_with_index {|descendant, i| if yield descendant if descendant.elem? subst[i] = descendant.filter(&block) else subst[i] = descendant end else subst[i] = nil end } to_node.subst_subnode(subst) end |
#find_element(*names) ⇒ Object
find_element
searches an element which universal name is specified by the arguments. It returns nil if not found.
438 439 440 441 |
# File 'lib/hpricot/traverse.rb', line 438 def find_element(*names) traverse_element(*names) {|e| return e } nil end |
#get_element_by_id(id) ⇒ Object
448 449 450 451 452 453 454 455 |
# File 'lib/hpricot/traverse.rb', line 448 def get_element_by_id(id) traverse_all_element do |ele| if ele.elem? and eid = ele.get_attribute('id') return ele if eid.to_s == id end end nil end |
#get_elements_by_tag_name(*a) ⇒ Object
457 458 459 460 461 462 463 |
# File 'lib/hpricot/traverse.rb', line 457 def get_elements_by_tag_name(*a) list = Elements[] traverse_element(*a.map { |tag| [tag, "{http://www.w3.org/1999/xhtml}#{tag}"] }.flatten) do |e| list << e end list end |
#insert_after(nodes, ele) ⇒ Object
Insert nodes
, an array of HTML elements or a single element, after the node ele
, a child of the current node.
412 413 414 415 416 417 418 419 420 421 |
# File 'lib/hpricot/traverse.rb', line 412 def insert_after(nodes, ele) case nodes when Array nodes.each { |n| insert_after(n, ele) } else reparent nodes idx = children.index(ele) children[idx ? idx + 1 : children.length, 0] = nodes end end |
#insert_before(nodes, ele) ⇒ Object
Insert nodes
, an array of HTML elements or a single element, before the node ele
, a child of the current node.
400 401 402 403 404 405 406 407 408 |
# File 'lib/hpricot/traverse.rb', line 400 def insert_before(nodes, ele) case nodes when Array nodes.each { |n| insert_before(n, ele) } else reparent nodes children[children.index(ele) || 0, 0] = nodes end end |
#next_sibling ⇒ Object
Returns the container node neighboring this node to the south: just below it. By “container” node, I mean: this method does not find text nodes or comments or cdata or any of that. See Hpricot::Traverse#next_node if you need to hunt out all kinds of nodes.
353 354 355 356 |
# File 'lib/hpricot/traverse.rb', line 353 def next_sibling sib = parent.containers sib[sib.index(self) + 1] if parent end |
#previous_sibling ⇒ Object
Returns the container node neighboring this node to the north: just above it. By “container” node, I mean: this method does not find text nodes or comments or cdata or any of that. See Hpricot::Traverse#previous_node if you need to hunt out all kinds of nodes.
361 362 363 364 365 |
# File 'lib/hpricot/traverse.rb', line 361 def previous_sibling sib = parent.containers x = sib.index(self) - 1 sib[x] if sib and x >= 0 end |
#replace_child(old, new) ⇒ Object
Replace old
, a child of the current node, with new
node.
393 394 395 396 |
# File 'lib/hpricot/traverse.rb', line 393 def replace_child(old, new) reparent new children[children.index(old), 1] = [*new] end |
#siblings_at(*pos) ⇒ Object
Puts together an array of neighboring sibling elements based on their proximity to this element.
This method accepts ranges and sets of numbers.
ele.siblings_at(-3..-1, 1..3) # gets three elements before and three after
ele.siblings_at(1, 5, 7) # gets three elements at offsets below the current element
ele.siblings_at(0, 5..6) # the current element and two others
Like the other “sibling” methods, this doesn’t find text and comment nodes. Use nodes_at to include those nodes.
378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/hpricot/traverse.rb', line 378 def siblings_at(*pos) sib = parent.containers i, si = 0, sib.index(self) Elements[* sib.select do |x| sel = case i - si when *pos true end i += 1 sel end ] end |
#traverse_text_internal(&block) ⇒ Object
613 614 615 |
# File 'lib/hpricot/traverse.rb', line 613 def traverse_text_internal(&block) each_child {|c| c.traverse_text_internal(&block) } end |