From 706d15832878dec54fb9c10ca7bc6deecb9d5226 Mon Sep 17 00:00:00 2001 From: Matthew Lehner Date: Thu, 13 Dec 2012 16:27:06 -0800 Subject: [PATCH 1/2] extract head_tag_regex to constant --- lib/rack/livereload.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rack/livereload.rb b/lib/rack/livereload.rb index 38b2ef0..bec1bf9 100644 --- a/lib/rack/livereload.rb +++ b/lib/rack/livereload.rb @@ -4,6 +4,7 @@ module Rack class LiveReload LIVERELOAD_JS_PATH = '/__rack/livereload.js' LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js' + HEAD_TAG_REGEX = /|/ BAD_USER_AGENTS = [ %r{MSIE} ] @@ -77,7 +78,7 @@ module Rack template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__))) if line[']*>/) { |match| %{#{match}#{template.result(binding)}} } + line.gsub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} } end headers["X-Rack-LiveReload"] = '1' From 0c5d9bc06bb8af77c738183735951a0bf56dc0be Mon Sep 17 00:00:00 2001 From: Matthew Lehner Date: Thu, 13 Dec 2012 16:27:16 -0800 Subject: [PATCH 2/2] add specs for regex --- spec/rack/livereload_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/rack/livereload_spec.rb b/spec/rack/livereload_spec.rb index 4652732..649a9e6 100644 --- a/spec/rack/livereload_spec.rb +++ b/spec/rack/livereload_spec.rb @@ -223,5 +223,22 @@ describe Rack::LiveReload do it { should be_bad_browser(user_agent) } 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 tag' do + regex.match("").to_s.should eq('') + regex.match("").to_s.should eq('') + regex.match("").to_s.should eq("") + end + + it 'responds false when no head tag' do + regex.match("
").should be_false + end + end end