Load fields and labels directly from the scope

This commit is contained in:
Bryan Helmkamp 2008-11-30 00:47:49 -05:00
parent 40e0272dff
commit 4594a39327
2 changed files with 30 additions and 16 deletions

View File

@ -30,27 +30,14 @@ module Webrat
end
def fields
return @fields if @fields
@fields = []
[SelectField, TextareaField, ButtonField, CheckboxField, PasswordField,
RadioField, FileField, ResetField, TextField, HiddenField].each do |field_class|
@fields += Webrat::XML.xpath_search(@element, *field_class.xpath_search).map do |element|
field = field_class.new(@session, element)
@session.elements[Webrat::XML.xpath_to(element)] = field
field
end
@fields ||= Webrat::XML.xpath_search(@element, *Field.xpath_search).map do |element|
@session.element_to_webrat_element(element)
end
@fields
end
def labels
@labels ||= Webrat::XML.css_search(element, "label").map do |element|
label = Label.new(@session, element)
@session.elements[Webrat::XML.xpath_to(element)] = label
label
@session.element_to_webrat_element(element)
end
end

View File

@ -29,6 +29,8 @@ module Webrat
@session = session
instance_eval(&block) if block_given?
labels
fields
forms # preload
areas # preload
links # preload
@ -340,5 +342,30 @@ module Webrat
end
end
def fields
return @fields if @fields
@fields = []
[SelectField, TextareaField, ButtonField, CheckboxField, PasswordField,
RadioField, FileField, ResetField, TextField, HiddenField].each do |field_class|
@fields += Webrat::XML.xpath_search(dom, *field_class.xpath_search).map do |element|
field = field_class.new(self, element)
@session.elements[Webrat::XML.xpath_to(element)] = field
field
end
end
@fields
end
def labels
@labels ||= Webrat::XML.css_search(dom, "label").map do |element|
label = Label.new(self, element)
@session.elements[Webrat::XML.xpath_to(element)] = label
label
end
end
end
end