Created
November 24, 2010 13:01
-
-
Save nuna/713617 to your computer and use it in GitHub Desktop.
This file contains 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
module Rack #:nodoc: | |
class ForceEscapeSingleQuoteInHTMLAttribute | |
def initialize(app, options = {}) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
if headers['Content-Type'] =~ %r!text/html|application/xhtml\+xml! | |
body = build_response_body(response) | |
body.gsub!(/<.+?>/) do |i| | |
i.gsub(/(\w+)=".*?"/){|attribute| $1 =~ /^on/ ? attribute : attribute.gsub(/'/, ''') } | |
end | |
headers['Content-Length'] = body.length.to_s | |
response = [body] | |
end | |
[status, headers, response] | |
end | |
private | |
def build_response_body(response) | |
body = '' | |
response.each{|part| body << part } | |
body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment