diff --git a/lib/rack/livereload.rb b/lib/rack/livereload.rb index fc33f0f..4555892 100644 --- a/lib/rack/livereload.rb +++ b/lib/rack/livereload.rb @@ -61,7 +61,7 @@ module Rack content_length = 0 new_body.each do |line| - if !headers['X-Rack-LiveReload'] && line[''] + if !headers['X-Rack-LiveReload'] && (line[''] || line['', %{#{template.result(binding)}}) + if line['', %{#{template.result(binding)}}) + end headers["X-Rack-LiveReload"] = '1' end diff --git a/rack-livereload.gemspec b/rack-livereload.gemspec index f029d3c..be758d4 100644 --- a/rack-livereload.gemspec +++ b/rack-livereload.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |s| s.add_development_dependency "guard-cucumber" s.add_development_dependency "guard-livereload" s.add_development_dependency "webmock" + s.add_development_dependency "nokogiri" s.add_development_dependency 'appraisal', '~> 0.4' s.add_runtime_dependency "rack" end diff --git a/spec/rack/livereload_spec.rb b/spec/rack/livereload_spec.rb index af383e0..7bb5081 100644 --- a/spec/rack/livereload_spec.rb +++ b/spec/rack/livereload_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'nokogiri' describe Rack::LiveReload do let(:middleware) { described_class.new(app, options) } @@ -67,7 +68,7 @@ describe Rack::LiveReload do context 'text/html' do before do - app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ '' ] ]) + app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ page_html ] ]) middleware.stubs(:use_vendored?).returns(true) end @@ -78,6 +79,8 @@ describe Rack::LiveReload do let(:body) { ret.last.join } let(:length) { ret[1]['Content-Length'] } + let(:page_html) { '' } + context 'vendored' do it 'should add the vendored livereload js script tag' do body.should include("script") @@ -92,6 +95,17 @@ describe Rack::LiveReload do end end + context 'before script tags' do + let(:page_html) { '' } + + let(:body_dom) { Nokogiri::XML(body) } + + it 'should add the livereload js script tag before all other script tags' do + 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 + end + context 'not vendored' do before do middleware.stubs(:use_vendored?).returns(false)