Class: WebUnit::HtmlElem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HtmlElem.



13
14
15
16
17
18
19
20
# File 'lib/webunit/htmlelem.rb', line 13

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.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def array
  @array
end

#attrsObject (readonly)

Returns the value of attribute attrs.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def children
  @children
end

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def name
  @name
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/webunit/htmlelem.rb', line 11

def tag
  @tag
end

Instance Method Details

#append(data) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/webunit/htmlelem.rb', line 22

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

#extractObject



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

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


55
56
57
58
59
60
61
62
# File 'lib/webunit/htmlelem.rb', line 55

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)


98
99
100
101
102
103
104
105
106
# File 'lib/webunit/htmlelem.rb', line 98

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

#inspectObject



43
44
45
46
47
48
# File 'lib/webunit/htmlelem.rb', line 43

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.


113
114
115
116
117
118
# File 'lib/webunit/htmlelem.rb', line 113

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:



84
85
86
87
88
89
# File 'lib/webunit/htmlelem.rb', line 84

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


69
70
71
72
73
74
75
76
# File 'lib/webunit/htmlelem.rb', line 69

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