Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Import from http://sean.treadway.info/svn/plugins/responds_to_parent

…r215 - moving svn hosting to code.google.com

git-svn-id: http://responds-to-parent.googlecode.com/svn/trunk@2 34742bed-b83e-0410-92b4-45230d45b441
  • Loading branch information...
commit 8ed4e51730c2446d4304d428b9e983704e3dd1b3 1 parent 5757ee4
treadway authored
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Sean Treadway
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
42 README
@@ -0,0 +1,42 @@
+RespondsToParent
+================
+
+Adds responds_to_parent to your controller to respond to the parent document of your page.
+Make Ajaxy file uploads by posting the form to a hidden iframe, and respond with
+RJS to the parent window.
+
+Example
+=======
+
+Controller:
+
+ class Test < ActionController::Base
+ def main
+ end
+
+ def form_action
+ # Do stuff with params[:uploaded_file]
+
+ responds_to_parent do
+ render :update do |page|
+ page << "alert($('stuff').innerHTML)"
+ end
+ end
+ end
+ end
+
+main.rhtml:
+
+ <html>
+ <body>
+ <div id="stuff">Here is some stuff</div>
+
+ <form target="frame" action="form_action">
+ <input type="file" name="uploaded_file"/>
+ <input type="submit"/>
+ </form>
+
+ <iframe id='frame' name="frame"></iframe>
+ </body>
+ </html>
+
View
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the responds_to_parent plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the responds_to_parent plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'RespondsToParent'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
2  init.rb
@@ -0,0 +1,2 @@
+ActionController::Base.send :include, RespondsToParent
+require 'parent_selector_assertion'
View
144 lib/parent_selector_assertion.rb
@@ -0,0 +1,144 @@
+module ActionController
+ module Assertions
+ module SelectorAssertions
+ # :call-seq:
+ # assert_select_parent()
+ # assert_select_parent() { |script| ... }
+ #
+ # Selects JavaScript that is generated for the `parent' window.
+ #
+ # Without a block, #assert_select_parent asserts that the response
+ # is generated by responds_to_parent.
+ #
+ # With a block, #assert_select_parent selects script that is supposed
+ # to be evaluated in the parent window and passes it to the block.
+ # Typically #assert_select_rjs is used in the block.
+ def assert_select_parent(*args, &block)
+ wrapper_re_str = Regexp.escape("with(window.parent) { setTimeout(function() { window.eval('") +
+ "(.*)" +
+ Regexp.escape("'); loc.replace('about:blank'); }, 1) }")
+ match = @response.body.match(Regexp.new(wrapper_re_str))
+
+ if match
+ escaped_js = match[1]
+ unescaped_js = escaped_js.
+ gsub(%r!</scr"\+"ipt>!, '</script>').
+ gsub(/\\(\'|\")/, '\1').
+ gsub(/((?:^|[^\\])(?:\\\\)*)\\n/, "\\1\n"). # replace `n' with odd number of backslash.
+ gsub(/\\\\/, '\\')
+ @response.body = unescaped_js # assert_select_rjs refers @response.body.
+
+ if block_given?
+ begin
+ in_scope, @selected = @selected, unescaped_js
+ yield unescaped_js
+ ensure
+ @selected = in_scope
+ end
+ end
+ unescaped_js
+ else
+ # doesn't seem a responds_to_parent content.
+ flunk args.shift || "No content for the parent window."
+ end
+ end
+ end
+ end
+end
+
+module ActionController
+ module Assertions
+ module SelectorAssertions
+ # :call-seq:
+ # assert_select_parent()
+ # assert_select_parent() { |script| ... }
+ #
+ # Selects JavaScript that is generated for the `parent' window.
+ #
+ # Without a block, #assert_select_parent asserts that the response
+ # is generated by responds_to_parent.
+ #
+ # With a block, #assert_select_parent selects script that is supposed
+ # to be evaluated in the parent window and passes it to the block.
+ # Typically #assert_select_rjs is used in the block.
+ def assert_select_parent(*args, &block)
+ wrapper_re_str = Regexp.escape("with(window.parent) { setTimeout(function() { window.eval('") +
+ "(.*)" +
+ Regexp.escape("'); loc.replace('about:blank'); }, 1) }")
+ match = @response.body.match(Regexp.new(wrapper_re_str))
+
+ if match
+ escaped_js = match[1]
+ unescaped_js = escaped_js.
+ gsub(%r!</scr"\+"ipt>!, '</script>').
+ gsub(/\\(\'|\")/, '\1').
+ gsub(/((?:^|[^\\])(?:\\\\)*)\\n/, "\\1\n"). # replace `n' with odd number of backslash.
+ gsub(/\\\\/, '\\')
+ @response.body = unescaped_js # assert_select_rjs refers @response.body.
+
+ if block_given?
+ begin
+ in_scope, @selected = @selected, unescaped_js
+ yield unescaped_js
+ ensure
+ @selected = in_scope
+ end
+ end
+ unescaped_js
+ else
+ # doesn't seem a responds_to_parent content.
+ flunk args.shift || "No content for the parent window."
+ end
+ end
+ end
+ end
+end
+
+module ActionController
+ module Assertions
+ module SelectorAssertions
+ # :call-seq:
+ # assert_select_parent()
+ # assert_select_parent() { |script| ... }
+ #
+ # Selects JavaScript that is generated for the `parent' window.
+ #
+ # Without a block, #assert_select_parent asserts that the response
+ # is generated by responds_to_parent.
+ #
+ # With a block, #assert_select_parent selects script that is supposed
+ # to be evaluated in the parent window and passes it to the block.
+ # Typically #assert_select_rjs is used in the block.
+ def assert_select_parent(*args, &block)
+ wrapper_re_str = Regexp.escape("with(window.parent) { setTimeout(function() { window.eval('") +
+ "(.*)" +
+ Regexp.escape("'); loc.replace('about:blank'); }, 1) }")
+ match = @response.body.match(Regexp.new(wrapper_re_str))
+
+ if match
+ escaped_js = match[1]
+ unescaped_js = escaped_js.
+ gsub(%r!</scr"\+"ipt>!, '</script>').
+ gsub(/\\(\'|\")/, '\1').
+ gsub(/((?:^|[^\\])(?:\\\\)*)\\n/, "\\1\n"). # replace `n' with odd number of backslash.
+ gsub(/\\\\/, '\\')
+ @response.body = unescaped_js # assert_select_rjs refers @response.body.
+
+ if block_given?
+ begin
+ in_scope, @selected = @selected, unescaped_js
+ yield unescaped_js
+ ensure
+ @selected = in_scope
+ end
+ end
+ unescaped_js
+ else
+ # doesn't seem a responds_to_parent content.
+ flunk args.shift || "No content for the parent window."
+ end
+ end
+ end
+ end
+end
+
View
46 lib/responds_to_parent.rb
@@ -0,0 +1,46 @@
+# Module containing the methods useful for child IFRAME to parent window communication
+module RespondsToParent
+
+ # Executes the response body as JavaScript in the context of the parent window.
+ # Use this method of you are posting a form to a hidden IFRAME or if you would like
+ # to use IFRAME base RPC.
+ def responds_to_parent(&block)
+ yield
+
+ if performed?
+ # We're returning HTML instead of JS or XML now
+ response.headers['Content-Type'] = 'text/html; charset=UTF-8'
+
+ # Either pull out a redirect or the request body
+ script = if location = erase_redirect_results
+ "document.location.href = #{location.to_s.inspect}"
+ else
+ response.body
+ end
+
+ # Escape quotes, linebreaks and slashes, maintaining previously escaped slashes
+ # Suggestions for improvement?
+ script = (script || '').
+ gsub('\\', '\\\\\\').
+ gsub(/\r\n|\r|\n/, '\\n').
+ gsub(/['"]/, '\\\\\&').
+ gsub('</script>','</scr"+"ipt>')
+
+ # Clear out the previous render to prevent double render
+ erase_results
+
+ # Eval in parent scope and replace document location of this frame
+ # so back button doesn't replay action on targeted forms
+ # loc = document.location to be set after parent is updated for IE
+ # with(window.parent) - pull in variables from parent window
+ # setTimeout - scope the execution in the windows parent for safari
+ # window.eval - legal eval for Opera
+ render :text => "<html><body><script type='text/javascript' charset='utf-8'>
+ var loc = document.location;
+ with(window.parent) { setTimeout(function() { window.eval('#{script}'); loc.replace('about:blank'); }, 1) }
+ </script></body></html>"
+ end
+ end
+ alias respond_to_parent responds_to_parent
+end
+
View
318 test/assert_select_parent_test.rb
@@ -0,0 +1,318 @@
+require File.dirname(__FILE__) + '/../../../../config/environment'
+require 'test/unit'
+require 'test_help'
+
+class AssertSelectParentTest < Test::Unit::TestCase
+ class AssertSelectParentController < ActionController::Base
+ def response_with=(content)
+ @content = content
+ end
+
+ def response_with(&block)
+ @update = block
+ end
+
+ def rjs
+ responds_to_parent do
+ render :update do |page|
+ @update.call page
+ end
+ end
+ @update = nil
+ end
+
+ def text
+ responds_to_parent do
+ render :text => @content, :layout => false
+ end
+ @content = nil
+ end
+
+ def not_respond_to_parent
+ render :nothing => true
+ end
+
+ def rescue_action(e)
+ raise e
+ end
+ end
+
+ def setup
+ @controller = AssertSelectParentController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_basic
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ found = false
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#1"
+ found = true
+ end
+ end
+ assert found
+ end
+
+ def test_bubble_up_failure
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ assert_raise(Test::Unit::AssertionFailedError) do
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#nonexistent"
+ end
+ end
+ end
+ end
+
+ def test_fail_if_no_content_for_parent
+ get :not_respond_to_parent
+ assert_raise(Test::Unit::AssertionFailedError) { assert_select_parent }
+ end
+
+ def test_quotes
+ do_test_with_text %(single' double" escaped\\' escaped\\" doubleescaped\\\\\\' doubleescaped\\\\\\")
+ end
+
+ def test_new_line
+ do_test_with_text "line1\nline2\\nline2\\\nline3\\\\nline3\\\\\nline4\\\\\\nline4"
+ end
+
+ protected
+ def render_rjs(&block)
+ @controller.response_with &block
+ get :rjs
+ end
+
+ def render_text(text)
+ @controller.response_with = text
+ get :text
+ end
+
+ def do_test_with_text(text)
+ render_text text
+
+ assert_select_parent do |text_for_parent|
+ assert_equal text, text_for_parent
+ end
+ end
+end
+require File.dirname(__FILE__) + '/../../../../config/environment'
+require 'test/unit'
+require 'test_help'
+
+class AssertSelectParentTest < Test::Unit::TestCase
+ class AssertSelectParentController < ActionController::Base
+ def response_with=(content)
+ @content = content
+ end
+
+ def response_with(&block)
+ @update = block
+ end
+
+ def rjs
+ responds_to_parent do
+ render :update do |page|
+ @update.call page
+ end
+ end
+ @update = nil
+ end
+
+ def text
+ responds_to_parent do
+ render :text => @content, :layout => false
+ end
+ @content = nil
+ end
+
+ def not_respond_to_parent
+ render :nothing => true
+ end
+
+ def rescue_action(e)
+ raise e
+ end
+ end
+
+ def setup
+ @controller = AssertSelectParentController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_basic
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ found = false
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#1"
+ found = true
+ end
+ end
+ assert found
+ end
+
+ def test_bubble_up_failure
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ assert_raise(Test::Unit::AssertionFailedError) do
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#nonexistent"
+ end
+ end
+ end
+ end
+
+ def test_fail_if_no_content_for_parent
+ get :not_respond_to_parent
+ assert_raise(Test::Unit::AssertionFailedError) { assert_select_parent }
+ end
+
+ def test_quotes
+ do_test_with_text %(single' double" escaped\\' escaped\\" doubleescaped\\\\\\' doubleescaped\\\\\\")
+ end
+
+ def test_new_line
+ do_test_with_text "line1\nline2\\nline2\\\nline3\\\\nline3\\\\\nline4\\\\\\nline4"
+ end
+
+ protected
+ def render_rjs(&block)
+ @controller.response_with &block
+ get :rjs
+ end
+
+ def render_text(text)
+ @controller.response_with = text
+ get :text
+ end
+
+ def do_test_with_text(text)
+ render_text text
+
+ assert_select_parent do |text_for_parent|
+ assert_equal text, text_for_parent
+ end
+ end
+end
+require File.dirname(__FILE__) + '/../../../../config/environment'
+require 'test/unit'
+require 'test_help'
+
+class AssertSelectParentTest < Test::Unit::TestCase
+ class AssertSelectParentController < ActionController::Base
+ def response_with=(content)
+ @content = content
+ end
+
+ def response_with(&block)
+ @update = block
+ end
+
+ def rjs
+ responds_to_parent do
+ render :update do |page|
+ @update.call page
+ end
+ end
+ @update = nil
+ end
+
+ def text
+ responds_to_parent do
+ render :text => @content, :layout => false
+ end
+ @content = nil
+ end
+
+ def not_respond_to_parent
+ render :nothing => true
+ end
+
+ def rescue_action(e)
+ raise e
+ end
+ end
+
+ def setup
+ @controller = AssertSelectParentController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_basic
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ found = false
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#1"
+ found = true
+ end
+ end
+ assert found
+ end
+
+ def test_bubble_up_failure
+ render_rjs do |page|
+ page.replace "test", "<div id=\"1\">foo</div>"
+ end
+
+ assert_raise(Test::Unit::AssertionFailedError) do
+ assert_select_parent do
+ assert_select_rjs do
+ assert_select "#nonexistent"
+ end
+ end
+ end
+ end
+
+ def test_fail_if_no_content_for_parent
+ get :not_respond_to_parent
+ assert_raise(Test::Unit::AssertionFailedError) { assert_select_parent }
+ end
+
+ def test_quotes
+ do_test_with_text %(single' double" escaped\\' escaped\\" doubleescaped\\\\\\' doubleescaped\\\\\\")
+ end
+
+ def test_new_line
+ do_test_with_text "line1\nline2\\nline2\\\nline3\\\\nline3\\\\\nline4\\\\\\nline4"
+ end
+
+ protected
+ def render_rjs(&block)
+ @controller.response_with &block
+ get :rjs
+ end
+
+ def render_text(text)
+ @controller.response_with = text
+ get :text
+ end
+
+ def do_test_with_text(text)
+ render_text text
+
+ assert_select_parent do |text_for_parent|
+ assert_equal text, text_for_parent
+ end
+ end
+end
View
115 test/responds_to_parent_test.rb
@@ -0,0 +1,115 @@
+require File.dirname(__FILE__) + '/../../../../config/environment'
+require 'test/unit'
+require 'test_help'
+
+class IFrameController < ActionController::Base
+ def normal
+ render :update do |page|
+ page.alert "foo"
+ end
+ end
+
+ def aliased
+ respond_to_parent do
+ render :text => 'woot'
+ end
+ end
+
+ def redirect
+ responds_to_parent do
+ redirect_to '/another/place'
+ end
+ end
+
+ def no_block
+ responds_to_parent
+ end
+
+ def empty_render
+ responds_to_parent do
+ end
+
+ render :text => ''
+ end
+
+ def quotes
+ responds_to_parent do
+ render :text => %(single' double" qs\\' qd\\" escaped\\\' doubleescaped\\\\')
+ end
+ end
+
+ def newlines
+ responds_to_parent do
+ render :text => "line1\nline2\\nline2"
+ end
+ end
+
+ def update
+ responds_to_parent do
+ render :update do |page|
+ page.alert 'foo'
+ page.alert 'bar'
+ end
+ end
+ end
+
+ def rescue_action(e)
+ raise e
+ end
+end
+
+class RespondsToParentTest < Test::Unit::TestCase
+ def setup
+ @controller = IFrameController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_normal
+ get :normal
+ assert_match /alert\("foo"\)/, @response.body
+ assert_no_match /window\.parent/, @response.body
+ end
+
+ def test_quotes_should_be_escaped
+ render :quotes
+ assert_match %r{eval\('single\\' double\\" qs\\\\\\' qd\\\\\\" escaped\\\\\\' doubleescaped\\\\\\\\\\'}, @response.body
+ end
+
+ def test_newlines_should_be_escaped
+ render :newlines
+ assert_match %r{eval\('line1\\nline2\\\\nline2'\)}, @response.body
+ end
+
+ def test_no_block_should_raise
+ assert_raises LocalJumpError do
+ get :no_block
+ end
+ end
+
+ def test_empty_render_should_not_expand_javascript
+ get :empty_render
+ assert_equal '', @response.body
+ end
+
+ def test_update_should_perform_combined_rjs
+ render :update
+ assert_match /alert\(\\"foo\\"\);\\nalert\(\\"bar\\"\)/, @response.body
+ end
+
+ def test_aliased_method_should_not_raise
+ assert_nothing_raised do
+ render :aliased
+ assert_match /eval\('woot'\)/, @response.body
+ end
+ end
+
+protected
+
+ def render(action)
+ get action
+ assert_match /<script type='text\/javascript'/, @response.body
+ assert_match /with\(window\.parent\)/, @response.body
+ assert_match /loc\.replace\('about:blank'\)/, @response.body
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.