Only injects code into the first occurrence of <head> tags

This commit is contained in:
Nathan Hoel 2014-12-05 11:23:31 -05:00
parent 952ea670dd
commit 3c58d4b0c0
6 changed files with 18 additions and 5 deletions

View File

@ -71,7 +71,7 @@ module Rack
@new_body.each do |line| @new_body.each do |line|
if !@livereload_added && line['<head'] if !@livereload_added && line['<head']
line.gsub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} } line.sub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} }
@livereload_added = true @livereload_added = true
end end
@ -109,4 +109,3 @@ module Rack
end end
end end
end end

View File

@ -21,6 +21,7 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example: # specify any dependencies here; for example:
s.add_development_dependency "rspec" s.add_development_dependency "rspec"
s.add_development_dependency "rspec-its"
s.add_development_dependency "cucumber" s.add_development_dependency "cucumber"
s.add_development_dependency "httparty" s.add_development_dependency "httparty"
s.add_development_dependency "sinatra" s.add_development_dependency "sinatra"

View File

@ -15,7 +15,7 @@ describe Rack::LiveReload::BodyProcessor do
end end
it 'responds false when no head tag' do it 'responds false when no head tag' do
regex.match("<header></header>").should be_false regex.match("<header></header>").should be_falsey
end end
end end
@ -143,6 +143,14 @@ describe Rack::LiveReload::BodyProcessor do
end end
end end
context 'in document with more than one reference to a head tag' do
let(:page_html) { "<head><head><!-- <head></head> -->" }
it 'should not add the livereload js' do
processed_body.should include == "<!-- <head></head> -->"
end
end
context 'not vendored' do context 'not vendored' do
before do before do
processor.stubs(:use_vendored?).returns(false) processor.stubs(:use_vendored?).returns(false)

View File

@ -14,7 +14,7 @@ describe Rack::LiveReload::ProcessingSkipAnalyzer do
describe '#skip_processing?' do describe '#skip_processing?' do
it "should skip processing" do it "should skip processing" do
subject.skip_processing?.should be_true subject.skip_processing?.should be_truthy
end end
end end

View File

@ -20,7 +20,7 @@ describe Rack::LiveReload do
end end
it 'should return the js file' do it 'should return the js file' do
middleware._call(env).should be_true middleware._call(env).should be_truthy
end end
end end
end end

View File

@ -1,9 +1,14 @@
require 'mocha/api' require 'mocha/api'
require 'webmock/rspec' require 'webmock/rspec'
require 'rspec/its'
require 'rack-livereload' require 'rack-livereload'
RSpec.configure do |c| RSpec.configure do |c|
c.expect_with :rspec do |config|
config.syntax = :should
end
c.mock_with :mocha c.mock_with :mocha
end end