Permalink
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...
treadway
treadway committed Nov 12, 2007
1 parent 5757ee4 commit 8ed4e51730c2446d4304d428b9e983704e3dd1b3
Showing with 709 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +42 −0 README
  3. +22 −0 Rakefile
  4. +2 −0 init.rb
  5. +144 −0 lib/parent_selector_assertion.rb
  6. +46 −0 lib/responds_to_parent.rb
  7. +318 −0 test/assert_select_parent_test.rb
  8. +115 −0 test/responds_to_parent_test.rb
View
@@ -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
@@ -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
@@ -0,0 +1,2 @@
+ActionController::Base.send :include, RespondsToParent
+require 'parent_selector_assertion'
@@ -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
@@ -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
+
Oops, something went wrong.

0 comments on commit 8ed4e51

Please sign in to comment.