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
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 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 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_hyperlink traverses hyperlinks such as HTML href attribute of A element.
It yields Hpricot::Text.
Note that each_hyperlink yields HTML href attribute of BASE element.
# File lib/hpricot/traverse.rb, line 614
614: def each_hyperlink # :yields: text
615: links = []
616: each_hyperlink_attribute {|elem, attr, hyperlink|
617: yield hyperlink
618: }
619: end
each_hyperlink_uri traverses hyperlinks such as HTML href attribute of A element.
It yields Hpricot::Text 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.
# File lib/hpricot/traverse.rb, line 591
591: def each_hyperlink_uri(base_uri=nil) # :yields: hyperlink, uri
592: base_uri = URI.parse(base_uri) if String === base_uri
593: links = []
594: each_hyperlink_attribute {|elem, attr, hyperlink|
595: if %{\{http://www.w3.org/1999/xhtml\}(?:base)\z} =~ elem.name
596: base_uri = URI.parse(hyperlink.to_s)
597: else
598: links << hyperlink
599: end
600: }
601: if base_uri
602: links.each {|hyperlink| yield hyperlink, base_uri + hyperlink.to_s }
603: else
604: links.each {|hyperlink| yield hyperlink, URI.parse(hyperlink.to_s) }
605: end
606: end
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 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 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
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
# 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
# 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 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 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
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
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
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 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
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
# File lib/hpricot/traverse.rb, line 540
540: def each_hyperlink_attribute
541: traverse_element(
542: '{http://www.w3.org/1999/xhtml}a',
543: '{http://www.w3.org/1999/xhtml}area',
544: '{http://www.w3.org/1999/xhtml}link',
545: '{http://www.w3.org/1999/xhtml}img',
546: '{http://www.w3.org/1999/xhtml}object',
547: '{http://www.w3.org/1999/xhtml}q',
548: '{http://www.w3.org/1999/xhtml}blockquote',
549: '{http://www.w3.org/1999/xhtml}ins',
550: '{http://www.w3.org/1999/xhtml}del',
551: '{http://www.w3.org/1999/xhtml}form',
552: '{http://www.w3.org/1999/xhtml}input',
553: '{http://www.w3.org/1999/xhtml}head',
554: '{http://www.w3.org/1999/xhtml}base',
555: '{http://www.w3.org/1999/xhtml}script') {|elem|
556: case elem.name
557: when %{\{http://www.w3.org/1999/xhtml\}(?:base|a|area|link)\z}
558: attrs = ['href']
559: when %{\{http://www.w3.org/1999/xhtml\}(?:img)\z}
560: attrs = ['src', 'longdesc', 'usemap']
561: when %{\{http://www.w3.org/1999/xhtml\}(?:object)\z}
562: attrs = ['classid', 'codebase', 'data', 'usemap']
563: when %{\{http://www.w3.org/1999/xhtml\}(?:q|blockquote|ins|del)\z}
564: attrs = ['cite']
565: when %{\{http://www.w3.org/1999/xhtml\}(?:form)\z}
566: attrs = ['action']
567: when %{\{http://www.w3.org/1999/xhtml\}(?:input)\z}
568: attrs = ['src', 'usemap']
569: when %{\{http://www.w3.org/1999/xhtml\}(?:head)\z}
570: attrs = ['profile']
571: when %{\{http://www.w3.org/1999/xhtml\}(?:script)\z}
572: attrs = ['src', 'for']
573: end
574: attrs.each {|attr|
575: if hyperlink = elem.get_attribute(attr)
576: yield elem, attr, hyperlink
577: end
578: }
579: }
580: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.