Included Modules

Hpricot::Container::Trav

Public Instance Methods

classes() click to toggle source

Returns a list of CSS classes to which this element belongs.

     # File lib/hpricot/traverse.rb, line 518
518:     def classes
519:       get_attribute('class').to_s.strip.split(/\s+/)
520:     end
containers() click to toggle source

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.

     # File lib/hpricot/traverse.rb, line 404
404:     def containers
405:       children.grep(Container::Trav)
406:     end
each_child() click to toggle source

each_child iterates over each child.

     # File lib/hpricot/traverse.rb, line 498
498:     def each_child(&block) # :yields: child_node
499:       children.each(&block) if children
500:       nil
501:     end
each_child_with_index() click to toggle source

each_child_with_index iterates over each child.

     # File lib/hpricot/traverse.rb, line 504
504:     def each_child_with_index(&block) # :yields: child_node, index
505:       children.each_with_index(&block) if children
506:       nil
507:     end
each_uri(base_uri=nil) click to toggle source

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|)).

     # File lib/hpricot/traverse.rb, line 628
628:     def each_uri(base_uri=nil) # :yields: URI
629:       each_hyperlink_uri(base_uri) {|hyperlink, uri| yield uri }
630:     end
filter(&block) click to toggle source

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.

     # File lib/hpricot/traverse.rb, line 719
719:     def filter(&block)
720:       subst = {}
721:       each_child_with_index {|descendant, i|
722:         if yield descendant
723:           if descendant.elem?
724:             subst[i] = descendant.filter(&block)
725:           else
726:             subst[i] = descendant
727:           end
728:         else
729:           subst[i] = nil
730:         end
731:       }
732:       to_node.subst_subnode(subst)
733:     end
find_element(*names) click to toggle source

find_element searches an element which universal name is specified by the arguments. It returns nil if not found.

     # File lib/hpricot/traverse.rb, line 512
512:     def find_element(*names)
513:       traverse_element(*names) {|e| return e }
514:       nil
515:     end
following_siblings() click to toggle source

Find sibling elements which follow the current one. Like the other “sibling” methods, this weeds out text and comment nodes.

     # File lib/hpricot/traverse.rb, line 435
435:     def following_siblings() 
436:       sibs = parent.containers 
437:       si = sibs.index(self) + 1 
438:       return Elements[*sibs[si...sibs.length]] 
439:     end
get_element_by_id(id) click to toggle source
     # File lib/hpricot/traverse.rb, line 522
522:     def get_element_by_id(id)
523:       traverse_all_element do |ele|
524:           if ele.elem? and eid = ele.get_attribute('id')
525:               return ele if eid.to_s == id
526:           end
527:       end
528:       nil
529:     end
get_elements_by_tag_name(*a) click to toggle source
     # File lib/hpricot/traverse.rb, line 531
531:     def get_elements_by_tag_name(*a)
532:       list = Elements[]
533:       a.delete("*")
534:       traverse_element(*a.map { |tag| [tag, "{http://www.w3.org/1999/xhtml}#{tag}"] }.flatten) do |e|
535:         list << e if e.elem?
536:       end
537:       list
538:     end
insert_after(nodes, ele) click to toggle source

Insert nodes, an array of HTML elements or a single element, after the node ele, a child of the current node.

     # File lib/hpricot/traverse.rb, line 486
486:     def insert_after(nodes, ele)
487:       case nodes
488:       when Array
489:         nodes.reverse_each { |n| insert_after(n, ele) }
490:       else
491:         reparent nodes
492:         idx = children.index(ele)
493:         children[idx ? idx + 1 : children.length, 0] = nodes
494:       end
495:     end
insert_before(nodes, ele) click to toggle source

Insert nodes, an array of HTML elements or a single element, before the node ele, a child of the current node.

     # File lib/hpricot/traverse.rb, line 474
474:     def insert_before(nodes, ele)
475:       case nodes
476:       when Array
477:         nodes.each { |n| insert_before(n, ele) }
478:       else
479:         reparent nodes
480:         children[children.index(ele) || 0, 0] = nodes
481:       end
482:     end
next_sibling() click to toggle source

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.

     # File lib/hpricot/traverse.rb, line 411
411:     def next_sibling
412:       sib = parent.containers
413:       sib[sib.index(self) + 1] if parent
414:     end
preceding_siblings() click to toggle source

Find all preceding sibling elements. Like the other “sibling” methods, this weeds out text and comment nodes.

     # File lib/hpricot/traverse.rb, line 427
427:     def preceding_siblings() 
428:       sibs = parent.containers 
429:       si = sibs.index(self) 
430:       return Elements[*sibs[0...si]] 
431:     end
previous_sibling() click to toggle source

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.

     # File lib/hpricot/traverse.rb, line 419
419:     def previous_sibling
420:       sib = parent.containers
421:       x = sib.index(self) - 1
422:       sib[x] if sib and x >= 0
423:     end
replace_child(old, new) click to toggle source

Replace old, a child of the current node, with new node.

     # File lib/hpricot/traverse.rb, line 467
467:     def replace_child(old, new)
468:       reparent new
469:       children[children.index(old), 1] = [*new]
470:     end
siblings_at(*pos) click to toggle source

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.

     # File lib/hpricot/traverse.rb, line 452
452:     def siblings_at(*pos)
453:       sib = parent.containers
454:       i, si = 0, sib.index(self)
455:       Elements[*
456:         sib.select do |x|
457:           sel = case i - si when *pos
458:                   true
459:                 end
460:           i += 1
461:           sel
462:         end
463:       ]
464:     end

Private Instance Methods

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.