diff --git a/README.md b/README.md index 2af50a98..37f79689 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,13 @@ htmlproof ./_site * Whether your internal links are not broken; this includes hash references (`#linkToMe`) * Whether external links are working +### Scripts + +`script` elements: + +* Whether your internal script references are not broken +* Whether external scripts are loading + ## Configuration The `HTML::Proofer` constructor takes an optional hash of additional options: diff --git a/lib/html/proofer/checkable.rb b/lib/html/proofer/checkable.rb index 5215ebc6..a7e5d7dd 100644 --- a/lib/html/proofer/checkable.rb +++ b/lib/html/proofer/checkable.rb @@ -10,6 +10,7 @@ def initialize(obj, type, check) @rel = obj['rel'] @data_ignore_proofer = obj['data-proofer-ignore'] + @content = obj.content @check = check @checked_paths = {} @type = type diff --git a/lib/html/proofer/checks.rb b/lib/html/proofer/checks.rb index 998eb94f..e4bb43dd 100644 --- a/lib/html/proofer/checks.rb +++ b/lib/html/proofer/checks.rb @@ -1,10 +1,13 @@ module HTML class Proofer class Checks - require File.dirname(__FILE__) + '/check' - require File.dirname(__FILE__) + '/checks/images' - require File.dirname(__FILE__) + '/checks/links' - require File.dirname(__FILE__) + '/checks/favicon' + [ + "/check", + "/checks/images", + "/checks/links", + "/checks/scripts", + "/checks/favicon" + ].each { |r| require File.dirname(__FILE__) + r } end end end diff --git a/lib/html/proofer/checks/scripts.rb b/lib/html/proofer/checks/scripts.rb new file mode 100644 index 00000000..eb1b5c1d --- /dev/null +++ b/lib/html/proofer/checks/scripts.rb @@ -0,0 +1,40 @@ +# encoding: utf-8 + +class Script < ::HTML::Proofer::Checkable + + def src + @src unless @src.nil? || @src.empty? + end + + def missing_src? + !src + end + + def blank? + @content.strip.empty? + end + +end + +class Scripts < ::HTML::Proofer::Checks::Check + def run + @html.css('script').each do |s| + script = Script.new s, "script", self + + next if script.ignore? + next unless script.blank? + + # does the script exist? + if script.missing_src? + self.add_issue "script is empty and has no src attribute" + elsif script.remote? + add_to_external_urls script.src + else + self.add_issue("internal script #{script.src} does not exist") unless script.exists? + end + + end + + external_urls + end +end diff --git a/spec/html/proofer/fixtures/script.js b/spec/html/proofer/fixtures/script.js new file mode 100644 index 00000000..b506100a --- /dev/null +++ b/spec/html/proofer/fixtures/script.js @@ -0,0 +1 @@ +var x = 1; diff --git a/spec/html/proofer/fixtures/script_broken_external.html b/spec/html/proofer/fixtures/script_broken_external.html new file mode 100644 index 00000000..90fcc77d --- /dev/null +++ b/spec/html/proofer/fixtures/script_broken_external.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spec/html/proofer/fixtures/script_content.html b/spec/html/proofer/fixtures/script_content.html new file mode 100644 index 00000000..5e2c320d --- /dev/null +++ b/spec/html/proofer/fixtures/script_content.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spec/html/proofer/fixtures/script_content_absent.html b/spec/html/proofer/fixtures/script_content_absent.html new file mode 100644 index 00000000..c0993bb6 --- /dev/null +++ b/spec/html/proofer/fixtures/script_content_absent.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spec/html/proofer/fixtures/script_missing_internal.html b/spec/html/proofer/fixtures/script_missing_internal.html new file mode 100644 index 00000000..be24abee --- /dev/null +++ b/spec/html/proofer/fixtures/script_missing_internal.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spec/html/proofer/fixtures/script_valid_internal.html b/spec/html/proofer/fixtures/script_valid_internal.html new file mode 100644 index 00000000..ef1bd77a --- /dev/null +++ b/spec/html/proofer/fixtures/script_valid_internal.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spec/html/proofer/scripts_spec.rb b/spec/html/proofer/scripts_spec.rb new file mode 100644 index 00000000..1abab501 --- /dev/null +++ b/spec/html/proofer/scripts_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe "Scripts tests" do + + it "fails for broken external src" do + file = "#{FIXTURES_DIR}/script_broken_external.html" + output = capture_stderr { HTML::Proofer.new(file).run } + output.should match /External link http:\/\/www.asdo3IRJ395295jsingrkrg4.com\/asdo3IRJ.js? failed: 0 Couldn't resolve host name/ + end + + it "works for valid internal src" do + file = "#{FIXTURES_DIR}/script_valid_internal.html" + output = capture_stderr { HTML::Proofer.new(file).run } + output.should == "" + end + + it "fails for missing internal src" do + file = "#{FIXTURES_DIR}/script_missing_internal.html" + output = capture_stderr { HTML::Proofer.new(file).run } + output.should match /doesnotexist.js does not exist/ + end + + it "works for present content" do + file = "#{FIXTURES_DIR}/script_content.html" + output = capture_stderr { HTML::Proofer.new(file).run } + output.should == "" + end + + it "fails for absent content" do + file = "#{FIXTURES_DIR}/script_content_absent.html" + output = capture_stderr { HTML::Proofer.new(file).run } + output.should match /script is empty and has no src attribute/ + end + +end