File: element_spec.rb

package info (click to toggle)
ruby-html-proofer 3.19.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ruby: 3,203; sh: 9; makefile: 4; javascript: 1; php: 1
file content (67 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

require 'spec_helper'

describe HTMLProofer::Element do
  before(:each) do
    @check = HTMLProofer::Check.new('', '', Nokogiri::HTML5(''), nil, 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, 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, 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, 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, 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, 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, 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, nil)
      expect(checkable.ignore?).to eq true
    end
  end

  describe 'ivar setting' do
    it 'does not explode if given a bad attribute' do
      broken_attribute = "#{FIXTURES_DIR}/html/invalid_attribute.html"
      proofer = run_proofer(broken_attribute, :file)
      expect(proofer.failed_tests.length).to eq 0
    end
  end
end