Skip to content

Commit

Permalink
Merge pull request #560 from gjtorikian/leave-semicolon-attr
Browse files Browse the repository at this point in the history
Leave semicolon attr
  • Loading branch information
gjtorikian committed Mar 26, 2020
2 parents 6859a4c + 9cdaed2 commit 207461b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 20 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,6 +1,5 @@
language: ruby
rvm:
- 2.3.6
- 2.4.3
- 2.5.0
- 2.6.0
Expand Down
5 changes: 3 additions & 2 deletions lib/html-proofer/check.rb
Expand Up @@ -5,18 +5,19 @@ module HTMLProofer
class Check
attr_reader :node, :html, :element, :src, :path, :options, :issues, :external_urls

def initialize(src, path, html, options)
def initialize(src, path, html, logger, options)
@src = src
@path = path
@html = remove_ignored(html)
@logger = logger
@options = options
@issues = []
@external_urls = {}
end

def create_element(node)
@node = node
Element.new(node, self)
Element.new(node, self, @logger)
end

def run
Expand Down
6 changes: 3 additions & 3 deletions lib/html-proofer/check/opengraph.rb
Expand Up @@ -3,8 +3,8 @@
class OpenGraphElement < ::HTMLProofer::Element
attr_reader :src

def initialize(obj, check)
super(obj, check)
def initialize(obj, check, logger)
super(obj, check, logger)
# Fake up src from the content attribute
instance_variable_set('@src', @content)

Expand All @@ -23,7 +23,7 @@ def empty_src?

def run
@html.css('meta[property="og:url"], meta[property="og:image"]').each do |m|
@opengraph = OpenGraphElement.new(m, self)
@opengraph = OpenGraphElement.new(m, self, @logger)

next if @opengraph.ignore?

Expand Down
16 changes: 11 additions & 5 deletions lib/html-proofer/element.rb
Expand Up @@ -10,12 +10,18 @@ class Element

attr_reader :id, :name, :alt, :href, :link, :src, :line, :data_proofer_ignore

def initialize(obj, check)
def initialize(obj, check, logger)
@logger = logger
# Construct readable ivars for every element
obj.attributes.each_pair do |attribute, value|
name = attribute.tr('-:.', '_').to_s.to_sym
(class << self; self; end).send(:attr_reader, name)
instance_variable_set("@#{name}", value.value)
begin
obj.attributes.each_pair do |attribute, value|
name = attribute.tr('-:.;', '_').to_s.to_sym
(class << self; self; end).send(:attr_reader, name)
instance_variable_set("@#{name}", value.value)
end
rescue NameError => e
@logger.log :error, "Attribute set `#{obj}` contains an error!"
raise e
end

@aria_hidden = defined?(@aria_hidden) && @aria_hidden == 'true' ? true : false
Expand Down
4 changes: 3 additions & 1 deletion lib/html-proofer/runner.rb
Expand Up @@ -100,7 +100,7 @@ def check_parsed(html, path)
@src.each do |src|
checks.each do |klass|
@logger.log :debug, "Checking #{klass.to_s.downcase} on #{path} ..."
check = Object.const_get(klass).new(src, path, html, @options)
check = Object.const_get(klass).new(src, path, html, @logger, @options)
check.run
external_urls = check.external_urls
external_urls = Hash[check.external_urls.map { |url, file| [swap(url, @options[:url_swap]), file] }] if @options[:url_swap]
Expand Down Expand Up @@ -147,6 +147,8 @@ def ignore_file?(file)
def checks
return @checks if defined?(@checks) && !@checks.nil?

return (@checks = ['LinkCheck']) if @type == :links

@checks = HTMLProofer::Check.subchecks.map(&:name)
@checks.delete('FaviconCheck') unless @options[:check_favicon]
@checks.delete('HtmlCheck') unless @options[:check_html]
Expand Down
16 changes: 8 additions & 8 deletions spec/html-proofer/element_spec.rb
Expand Up @@ -4,55 +4,55 @@

describe HTMLProofer::Element do
before(:each) do
@check = HTMLProofer::Check.new('', '', Nokogiri::HTML5(''), HTMLProofer::Configuration::PROOFER_DEFAULTS)
@check = HTMLProofer::Check.new('', '', Nokogiri::HTML5(''), nil, HTMLProofer::Configuration::PROOFER_DEFAULTS)
end

describe '#initialize' do
it 'accepts the xmlns attribute' do
nokogiri = Nokogiri::HTML5('<a xmlns:cc="http://creativecommons.org/ns#">Creative Commons</a>')
checkable = HTMLProofer::Element.new(nokogiri.css('a').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('a').first, @check, nil)
expect(checkable.instance_variable_get(:@xmlns_cc)).to eq 'http://creativecommons.org/ns#'
end

it 'assignes the text node' do
nokogiri = Nokogiri::HTML5('<p>One')
checkable = HTMLProofer::Element.new(nokogiri.css('p').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('p').first, @check, nil)
expect(checkable.instance_variable_get(:@text)).to eq 'One'
end

it 'accepts the content attribute' do
nokogiri = Nokogiri::HTML5('<meta name="twitter:card" content="summary">')
checkable = HTMLProofer::Element.new(nokogiri.css('meta').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('meta').first, @check, nil)
expect(checkable.instance_variable_get(:@content)).to eq 'summary'
end
end

describe '#ignores_pattern_check' do
it 'works for regex patterns' do
nokogiri = Nokogiri::HTML5('<script src=/assets/main.js></script>')
checkable = HTMLProofer::Element.new(nokogiri.css('script').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('script').first, @check, nil)
expect(checkable.ignores_pattern_check([%r{\/assets\/.*(js|css|png|svg)}])).to eq true
end

it 'works for string patterns' do
nokogiri = Nokogiri::HTML5('<script src=/assets/main.js></script>')
checkable = HTMLProofer::Element.new(nokogiri.css('script').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('script').first, @check, nil)
expect(checkable.ignores_pattern_check(['/assets/main.js'])).to eq true
end
end

describe '#url' do
it 'works for src attributes' do
nokogiri = Nokogiri::HTML5('<img src=image.png />')
checkable = HTMLProofer::Element.new(nokogiri.css('img').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('img').first, @check, nil)
expect(checkable.url).to eq 'image.png'
end
end

describe '#ignore' do
it 'works for twitter cards' do
nokogiri = Nokogiri::HTML5('<meta name="twitter:url" data-proofer-ignore content="http://example.com/soon-to-be-published-url">')
checkable = HTMLProofer::Element.new(nokogiri.css('meta').first, @check)
checkable = HTMLProofer::Element.new(nokogiri.css('meta').first, @check, nil)
expect(checkable.ignore?).to eq true
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/html-proofer/fixtures/images/semicolon.html
@@ -0,0 +1,9 @@
<img
class="img-fluid"
src="./gpl.png"
alt="Frank Blank"
;
width="365"
;
height="365"
/><br />
6 changes: 6 additions & 0 deletions spec/html-proofer/images_spec.rb
Expand Up @@ -201,4 +201,10 @@
proofer = run_proofer(relative_images, :file)
expect(proofer.failed_tests).to eq []
end

it 'ignores semicolon outside attribute name' do
relative_images = "#{FIXTURES_DIR}/images/semicolon.html"
proofer = run_proofer(relative_images, :file)
expect(proofer.failed_tests).to eq []
end
end

0 comments on commit 207461b

Please sign in to comment.