Skip to content

Instantly share code, notes, and snippets.

@nuna
Created November 24, 2010 13:01
Show Gist options
  • Save nuna/713617 to your computer and use it in GitHub Desktop.
Save nuna/713617 to your computer and use it in GitHub Desktop.
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(/'/, '&#39;') }
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