Class: Web::Unit::HtmlElem

Inherits:
Object show all
Defined in:
lib/web/unit/htmlelem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, ah = {}) ⇒ HtmlElem

Returns a new instance of HtmlElem.



14
15
16
17
18
19
20
21
# File 'lib/web/unit/htmlelem.rb', line 14

def initialize( tag, ah={} )
  @children = []
  @tag = tag
  @attrs = ah
  @data = ''
  @array = []
  @name = ah['name'] if ah
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def array
  @array
end

#attrsObject (readonly)

Returns the value of attribute attrs.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def children
  @children
end

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def name
  @name
end

#tagObject (readonly)

Returns the value of attribute tag.



12
13
14
# File 'lib/web/unit/htmlelem.rb', line 12

def tag
  @tag
end

Instance Method Details

#append(data) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/web/unit/htmlelem.rb', line 23

def append( data )
  if data.is_a?( HtmlElem )
    @children << data
  else
    @data  << data
  end
  @array << data
end

#extractObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/web/unit/htmlelem.rb', line 32

def extract
  data = ''
  @array.each do |e|
    if e.is_a?( String )
      data << " #{e} "
    elsif e.is_a?( HtmlElem )
      data << e.extract
    end
  end
  data.squeeze( ' ' ).strip
end

#find(tag) ⇒ Object

— HtmlElem#find(tag)

return an Array of HtmlElems, which have ((|tag|)).


56
57
58
59
60
61
62
63
# File 'lib/web/unit/htmlelem.rb', line 56

def find( tag )
  a = []
  a.push self if @tag == tag
  @children.each do |c|
    a = a + c.find( tag )
  end
  a
end

#has?(elem) ⇒ Boolean

— HtmlElem#has?(elem)

return true, when self have ((|elem|)) as child.
otherwise, return false.
((|elem|)) should be a HtmlElem or a String.

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/web/unit/htmlelem.rb', line 99

def has?( elem )
  if elem.kind_of?( HtmlElem )
    @children.include?( elem )
  elsif elem.instance_of?( String )
    @array.include?( elem )
  else
    false
  end
end

#inspectObject



44
45
46
47
48
49
# File 'lib/web/unit/htmlelem.rb', line 44

def inspect
  return "<#toplevel>" if @tag == nil
  as = ''
  attrs.each{ |k,v| as << " #{k}=#{v}" }
  "<#{@tag+as}>:#{data}:"
end

— HtmlElem#print(indent=”) – FOR TEST USE ONLY

print tree structure, from self to lower.


114
115
116
117
118
119
# File 'lib/web/unit/htmlelem.rb', line 114

def print( indent='' ) # not tested :-(
  puts indent + inspect
  @children.each do |c|
    c.print( indent + '  ' )
  end
end

— HtmlElem#readlink(str)

return a Response, gotten with reading a Link have ((|str|)).
raise ElemNotFound exception, when no Links found.

Raises:



85
86
87
88
89
90
# File 'lib/web/unit/htmlelem.rb', line 85

def readlink( str )
  self.find( 'a' ).each do |link|
    return link.read if link.data == str
  end
  raise ElemNotFound, "no Links have '#{str}' as data."
end

#search(data) ⇒ Object

— HtmlElem#search(data)

return an Array of HtmlElems, which have ((|data|)).


70
71
72
73
74
75
76
77
# File 'lib/web/unit/htmlelem.rb', line 70

def search( data )
  a = []
  a.push self if @data == data
  @children.each do |c|
    a = a + c.search( data )
  end
  a
end