Class: JavascriptEraser

Inherits:
Object
  • Object
show all
Defined in:
lib/javascript_eraser.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JavascriptEraser

Returns a new instance of JavascriptEraser.



2
3
4
# File 'lib/javascript_eraser.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/javascript_eraser.rb', line 6

def call(env)
  status, headers, response = @app.call(env)

  if headers['Content-Type'] and headers['Content-Type'].include?("javascript")
    response = ""
  elsif response.respond_to?(:body) and response.body.is_a?(String)
    response.body = response.body.gsub(/<script(.|\n)*?type(.|\n)*?javascript(.|\n)*?\/script>/,"")
    response.body = response.body.gsub(/\s*on\w+=".*?[^\\]"\s*(.*?)\s*>/, "\\1>")
    response.body = response.body.gsub(/\s*on\w+='.*?[^\\]'\s*(.*?)\s*>/, "\\1>")
  elsif response.class.to_s != "Rack::File"
    puts "JavascriptEraser: unknown response type: #{response.class}. check order of appearance in middleware stack."
  end

  [status, headers, response]
end