913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
# File 'lib/formkeeper.rb', line 913
def fill_up(str, params)
doc = Hpricot(str)
(doc/"input").each do |elem|
name = elem[:name]
next if name.nil?
name = name.sub(/\[\]$/, '');
next unless params.has_key?(name.to_s)
type = elem[:type]
next if type.nil?
next unless @input_elem_fill_methods.has_key?(type.to_sym)
@input_elem_fill_methods[type.to_sym].call(elem, params[name.to_s])
end
(doc/"select").each do |select_elem|
name = select_elem[:name]
next if name.nil?
name = name.sub(/\[\]$/, '');
next unless params.has_key?(name.to_s)
param = params[name.to_s]
multiple = select_elem.has_attribute?(:multiple)
can_select_more = true
(select_elem/"option").each do |option_elem|
value = option_elem[:value]
next if value.nil? or value.empty?
case param
when Array
if can_select_more and param.include?(value)
option_elem[:selected] = "selected"
can_select_more = false unless multiple
else
option_elem.remove_attribute(:selected)
end
when String
if can_select_more and param == value
option_elem[:selected] = "selected"
can_select_more = false unless multiple
else
option_elem.remove_attribute(:selected)
end
else
if can_select_more and param.to_s == value
option_elem[:selected] = "selected"
can_select_more = false unless multiple
else
option_elem.remove_attribute(:selected)
end
end
end
end
(doc/"textarea").each do |elem|
name = elem[:name]
next if name.nil?
next unless params.has_key?(name.to_s)
param = params[name.to_s]
if param.kind_of?(Array)
elem.innerHTML = Rack::Utils::escape_html(param[0])
else
elem.innerHTML = Rack::Utils::escape_html(param)
end
end
doc.to_html
end
|