Changeset 5202
- Timestamp:
- 09/29/06 07:39:31 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb (modified) (10 diffs)
- trunk/actionpack/test/controller/assert_select_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r5201 r5202 1 1 *SVN* 2 3 * assert_select_rjs decodes escaped unicode chars since the Javascript generators encode them. #6240 [japgolly] 2 4 3 5 * 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 25 25 # for example for dealing with feed item descriptions. 26 26 # * #assert_select_email -- Assertions on the HTML body of an e-mail. 27 # 27 # 28 28 # Also see HTML::Selector for learning how to use selectors. 29 29 module SelectorAssertions … … 80 80 when Array 81 81 selector = HTML::Selector.new(*arg) 82 when HTML::Selector 82 when HTML::Selector 83 83 selector = arg 84 84 else raise ArgumentError, "Expecting a selector as the first argument" 85 85 end 86 86 87 selector.select(root) 87 selector.select(root) 88 88 end 89 89 … … 145 145 # 146 146 # === Examples 147 # 147 # 148 148 # # At least one form element 149 149 # assert_select "form" … … 201 201 when Array 202 202 selector = HTML::Selector.new(*arg) 203 when HTML::Selector 203 when HTML::Selector 204 204 selector = arg 205 205 else raise ArgumentError, "Expecting a selector as the first argument" … … 243 243 end 244 244 245 matches = selector.select(root) 245 matches = selector.select(root) 246 246 # Equality test. 247 247 equals.each do |type, value| … … 406 406 Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 407 407 end 408 408 409 409 # Duplicate the body since the next step involves destroying it. 410 410 matches = nil 411 411 @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) 416 413 matches ||= [] 417 414 matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? } … … 450 447 # 451 448 # assert_select_feed :rss, 2.0 do 452 # # Select description element of each feed item. 449 # # Select description element of each feed item. 453 450 # assert_select "channel>item>description" do 454 451 # # Run assertions on the encoded elements. … … 537 534 RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", 538 535 Regexp::MULTILINE) 536 RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/ 539 537 end 540 538 … … 548 546 while true 549 547 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) 554 549 matches = HTML::Document.new(html).root.children.select { |n| n.tag? } 555 550 root.children.concat matches … … 563 558 end 564 559 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 565 571 end 566 572 end trunk/actionpack/test/controller/assert_select_test.rb
r5110 r5202 289 289 end 290 290 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 291 303 292 304 def test_assert_select_rjs_with_id