Skip to content

Commit

Permalink
Address module changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Nov 17, 2019
1 parent 0812ad4 commit a50c44e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/html-proofer/middleware.rb
Expand Up @@ -2,6 +2,8 @@

module HTMLProofer
class Middleware
include HTMLProofer::Utils

class InvalidHtmlError < StandardError
def initialize(failures)
@failures = failures
Expand Down Expand Up @@ -67,7 +69,7 @@ def call(env)
'response',
Middleware.options
).check_parsed(
Nokogiri::HTML(Utils.clean_content(html)), 'response'
Nokogiri::HTML(clean_content(html)), 'response'
)

raise InvalidHtmlError, parsed[:failures] unless parsed[:failures].empty?
Expand Down
6 changes: 3 additions & 3 deletions lib/html-proofer/utils.rb
Expand Up @@ -8,7 +8,7 @@ def pluralize(count, single, plural)
"#{count} #{(count == 1 ? single : plural)}"
end

def self.create_nokogiri(path)
def create_nokogiri(path)
content = if File.exist?(path) && !File.directory?(path)
File.open(path).read
else
Expand All @@ -18,7 +18,7 @@ def self.create_nokogiri(path)
Nokogiri::HTML(clean_content(content))
end

def self.swap(href, replacement)
def swap(href, replacement)
replacement.each do |link, replace|
href = href.gsub(link, replace)
end
Expand All @@ -28,7 +28,7 @@ def self.swap(href, replacement)
# address a problem with Nokogiri's parsing URL entities
# problem from http://git.io/vBYU1
# solution from http://git.io/vBYUi
def self.clean_content(string)
def clean_content(string)
string.gsub(%r{(?:https?:)?//([^>]+)}i) do |url|
url.gsub(/&(?!amp;)/, '&amp;')
end
Expand Down
8 changes: 5 additions & 3 deletions spec/html-proofer/utils_spec.rb
Expand Up @@ -4,18 +4,20 @@

describe HTMLProofer::Utils do
describe '::create_nokogiri' do
include HTMLProofer::Utils

it 'passes for a string' do
noko = HTMLProofer::Utils.create_nokogiri '<html lang="jp">'
noko = create_nokogiri '<html lang="jp">'
expect(noko.css('html').first['lang']).to eq 'jp'
end

it 'passes for a file' do
noko = HTMLProofer::Utils.create_nokogiri "#{FIXTURES_DIR}/utils/lang-jp.html"
noko = create_nokogiri "#{FIXTURES_DIR}/utils/lang-jp.html"
expect(noko.css('html').first['lang']).to eq 'jp'
end

it 'ignores directories' do
noko = HTMLProofer::Utils.create_nokogiri "#{FIXTURES_DIR}/utils"
noko = create_nokogiri "#{FIXTURES_DIR}/utils"
expect(noko.content).to eq 'spec/html-proofer/fixtures/utils'
end
end
Expand Down

0 comments on commit a50c44e

Please sign in to comment.