Skip to content
This repository
Browse code

Prototype helpers should generate Element.insert instead of Insertion…

….new, which has been deprecated in Prototype 1.6.
  • Loading branch information...
commit eaab895f83276674891227c656df9b4cebc50200 1 parent c8e80f6
Emilio Tagua authored July 22, 2008 jeremy committed July 30, 2008
26  actionpack/lib/action_controller/assertions/selector_assertions.rb
@@ -407,6 +407,7 @@ def assert_select_rjs(*args, &block)
407 407
 
408 408
           if rjs_type == :insert
409 409
             arg = args.shift
  410
+            position  = arg
410 411
             insertion = "insert_#{arg}".to_sym
411 412
             raise ArgumentError, "Unknown RJS insertion type #{arg}" unless RJS_STATEMENTS[insertion]
412 413
             statement = "(#{RJS_STATEMENTS[insertion]})"
@@ -418,6 +419,7 @@ def assert_select_rjs(*args, &block)
418 419
         else
419 420
           statement = "#{RJS_STATEMENTS[:any]}"
420 421
         end
  422
+        position ||= Regexp.new(RJS_INSERTIONS.join('|'))
421 423
 
422 424
         # Next argument we're looking for is the element identifier. If missing, we pick
423 425
         # any element.
@@ -434,9 +436,14 @@ def assert_select_rjs(*args, &block)
434 436
               Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
435 437
             when :remove, :show, :hide, :toggle
436 438
               Regexp.new("#{statement}\\(\"#{id}\"\\)")
437  
-            else
438  
-              Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
439  
-          end
  439
+            when :replace, :replace_html
  440
+              Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)")
  441
+            when :insert, :insert_html
  442
+              Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);")
  443
+             else
  444
+              Regexp.union(Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)"),
  445
+                Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);"))
  446
+           end
440 447
 
441 448
         # Duplicate the body since the next step involves destroying it.
442 449
         matches = nil
@@ -445,7 +452,7 @@ def assert_select_rjs(*args, &block)
445 452
             matches = @response.body.match(pattern)
446 453
           else
447 454
             @response.body.gsub(pattern) do |match|
448  
-              html = unescape_rjs($2)
  455
+              html = unescape_rjs(match)
449 456
               matches ||= []
450 457
               matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? }
451 458
               ""
@@ -585,17 +592,16 @@ def assert_select_email(&block)
585 592
             :hide                 => /Element\.hide/,
586 593
             :toggle                 => /Element\.toggle/
587 594
           }
  595
+          RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
  596
+          RJS_PATTERN_HTML = /"((\\"|[^"])*)"/
588 597
           RJS_INSERTIONS = [:top, :bottom, :before, :after]
589 598
           RJS_INSERTIONS.each do |insertion|
590  
-            RJS_STATEMENTS["insert_#{insertion}".to_sym] = Regexp.new(Regexp.quote("new Insertion.#{insertion.to_s.camelize}"))
  599
+            RJS_STATEMENTS["insert_#{insertion}".to_sym] = /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/
591 600
           end
592  
-          RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
593 601
           RJS_STATEMENTS[:insert_html] = Regexp.new(RJS_INSERTIONS.collect do |insertion|
594  
-            Regexp.quote("new Insertion.#{insertion.to_s.camelize}")
  602
+            /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/
595 603
           end.join('|'))
596  
-          RJS_PATTERN_HTML = /"((\\"|[^"])*)"/
597  
-          RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)",
598  
-                                              Regexp::MULTILINE)
  604
+          RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
599 605
           RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
600 606
         end
601 607
 
12  actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -608,7 +608,7 @@ def include_helpers_from_context
608 608
         # Example:
609 609
         #
610 610
         #   # Generates:
611  
-        #   #     new Insertion.Bottom("list", "<li>Some item</li>");
  611
+        #   #     new Element.insert("list", { bottom: <li>Some item</li>" });
612 612
         #   #     new Effect.Highlight("list");
613 613
         #   #     ["status-indicator", "cancel-link"].each(Element.hide);
614 614
         #   update_page do |page|
@@ -741,16 +741,16 @@ def select(pattern)
741 741
           #
742 742
           #   # Insert the rendered 'navigation' partial just before the DOM
743 743
           #   # element with ID 'content'.
744  
-          #   # Generates: new Insertion.Before("content", "-- Contents of 'navigation' partial --");
  744
+          #   # Generates: Element.insert("content", { before: "-- Contents of 'navigation' partial --" });
745 745
           #   page.insert_html :before, 'content', :partial => 'navigation'
746 746
           #
747 747
           #   # Add a list item to the bottom of the <ul> with ID 'list'.
748  
-          #   # Generates: new Insertion.Bottom("list", "<li>Last item</li>");
  748
+          #   # Generates: Element.insert("list", { bottom: "<li>Last item</li>" });
749 749
           #   page.insert_html :bottom, 'list', '<li>Last item</li>'
750 750
           #
751 751
           def insert_html(position, id, *options_for_render)
752  
-            insertion = position.to_s.camelize
753  
-            call "new Insertion.#{insertion}", id, render(*options_for_render)
  752
+            content = javascript_object_for(render(*options_for_render))
  753
+            record "Element.insert(\"#{id}\", { #{position.to_s.downcase}: #{content} });"
754 754
           end
755 755
 
756 756
           # Replaces the inner HTML of the DOM element with the given +id+.
@@ -1054,7 +1054,7 @@ def options_for_ajax(options)
1054 1054
 
1055 1055
         js_options['asynchronous'] = options[:type] != :synchronous
1056 1056
         js_options['method']       = method_option_to_s(options[:method]) if options[:method]
1057  
-        js_options['insertion']    = "Insertion.#{options[:position].to_s.camelize}" if options[:position]
  1057
+        js_options['insertion']    = options[:position].to_s.downcase if options[:position]
1058 1058
         js_options['evalScripts']  = options[:script].nil? || options[:script]
1059 1059
 
1060 1060
         if options[:form]
12  actionpack/test/template/prototype_helper_test.rb
@@ -287,13 +287,13 @@ def setup
287 287
   end
288 288
 
289 289
   def test_insert_html_with_string
290  
-    assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");',
  290
+    assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });',
291 291
       @generator.insert_html(:top, 'element', '<p>This is a test</p>')
292  
-    assert_equal 'new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',
  292
+    assert_equal 'Element.insert("element", { bottom: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
293 293
       @generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
294  
-    assert_equal 'new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',
  294
+    assert_equal 'Element.insert("element", { before: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
295 295
       @generator.insert_html(:before, 'element', '<p>This is a test</p>')
296  
-    assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',
  296
+    assert_equal 'Element.insert("element", { after: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
297 297
       @generator.insert_html(:after, 'element', '<p>This is a test</p>')
298 298
   end
299 299
 
@@ -366,8 +366,8 @@ def test_to_s
366 366
     @generator.replace_html('baz', '<p>This is a test</p>')
367 367
 
368 368
     assert_equal <<-EOS.chomp, @generator.to_s
369  
-new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
370  
-new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
  369
+Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
  370
+Element.insert("element", { bottom: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
371 371
 ["foo", "bar"].each(Element.remove);
372 372
 Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
373 373
     EOS

0 notes on commit eaab895

Please sign in to comment.
Something went wrong with that request. Please try again.