- 
                Notifications
    
You must be signed in to change notification settings  - Fork 22k
 
Add OutputBuffer#raw and #capture to reduce the need to swap the buffer #45731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -281,17 +281,8 @@ def read_fragment_for(name, options) | |
| controller.read_fragment(name, options) | ||
| end | ||
| 
     | 
||
| def write_fragment_for(name, options) | ||
| pos = output_buffer.length | ||
| yield | ||
| output_safe = output_buffer.html_safe? | ||
| # We need to modify the buffer in place to be deal with the view handlers | ||
| # like `builder` that don't access the buffer through `@output_buffer` but | ||
| # keep the initial reference. | ||
| fragment = output_buffer.slice!(pos..-1) | ||
| if output_safe | ||
| self.output_buffer = output_buffer.class.new(output_buffer) | ||
| end | ||
| def write_fragment_for(name, options, &block) | ||
| fragment = output_buffer.capture(&block) | ||
                
       | 
||
| controller.write_fragment(name, fragment, options) | ||
| end | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # frozen_string_literal: true | ||
| 
     | 
||
| require "abstract_unit" | ||
| 
     | 
||
| module SharedBufferTests | ||
| def self.included(test_case) | ||
| test_case.test "#<< maintains HTML safety" do | ||
| @buffer << "<script>alert('pwned!')</script>" | ||
| assert_predicate @buffer, :html_safe? | ||
| assert_predicate output, :html_safe? | ||
| assert_equal "<script>alert('pwned!')</script>", output | ||
| end | ||
| 
     | 
||
| test_case.test "#safe_append= bypasses HTML safety" do | ||
| @buffer.safe_append = "<p>This is fine</p>" | ||
| assert_predicate @buffer, :html_safe? | ||
| assert_predicate output, :html_safe? | ||
| assert_equal "<p>This is fine</p>", output | ||
| end | ||
| 
     | 
||
| test_case.test "#raw allow to bypass HTML escaping" do | ||
| raw_buffer = @buffer.raw | ||
| raw_buffer << "<script>alert('pwned!')</script>" | ||
| assert_predicate @buffer, :html_safe? | ||
| assert_predicate output, :html_safe? | ||
| assert_equal "<script>alert('pwned!')</script>", output | ||
| end | ||
| 
     | 
||
| test_case.test "#capture allow to intercept writes" do | ||
| @buffer << "Hello" | ||
| result = @buffer.capture do | ||
| @buffer << "George!" | ||
| end | ||
| assert_equal "George!", result | ||
| assert_predicate result, :html_safe? | ||
| 
     | 
||
| @buffer << " World!" | ||
| assert_equal "Hello World!", output | ||
| end | ||
| 
     | 
||
| test_case.test "#raw respects #capture" do | ||
| @buffer << "Hello" | ||
| raw_buffer = @buffer.raw | ||
| result = @buffer.capture do | ||
| raw_buffer << "George!" | ||
| end | ||
| assert_equal "George!", result | ||
| assert_predicate result, :html_safe? | ||
| 
     | 
||
| @buffer << " World!" | ||
| assert_equal "Hello World!", output | ||
| end | ||
| end | ||
| end | ||
| 
     | 
||
| class TestOutputBuffer < ActiveSupport::TestCase | ||
| include SharedBufferTests | ||
| 
     | 
||
| setup do | ||
| @buffer = ActionView::OutputBuffer.new | ||
| end | ||
| 
     | 
||
| test "can be duped" do | ||
| @buffer << "Hello" | ||
| copy = @buffer.dup | ||
| copy << " World!" | ||
| assert_equal "Hello World!", copy.to_s | ||
| assert_equal "Hello", output | ||
| end | ||
| 
     | 
||
| private | ||
| def output | ||
| @buffer.to_s | ||
| end | ||
| end | ||
| 
     | 
||
| class TestStreamingBuffer < ActiveSupport::TestCase | ||
| include SharedBufferTests | ||
| 
     | 
||
| setup do | ||
| @raw_buffer = +"" | ||
| @buffer = ActionView::StreamingBuffer.new(@raw_buffer.method(:<<)) | ||
| end | ||
| 
     | 
||
| private | ||
| def output | ||
| @raw_buffer.html_safe | ||
| end | ||
| end | 
This file was deleted.
      
      Oops, something went wrong.
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any legit reason for using
SafeBufferas@output_bufferand its holding us back.