Merge pull request #21 from matthewlehner/head_tag_only

Fixes for head tag regex
This commit is contained in:
John Bintz 2012-12-14 06:28:52 -08:00
commit b3bd9fa9a4
2 changed files with 19 additions and 1 deletions

View File

@ -4,6 +4,7 @@ module Rack
class LiveReload class LiveReload
LIVERELOAD_JS_PATH = '/__rack/livereload.js' LIVERELOAD_JS_PATH = '/__rack/livereload.js'
LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js' LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js'
HEAD_TAG_REGEX = /<head>|<head[^(er)][^<]*>/
BAD_USER_AGENTS = [ %r{MSIE} ] BAD_USER_AGENTS = [ %r{MSIE} ]
@ -77,7 +78,7 @@ module Rack
template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__))) template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__)))
if line['<head'] if line['<head']
line.gsub!(/<head([^(er)]|\s)[^>]*>/) { |match| %{#{match}#{template.result(binding)}} } line.gsub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} }
end end
headers["X-Rack-LiveReload"] = '1' headers["X-Rack-LiveReload"] = '1'

View File

@ -223,5 +223,22 @@ describe Rack::LiveReload do
it { should be_bad_browser(user_agent) } it { should be_bad_browser(user_agent) }
end end
describe 'head tag regex' do
let(:regex) { described_class::HEAD_TAG_REGEX }
subject { regex }
it { should be_kind_of(Regexp) }
it 'only picks a valid <head> tag' do
regex.match("<head></head>").to_s.should eq('<head>')
regex.match("<head><title></title></head>").to_s.should eq('<head>')
regex.match("<head attribute='something'><title></title></head>").to_s.should eq("<head attribute='something'>")
end
it 'responds false when no head tag' do
regex.match("<header></header>").should be_false
end
end
end end