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 content_length = 0
new_body.each do |line| 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{:.*}, '') host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
if use_vendored? if use_vendored?
@ -76,10 +76,8 @@ 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['<script'] if line['<head']
line.sub!('<script', %{#{template.result(binding)}<script}) line.gsub!(/<head[^>]*>/) { |match| %{#{match}#{template.result(binding)}} }
else
line.gsub!('</head>', %{#{template.result(binding)}</head>})
end end
headers["X-Rack-LiveReload"] = '1' headers["X-Rack-LiveReload"] = '1'

View File

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