Class: HTML::Proofer::Checkable
- Inherits:
-
Object
- Object
- HTML::Proofer::Checkable
show all
- Includes:
- Utils
- Defined in:
- lib/html/proofer/checkable.rb
Overview
Represents the superclass from which all checks derive.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
create_nokogiri, swap
Constructor Details
#initialize(obj, check) ⇒ Checkable
Returns a new instance of Checkable.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/html/proofer/checkable.rb', line 11
def initialize(obj, check)
obj.attributes.each_pair do |attribute, value|
next if attribute == 'data-proofer-ignore' instance_variable_set("@#{attribute}".to_sym, value.value)
end
@data_ignore_proofer = obj['data-proofer-ignore']
@content = obj.content
@check = check
@checked_paths = {}
@type = self.class.name
@line = obj.line
if @href && @check.options[:href_swap]
@href = swap(@href, @check.options[:href_swap])
end
@href.insert 0, 'http:' if @href =~ %r{^//}
@src.insert 0, 'http:' if @src =~ %r{^//}
end
|
Instance Attribute Details
#line ⇒ Object
Returns the value of attribute line.
9
10
11
|
# File 'lib/html/proofer/checkable.rb', line 9
def line
@line
end
|
Instance Method Details
#absolute_path ⇒ Object
121
122
123
124
|
# File 'lib/html/proofer/checkable.rb', line 121
def absolute_path
path = file_path || @check.path
File.expand_path path, Dir.pwd
end
|
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
116
117
118
119
|
# File 'lib/html/proofer/checkable.rb', line 116
def exists?
return @checked_paths[absolute_path] if @checked_paths.key? absolute_path
@checked_paths[absolute_path] = File.exist? absolute_path
end
|
#external? ⇒ Boolean
path is external to the file
83
84
85
|
# File 'lib/html/proofer/checkable.rb', line 83
def external?
!internal?
end
|
#file_path ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/html/proofer/checkable.rb', line 92
def file_path
return if path.nil?
if path =~ /^\// base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src)
elsif File.exist?(File.expand_path path, @check.src) base = File.dirname @check.path
elsif File.exist?(File.join(File.dirname(@check.path), path)) base = File.dirname @check.path
else base = @check.path
end
file = File.join base, path
if File.directory?(file) && !unslashed_directory?(file)
file = File.join file, @check.options[:directory_index_file]
end
file
end
|
#follow_location? ⇒ Boolean
142
143
144
|
# File 'lib/html/proofer/checkable.rb', line 142
def follow_location?
@check.options[:typhoeus] && @check.options[:typhoeus][:followlocation]
end
|
#hash ⇒ Object
51
52
53
|
# File 'lib/html/proofer/checkable.rb', line 51
def hash
parts.fragment unless parts.nil?
end
|
#ignore? ⇒ Boolean
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/html/proofer/checkable.rb', line 68
def ignore?
return true if @data_ignore_proofer
case @type
when 'FaviconCheckable'
return true if url.match(/^data:image/)
when 'LinkCheckable'
return true if ignores_pattern_check(@check.href_ignores)
when 'ImageCheckable'
return true if url.match(/^data:image/)
return true if ignores_pattern_check(@check.alt_ignores)
end
end
|
#ignores_pattern_check(links) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/html/proofer/checkable.rb', line 126
def ignores_pattern_check(links)
links.each do |ignore|
if ignore.is_a? String
return true if ignore == url
elsif ignore.is_a? Regexp
return true if ignore =~ url
end
end
false
end
|
#internal? ⇒ Boolean
path is an anchor or a query
88
89
90
|
# File 'lib/html/proofer/checkable.rb', line 88
def internal?
url.start_with? '#', '?'
end
|
#non_http_remote? ⇒ Boolean
64
65
66
|
# File 'lib/html/proofer/checkable.rb', line 64
def non_http_remote?
!scheme.nil? && !remote?
end
|
#parts ⇒ Object
41
42
43
44
45
|
# File 'lib/html/proofer/checkable.rb', line 41
def parts
@parts ||= Addressable::URI.parse url
rescue URI::Error
@parts = nil
end
|
#path ⇒ Object
47
48
49
|
# File 'lib/html/proofer/checkable.rb', line 47
def path
CGI.unescape parts.path unless parts.nil?
end
|
#remote? ⇒ Boolean
path is to an external server
60
61
62
|
# File 'lib/html/proofer/checkable.rb', line 60
def remote?
%w( http https ).include? scheme
end
|
#scheme ⇒ Object
55
56
57
|
# File 'lib/html/proofer/checkable.rb', line 55
def scheme
parts.scheme unless parts.nil?
end
|
#unslashed_directory?(file) ⇒ Boolean
138
139
140
|
# File 'lib/html/proofer/checkable.rb', line 138
def unslashed_directory?(file)
File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location?
end
|
#url ⇒ Object
33
34
35
|
# File 'lib/html/proofer/checkable.rb', line 33
def url
@src || @srcset || @href || ''
end
|
#valid? ⇒ Boolean
37
38
39
|
# File 'lib/html/proofer/checkable.rb', line 37
def valid?
!parts.nil?
end
|