put the javascript at the start of the head block

This commit is contained in:
John Bintz 2012-12-04 14:12:53 -05:00
parent 7a6a51cb62
commit 394af20be2
2 changed files with 6 additions and 7 deletions

View File

@ -61,7 +61,7 @@ module Rack
content_length = 0
new_body.each do |line|
if !headers['X-Rack-LiveReload'] && (line['</head>'] || line['<script'])
if !headers['X-Rack-LiveReload'] && line['<head']
host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
if use_vendored?
@ -76,10 +76,8 @@ module Rack
template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__)))
if line['<script']
line.sub!('<script', %{#{template.result(binding)}<script})
else
line.gsub!('</head>', %{#{template.result(binding)}</head>})
if line['<head']
line.gsub!(/<head[^>]*>/) { |match| %{#{match}#{template.result(binding)}} }
end
headers["X-Rack-LiveReload"] = '1'

View File

@ -95,12 +95,13 @@ describe Rack::LiveReload do
end
end
context 'before script tags' do
let(:page_html) { '<head><script type="text/javascript" insert="before"></script></head>' }
context 'at the top of the head tag' do
let(:page_html) { '<head attribute="attribute"><script type="text/javascript" insert="first"></script><script type="text/javascript" insert="before"></script></head>' }
let(:body_dom) { Nokogiri::XML(body) }
it 'should add the livereload js script tag before all other script tags' do
body_dom.at_css("head")[:attribute].should == 'attribute'
body_dom.at_css("script:eq(4)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
body_dom.at_css("script:last-child")[:insert].should == "before"
end