textareas with linefeeds - Fixes #1068

This commit is contained in:
Thomas Walpole 2013-04-25 18:09:34 -07:00
parent 52e0caf66c
commit e41f5e6dca
7 changed files with 37 additions and 6 deletions

View File

@ -18,7 +18,11 @@ module Capybara
attr_reader :native
def initialize(native)
native = Nokogiri::HTML(native) if native.is_a?(String)
native = Nokogiri::HTML(native).tap do |document|
document.xpath('//textarea').each do |textarea|
textarea.content.sub!(/\A\n/,'')
end
end if native.is_a?(String)
@native = native
end
@ -74,7 +78,7 @@ module Capybara
#
def value
if tag_name == 'textarea'
native.content.sub(/\A\n/, '')
native.content
elsif tag_name == 'select'
if native['multiple'] == 'multiple'
native.xpath(".//option[@selected='selected']").map { |option| option[:value] || option.content }

View File

@ -77,7 +77,11 @@ class Capybara::RackTest::Browser
end
def dom
@dom ||= Nokogiri::HTML(html)
@dom ||= Nokogiri::HTML(html).tap do |document|
document.xpath('//textarea').each do |textarea|
textarea.content=textarea.content.sub(/\A\n/,'')
end
end
end
def find(format, selector)

View File

@ -60,7 +60,7 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
merge_param!(params, field['name'].to_s, (option['value'] || option.text).to_s) if option
end
when 'textarea'
merge_param!(params, field['name'].to_s, field.text.to_s)
merge_param!(params, field['name'].to_s, field.text.to_s.gsub(/\n/, "\r\n"))
end
end
merge_param!(params, button[:name], button[:value] || "") if button[:name]

View File

@ -126,7 +126,11 @@ Capybara::SpecHelper.spec '#click_button' do
it "should not serialize a select tag without options" do
@results['tendency'].should be_nil
end
it "should convert lf to cr/lf in submitted textareas" do
@results['newline'].should == "\r\nNew line after and before textarea tag\r\n"
end
it "should not submit disabled fields" do
@results['disabled_text_field'].should be_nil
@results['disabled_textarea'].should be_nil

View File

@ -44,7 +44,7 @@ Capybara::SpecHelper.spec "#fill_in" do
@session.click_button('awesome')
extract_results(@session)['description'].should == 'Texty text'
end
it "should fill in a password field by id" do
@session.fill_in('form_password', :with => 'supasikrit')
@session.click_button('awesome')
@ -56,6 +56,12 @@ Capybara::SpecHelper.spec "#fill_in" do
@session.click_button('awesome')
extract_results(@session)['description'].should == 'is <strong>very</strong> secret!'
end
it "should handle newlines in a textarea", tw: true do
@session.fill_in('form_description', :with => "\nSome text\n")
@session.click_button('awesome')
extract_results(@session)['description'].should == "\r\nSome text\r\n"
end
it "should fill in a field with a custom type" do
@session.fill_in('Schmooo', :with => 'Schmooo is the game')

View File

@ -58,6 +58,11 @@ Capybara::SpecHelper.spec "node" do
@session.find('//textarea[@id="additional_newline"]').value.should == "\nbanana"
end
it "should not swallow leading newlines for set content in textarea" do
@session.find('//textarea[@id="normal"]').set("\nbanana")
@session.find('//textarea[@id="normal"]').value.should == "\nbanana"
end
it "return any HTML content in textarea" do
@session.find('//textarea[1]').set("some <em>html</em> here")
@session.find('//textarea[1]').value.should == "some <em>html</em> here"

View File

@ -135,6 +135,14 @@
<label for="form_description">Description</label></br>
<textarea name="form[description]" id="form_description">Descriptive text goes here</textarea>
<p>
<p>
<label for="form_newline">NewLine</label></br>
<textarea name="form[newline]" id="form_newline">
New line after and before textarea tag
</textarea>
</p>
<p>
<input type="radio" name="form[gender]" value="male" id="gender_male"/>