Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #918 from teampoltergeist/whitespace
Browse files Browse the repository at this point in the history
Update for Capybara 3 text normalizing changes
  • Loading branch information
twalpole authored Mar 17, 2018
2 parents 345d9b4 + 36c4a4c commit c184563
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.capybara_master
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

gemspec :path => '..'

gem 'capybara', github: 'jnicklas/capybara'
gem 'capybara', github: 'teamcapybara/capybara'
gem 'listen'
gem 'puma'

Expand Down
3 changes: 1 addition & 2 deletions lib/capybara/poltergeist/client/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ class Poltergeist.Browser
@confirm_processes = []
@prompt_responses = []


# Prevent firing `page.onInitialized` event twice. Calling currentUrl
# method before page is actually opened fires this event for the first time.
# The second time will be in the right place after `page.open`
prevUrl = if @currentPage.source is null then 'about:blank' else @currentPage.currentUrl()
prevUrl = if @currentPage.source? then @currentPage.currentUrl() else 'about:blank'

@currentPage.open(url)

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/poltergeist/client/compiled/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Poltergeist.Browser = (function() {
this.processed_modal_messages = [];
this.confirm_processes = [];
this.prompt_responses = [];
prevUrl = this.currentPage.source === null ? 'about:blank' : this.currentPage.currentUrl();
prevUrl = this.currentPage.source != null ? this.currentPage.currentUrl() : 'about:blank';
this.currentPage.open(url);
if (/#/.test(url) && prevUrl.split('#')[0] === url.split('#')[0]) {
this.currentPage.state = 'default';
Expand Down
22 changes: 19 additions & 3 deletions lib/capybara/poltergeist/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ def all_text
end

def visible_text
filter_text command(:visible_text)
if Capybara::VERSION.to_f < 3.0
filter_text command(:visible_text)
else
command(:visible_text).to_s
.gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
.gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
.gsub(/\n+/, "\n")
.tr("\u00a0", ' ')
end
end

def property(name)
Expand Down Expand Up @@ -184,8 +192,16 @@ def as_json(*)

private

def filter_text(text)
Capybara::Helpers.normalize_whitespace(text.to_s)
def filter_text(text, visible = true)
if Capybara::VERSION.to_f < 3
Capybara::Helpers.normalize_whitespace(text.to_s)
else
text.gsub(/[\u200b\u200e\u200f]/, '')
.gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
.gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
.gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
.tr("\u00a0", ' ')
end
end
end
end
33 changes: 25 additions & 8 deletions spec/integration/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,16 @@
expect(@session.evaluate_script(code)).to eq({"a"=>"(cyclic structure)", "b"=>{}, "c"=>{"a"=>"(cyclic structure)"}})
end

it 'returns BR as a space in #text' do
@session.visit '/poltergeist/simple'
expect(@session.find(:css, '#break').text).to eq('Foo Bar')
if Capybara::VERSION.to_f < 3.0
it 'returns BR as a space in #text' do
@session.visit '/poltergeist/simple'
expect(@session.find(:css, '#break').text).to eq('Foo Bar')
end
else
it 'returns BR as "\\n" in #text' do
@session.visit '/poltergeist/simple'
expect(@session.find(:css, '#break').text).to eq("Foo\nBar")
end
end

it 'handles hash changes' do
Expand Down Expand Up @@ -874,12 +881,22 @@
expect(@session.find(:css, '#bar').text).to eq 'bar'
end

it 'gets text stripped whitespace and nbsp' do
expect(@session.find(:css, '#baz').text).to eq 'baz'
end
if Capybara::VERSION.to_f < 3.0
it 'gets text stripped whitespace and nbsp' do
expect(@session.find(:css, '#baz').text).to eq 'baz'
end

it 'gets text stripped whitespace, nbsp and unicode whitespace' do
expect(@session.find(:css, '#qux').text).to eq 'qux'
it 'gets text stripped whitespace, nbsp and unicode whitespace' do
expect(@session.find(:css, '#qux').text).to eq 'qux'
end
else
it 'gets text stripped whitespace and then converts nbsp to space' do
expect(@session.find(:css, '#baz').text).to eq ' baz '
end

it 'gets text stripped whitespace' do
expect(@session.find(:css, '#qux').text).to eq " \u3000 qux \u3000 "
end
end
end

Expand Down

0 comments on commit c184563

Please sign in to comment.