diff --git a/README.md b/README.md index 920176f..4529d00 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,9 @@ makes debugging easier). Running `rake autocompile` will watch the enables `:hover` effects etc to trigger before the click happens, which can affect the click in some cases. [Issue #120] +* Don't blow up when `evaluate_script` is called with a cyclic + structure. + ### 0.7.0 ### #### Features #### diff --git a/lib/capybara/poltergeist/client/agent.coffee b/lib/capybara/poltergeist/client/agent.coffee index 2ad3071..1cf10f4 100644 --- a/lib/capybara/poltergeist/client/agent.coffee +++ b/lib/capybara/poltergeist/client/agent.coffee @@ -14,11 +14,17 @@ class PoltergeistAgent { error: { message: error.toString(), stack: error.stack } } @stringify: (object) -> - JSON.stringify object, (key, value) -> - if Array.isArray(this[key]) - return this[key] + try + JSON.stringify object, (key, value) -> + if Array.isArray(this[key]) + return this[key] + else + return value + catch error + if error instanceof TypeError + '"(cyclic structure)"' else - return value + throw error pushWindow: (new_window) -> @windows.push(new_window) diff --git a/lib/capybara/poltergeist/client/compiled/agent.js b/lib/capybara/poltergeist/client/compiled/agent.js index e12ca12..750e7a9 100644 --- a/lib/capybara/poltergeist/client/compiled/agent.js +++ b/lib/capybara/poltergeist/client/compiled/agent.js @@ -25,13 +25,21 @@ PoltergeistAgent = (function() { }; PoltergeistAgent.stringify = function(object) { - return JSON.stringify(object, function(key, value) { - if (Array.isArray(this[key])) { - return this[key]; + try { + return JSON.stringify(object, function(key, value) { + if (Array.isArray(this[key])) { + return this[key]; + } else { + return value; + } + }); + } catch (error) { + if (error instanceof TypeError) { + return '"(cyclic structure)"'; } else { - return value; + throw error; } - }); + } }; PoltergeistAgent.prototype.pushWindow = function(new_window) { diff --git a/spec/integration/session_spec.rb b/spec/integration/session_spec.rb index 009d30c..9bed932 100644 --- a/spec/integration/session_spec.rb +++ b/spec/integration/session_spec.rb @@ -293,5 +293,16 @@ describe Capybara::Session do @session.status_code.should == 402 end end + + it 'ignores cyclic structure errors in evaluate_script' do + code = <<-CODE + (function() { + var a = {} + a.a = a + return a + })() + CODE + @session.evaluate_script(code).should == "(cyclic structure)" + end end end