Module: HTree::Elem::Trav

Includes:
Container::Trav
Included in:
HTree::Elem, Loc
Defined in:
lib/htree/elem.rb,
lib/htree/modules.rb,
lib/htree/traverse.rb,
lib/htree/traverse.rb,
lib/htree/traverse.rb

Instance Method Summary collapse

Methods included from Container::Trav

#each_child, #each_child_with_index, #each_hyperlink, #each_hyperlink_uri, #each_uri, #filter, #find_element, #traverse_element, #traverse_text_internal

Methods included from Traverse

#bogusetag?, #comment?, #doc?, #doctype?, #elem?, #get_subnode, #procins?, #text?, #traverse_text, #xmldecl?

Instance Method Details

#attributesObject

attributes returns attributes as a hash. The hash keys are HTree::Name objects. The hash values are HTree::Text or HTree::Location objects.

p HTree('<a name="xx" href="uu">').root.attributes
# =>
{href=>{text "uu"}, name=>{text "xx"}}

p HTree('<a name="xx" href="uu">').make_loc.root.attributes
# =>
{href=>#<HTree::Location: doc()/a/@href>, name=>#<HTree::Location: doc()/a/@name>}


412
413
414
415
416
417
418
# File 'lib/htree/traverse.rb', line 412

def attributes
  result = {}
  each_attribute {|name, text|
    result[name] = text
  }
  result
end

#each_attrObject



420
421
422
423
424
425
426
# File 'lib/htree/traverse.rb', line 420

def each_attr
  each_attribute {|name, text|
    uname = name.universal_name
    str = text.to_s
    yield uname, str
  }
end

#fetch_attr(uname, *rest, &block) ⇒ Object

call-seq:

elem.fetch_attr(name) -> string or raise IndexError
elem.fetch_attr(name, default) -> string or default
elem.fetch_attr(name) {|uname| default } -> string or default

fetch_attr returns an attribute value as a string.

elem may be an instance of HTree::Elem or a location points to it.



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/htree/traverse.rb', line 463

def fetch_attr(uname, *rest, &block)
  if 1 < rest.length
    raise ArgumentError, "wrong number of arguments (#{1+rest.length} for 2)"
  end
  if !rest.empty? && block_given?
    raise ArgumentError, "block supersedes default value argument"
  end
  uname = uname.universal_name if uname.respond_to? :universal_name
  return update_attribute_hash.fetch(uname) {
    if block_given?
      return yield(uname)
    elsif !rest.empty?
      return rest[0]
    else
      raise IndexError, "attribute not found: #{uname.inspect}"
    end
  }.to_s
end

#fetch_attribute(uname, *rest, &block) ⇒ Object

call-seq:

elem.fetch_attribute(name) -> text or raise IndexError
elem.fetch_attribute(name, default) -> text or default
elem.fetch_attribute(name) {|uname| default } -> text or default

fetch_attribute returns an attribute value as a text.

elem may be an instance of HTree::Elem or a location points to it.



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/htree/traverse.rb', line 436

def fetch_attribute(uname, *rest, &block)
  if 1 < rest.length
    raise ArgumentError, "wrong number of arguments (#{1+rest.length} for 2)"
  end
  if !rest.empty? && block_given?
    raise ArgumentError, "block supersedes default value argument"
  end
  uname = uname.universal_name if uname.respond_to? :universal_name
  return update_attribute_hash.fetch(uname) {
    if block_given?
      return yield(uname)
    elsif !rest.empty?
      return rest[0]
    else
      raise IndexError, "attribute not found: #{uname.inspect}"
    end
  }
end

#get_attr(uname) ⇒ Object



487
488
489
490
491
492
493
# File 'lib/htree/traverse.rb', line 487

def get_attr(uname)
  if text = update_attribute_hash[uname]
    text.to_s
  else
    nil 
  end
end

#get_attribute(uname) ⇒ Object



482
483
484
485
# File 'lib/htree/traverse.rb', line 482

def get_attribute(uname)
  uname = uname.universal_name if uname.respond_to? :universal_name
  update_attribute_hash[uname]
end

#nameObject

name returns the universal name of the element as a string.

p HTree('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.name
# =>
"{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF"


391
# File 'lib/htree/traverse.rb', line 391

def name() element_name.universal_name end

#qualified_nameObject

qualified_name returns the qualified name of the element as a string.

p HTree('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.qualified_name
# =>
"rdf:RDF"


398
# File 'lib/htree/traverse.rb', line 398

def qualified_name() element_name.qualified_name end

#traverse_all_element {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



188
189
190
191
# File 'lib/htree/traverse.rb', line 188

def traverse_all_element(&block)
  yield self
  children.each {|c| c.traverse_all_element(&block) }
end

#traverse_some_element(name_set) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



206
207
208
209
# File 'lib/htree/traverse.rb', line 206

def traverse_some_element(name_set, &block)
  yield self if name_set.include? self.name
  children.each {|c| c.traverse_some_element(name_set, &block) }
end