Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5202

Show
Ignore:
Timestamp:
09/29/06 07:39:31 (2 years ago)
Author:
bitsweat
Message:

assert_select_rjs decodes escaped unicode chars since the Javascript generators encode them. Closes #6240.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r5201 r5202  
    11*SVN* 
     2 
     3* assert_select_rjs decodes escaped unicode chars since the Javascript generators encode them.  #6240 [japgolly] 
    24 
    35* Deprecation: @request will be removed after 1.2. Use the request method instead.  [Jeremy Kemper] 
  • trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb

    r5110 r5202  
    2525    #     for example for dealing with feed item descriptions. 
    2626    # * #assert_select_email    -- Assertions on the HTML body of an e-mail. 
    27     #  
     27    # 
    2828    # Also see HTML::Selector for learning how to use selectors. 
    2929    module SelectorAssertions 
     
    8080          when Array 
    8181            selector = HTML::Selector.new(*arg) 
    82           when HTML::Selector  
     82          when HTML::Selector 
    8383            selector = arg 
    8484          else raise ArgumentError, "Expecting a selector as the first argument" 
    8585        end 
    8686 
    87         selector.select(root)   
     87        selector.select(root) 
    8888      end 
    8989 
     
    145145      # 
    146146      # === Examples 
    147       #  
     147      # 
    148148      #   # At least one form element 
    149149      #   assert_select "form" 
     
    201201          when Array 
    202202            selector = HTML::Selector.new(*arg) 
    203           when HTML::Selector  
     203          when HTML::Selector 
    204204            selector = arg 
    205205          else raise ArgumentError, "Expecting a selector as the first argument" 
     
    243243        end 
    244244 
    245         matches = selector.select(root)   
     245        matches = selector.select(root) 
    246246        # Equality test. 
    247247        equals.each do |type, value| 
     
    406406              Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
    407407          end 
    408            
     408 
    409409        # Duplicate the body since the next step involves destroying it. 
    410410        matches = nil 
    411411        @response.body.gsub(pattern) do |match| 
    412           html = $2 
    413           # RJS encodes double quotes and line breaks. 
    414           html.gsub!(/\\"/, "\"") 
    415           html.gsub!(/\\n/, "\n") 
     412          html = unescape_rjs($2) 
    416413          matches ||= [] 
    417414          matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? } 
     
    450447      # 
    451448      #   assert_select_feed :rss, 2.0 do 
    452       #     # Select description element of each feed item.  
     449      #     # Select description element of each feed item. 
    453450      #     assert_select "channel>item>description" do 
    454451      #       # Run assertions on the encoded elements. 
     
    537534          RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", 
    538535                                              Regexp::MULTILINE) 
     536          RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/ 
    539537        end 
    540538 
     
    548546            while true 
    549547              next if body.sub!(RJS_PATTERN_EVERYTHING) do |match| 
    550                 # RJS encodes double quotes and line breaks. 
    551                 html = $3 
    552                 html.gsub!(/\\"/, "\"") 
    553                 html.gsub!(/\\n/, "\n") 
     548                html = unescape_rjs($3) 
    554549                matches = HTML::Document.new(html).root.children.select { |n| n.tag? } 
    555550                root.children.concat matches 
     
    563558          end 
    564559        end 
     560 
     561        # Unescapes a RJS string. 
     562        def unescape_rjs(rjs_string) 
     563          # RJS encodes double quotes and line breaks. 
     564          unescaped= rjs_string.gsub('\"', '"') 
     565          unescaped.gsub!('\n', "\n") 
     566          # RJS encodes non-ascii characters. 
     567          unescaped.gsub!(RJS_PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')} 
     568          unescaped 
     569        end 
     570 
    565571    end 
    566572  end 
  • trunk/actionpack/test/controller/assert_select_test.rb

    r5110 r5202  
    289289  end 
    290290 
     291  def test_assert_select_rjs_with_unicode 
     292    # Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly. 
     293    render_rjs do |page| 
     294      page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>" 
     295    end 
     296    assert_select_rjs do 
     297      assert_select "#1", :text => "\343\203\201\343\202\261\343\203\203\343\203\210" 
     298      assert_select "#1", "\343\203\201\343\202\261\343\203\203\343\203\210" 
     299      assert_select "#1", Regexp.new("\343\203\201..\343\203\210",0,'U') 
     300      assert_raises(AssertionFailedError) { assert_select "#1", Regexp.new("\343\203\201.\343\203\210",0,'U') } 
     301    end 
     302  end 
    291303 
    292304  def test_assert_select_rjs_with_id