Ticket #11401: change_insertion_new_for_element_insert.diff
| File change_insertion_new_for_element_insert.diff, 8.6 kB (added by miloops, 2 years ago) |
|---|
-
test/template/prototype_helper_test.rb
old new 303 303 end 304 304 305 305 def test_insert_html_with_string 306 assert_equal ' new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");',306 assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });', 307 307 @generator.insert_html(:top, 'element', '<p>This is a test</p>') 308 assert_equal ' new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',308 assert_equal 'Element.insert("element", { bottom: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 309 309 @generator.insert_html(:bottom, 'element', '<p>This is a test</p>') 310 assert_equal ' new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',310 assert_equal 'Element.insert("element", { before: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 311 311 @generator.insert_html(:before, 'element', '<p>This is a test</p>') 312 assert_equal ' new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',312 assert_equal 'Element.insert("element", { after: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 313 313 @generator.insert_html(:after, 'element', '<p>This is a test</p>') 314 314 end 315 315 … … 375 375 @generator.replace_html('baz', '<p>This is a test</p>') 376 376 377 377 assert_equal <<-EOS.chomp, @generator.to_s 378 new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");379 new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");378 Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" }); 379 Element.insert("element", { bottom: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" }); 380 380 ["foo", "bar"].each(Element.remove); 381 381 Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 382 382 EOS -
lib/action_controller/assertions/selector_assertions.rb
old new 406 406 407 407 if rjs_type == :insert 408 408 arg = args.shift 409 position = arg 409 410 insertion = "insert_#{arg}".to_sym 410 411 raise ArgumentError, "Unknown RJS insertion type #{arg}" unless RJS_STATEMENTS[insertion] 411 statement = "(#{RJS_STATEMENTS[insertion]})"412 412 else 413 413 raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type] 414 414 statement = "(#{RJS_STATEMENTS[rjs_type]})" … … 417 417 else 418 418 statement = "#{RJS_STATEMENTS[:any]}" 419 419 end 420 position ||= Regexp.new(RJS_INSERTIONS.join('|')) 420 421 421 422 # Next argument we're looking for is the element identifier. If missing, we pick 422 423 # any element. … … 433 434 Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 434 435 when :remove, :show, :hide, :toggle 435 436 Regexp.new("#{statement}\\(\"#{id}\"\\)") 437 when :replace, :replace_html 438 Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)") 439 when :insert, :insert_html 440 Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);") 436 441 else 437 Regexp. new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)442 Regexp.union(Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)"), Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);")) 438 443 end 439 444 440 445 # Duplicate the body since the next step involves destroying it. … … 444 449 matches = @response.body.match(pattern) 445 450 else 446 451 @response.body.gsub(pattern) do |match| 447 html = unescape_rjs( $2)452 html = unescape_rjs(match) 448 453 matches ||= [] 449 454 matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? } 450 455 "" … … 584 589 :hide => /Element\.hide/, 585 590 :toggle => /Element\.toggle/ 586 591 } 592 RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})") 593 RJS_PATTERN_HTML = /"((\\"|[^"])*)"/ 587 594 RJS_INSERTIONS = [:top, :bottom, :before, :after] 588 595 RJS_INSERTIONS.each do |insertion| 589 RJS_STATEMENTS["insert_#{insertion}".to_sym] = Regexp.new(Regexp.quote("new Insertion.#{insertion.to_s.camelize}"))596 RJS_STATEMENTS["insert_#{insertion}".to_sym] = /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/ 590 597 end 591 RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")592 598 RJS_STATEMENTS[:insert_html] = Regexp.new(RJS_INSERTIONS.collect do |insertion| 593 Regexp.quote("new Insertion.#{insertion.to_s.camelize}")599 /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/ 594 600 end.join('|')) 595 RJS_PATTERN_HTML = /"((\\"|[^"])*)"/ 596 RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", 597 Regexp::MULTILINE) 601 RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 598 602 RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/ 599 603 end 600 604 -
lib/action_view/helpers/prototype_helper.rb
old new 603 603 # Example: 604 604 # 605 605 # # Generates: 606 # # new Insertion.Bottom("list", "<li>Some item</li>");606 # # new Element.insert("list", { bottom: <li>Some item</li>" }); 607 607 # # new Effect.Highlight("list"); 608 608 # # ["status-indicator", "cancel-link"].each(Element.hide); 609 609 # update_page do |page| … … 715 715 # 716 716 # # Insert the rendered 'navigation' partial just before the DOM 717 717 # # element with ID 'content'. 718 # # Generates: new Insertion.Before("content", "-- Contents of 'navigation' partial --");718 # # Generates: Element.insert("content", { before: "-- Contents of 'navigation' partial --" }); 719 719 # insert_html :before, 'content', :partial => 'navigation' 720 720 # 721 721 # # Add a list item to the bottom of the <ul> with ID 'list'. 722 # # Generates: new Insertion.Bottom("list", "<li>Last item</li>");722 # # Generates: Element.insert("list", { bottom: "<li>Last item</li>" }); 723 723 # insert_html :bottom, 'list', '<li>Last item</li>' 724 724 # 725 725 def insert_html(position, id, *options_for_render) 726 insertion = position.to_s.camelize727 call "new Insertion.#{insertion}", id, render(*options_for_render)726 content = javascript_object_for(render(*options_for_render)) 727 record "Element.insert(\"#{id}\", { #{position.to_s.downcase}: #{content} });" 728 728 end 729 729 730 730 # Replaces the inner HTML of the DOM element with the given +id+. … … 761 761 # <%= render :partial => 'person', :collection => @people %> 762 762 # </div> 763 763 # 764 # # Insert a new person765 # #766 # # Generates: new Insertion.Bottom({object: "Matz", partial: "person"}, "");767 # page.insert_html :bottom, :partial => 'person', :object => @person768 #769 764 # # Replace an existing person 770 765 # 771 766 # # Generates: Element.replace("person_45", "-- Contents of partial --"); … … 1005 1000 protected 1006 1001 def options_for_ajax(options) 1007 1002 js_options = build_callbacks(options) 1008 1003 1009 1004 js_options['asynchronous'] = options[:type] != :synchronous 1010 1005 js_options['method'] = method_option_to_s(options[:method]) if options[:method] 1011 js_options['insertion'] = " Insertion.#{options[:position].to_s.camelize}" if options[:position]1006 js_options['insertion'] = "#{options[:position].to_s.downcase}" if options[:position] 1012 1007 js_options['evalScripts'] = options[:script].nil? || options[:script] 1013 1008 1014 1009 if options[:form]