Class: HTMLProofer::Element
- Inherits:
-
Object
- Object
- HTMLProofer::Element
show all
- Includes:
- Utils
- Defined in:
- lib/html-proofer/element.rb
Overview
Represents the element currently being processed
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#create_nokogiri, #pluralize, #swap
Constructor Details
#initialize(obj, check, logger) ⇒ Element
Returns a new instance of Element.
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
|
# File 'lib/html-proofer/element.rb', line 13
def initialize(obj, check, logger)
@logger = logger
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
@data_proofer_ignore = defined?(@data_proofer_ignore)
@text = obj.content
@check = check
@checked_paths = {}
@type = check.class.name
@line = obj.line
@html = check.html
parent_attributes = obj.ancestors.map { |a| a.respond_to?(:attributes) && a.attributes }
parent_attributes.pop @parent_ignorable = parent_attributes.any? { |a| !a['data-proofer-ignore'].nil? }
if defined?(@href)
@href.insert(0, 'http:') if @href =~ %r{^//}
else
@href = nil
end
if defined?(@src)
@src.insert(0, 'http:') if @src =~ %r{^//}
else
@src = nil
end
if defined?(@srcset)
@srcset.insert(0, 'http:') if @srcset =~ %r{^//}
else
@srcset = nil
end
end
|
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def alt
@alt
end
|
#data_proofer_ignore ⇒ Object
Returns the value of attribute data_proofer_ignore.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def data_proofer_ignore
@data_proofer_ignore
end
|
#href ⇒ Object
Returns the value of attribute href.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def href
@href
end
|
#id ⇒ Object
Returns the value of attribute id.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def id
@id
end
|
#line ⇒ Object
Returns the value of attribute line.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def line
@line
end
|
#link ⇒ Object
Returns the value of attribute link.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def link
@link
end
|
#name ⇒ Object
Returns the value of attribute name.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def name
@name
end
|
#src ⇒ Object
Returns the value of attribute src.
11
12
13
|
# File 'lib/html-proofer/element.rb', line 11
def src
@src
end
|
Instance Method Details
#absolute_path ⇒ Object
213
214
215
216
|
# File 'lib/html-proofer/element.rb', line 213
def absolute_path
path = file_path || @check.path
File.expand_path path, Dir.pwd
end
|
#allow_hash_href? ⇒ Boolean
131
132
133
|
# File 'lib/html-proofer/element.rb', line 131
def allow_hash_href?
@check.options[:allow_hash_href]
end
|
#allow_missing_href? ⇒ Boolean
127
128
129
|
# File 'lib/html-proofer/element.rb', line 127
def allow_missing_href?
@check.options[:allow_missing_href]
end
|
#base ⇒ Object
238
239
240
|
# File 'lib/html-proofer/element.rb', line 238
def base
@base ||= @html.at_css('base')
end
|
#check_img_http? ⇒ Boolean
135
136
137
|
# File 'lib/html-proofer/element.rb', line 135
def check_img_http?
@check.options[:check_img_http]
end
|
#check_sri? ⇒ Boolean
139
140
141
|
# File 'lib/html-proofer/element.rb', line 139
def check_sri?
@check.options[:check_sri]
end
|
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
207
208
209
210
211
|
# File 'lib/html-proofer/element.rb', line 207
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
144
145
146
|
# File 'lib/html-proofer/element.rb', line 144
def external?
!internal?
end
|
#file_path ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/html-proofer/element.rb', line 174
def file_path
return if path.nil? || path.empty?
path_dot_ext = ''
path_dot_ext = path + @check.options[:extension] if @check.options[:assume_extension]
if path =~ %r{^/} if File.directory?(@check.src)
base = @check.src
else
root_dir = @check.options[:root_dir]
base = root_dir || File.dirname(@check.src)
end
elsif File.exist?(File.expand_path(path, @check.src)) || File.exist?(File.expand_path(path_dot_ext, @check.src)) base = File.dirname @check.path
elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) base = File.dirname @check.path
else base = @check.path
end
file = File.join base, path
if @check.options[:assume_extension] && File.file?("#{file}#{@check.options[:extension]}")
file = "#{file}#{@check.options[:extension]}"
elsif File.directory?(file) && !unslashed_directory?(file) file = File.join file, @check.options[:directory_index_file]
end
file
end
|
#follow_location? ⇒ Boolean
234
235
236
|
# File 'lib/html-proofer/element.rb', line 234
def follow_location?
@check.options[:typhoeus] && @check.options[:typhoeus][:followlocation]
end
|
#hash ⇒ Object
87
88
89
|
# File 'lib/html-proofer/element.rb', line 87
def hash
parts&.fragment
end
|
#hash_link ⇒ Object
166
167
168
|
# File 'lib/html-proofer/element.rb', line 166
def hash_link
url.start_with?('#')
end
|
#html ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/html-proofer/element.rb', line 242
def html
if link_points_to_same_page?
@html
elsif internal?
create_nokogiri(absolute_path)
else
raise NotImplementedError, 'HTMLProofer should not have gotten here. Please report this as a bug.'
end
end
|
#ignore? ⇒ Boolean
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/html-proofer/element.rb', line 104
def ignore?
return true if @data_proofer_ignore
return true if @parent_ignorable
return true if url =~ /^javascript:/
if %w[ImageCheck FaviconCheck].include? @type
return true if url =~ /^data:image/
end
return true if ignores_pattern_check(@check.options[:url_ignore])
end
|
#ignore_alt? ⇒ Boolean
119
120
121
|
# File 'lib/html-proofer/element.rb', line 119
def ignore_alt?
return true if ignores_pattern_check(@check.options[:alt_ignore]) || @aria_hidden
end
|
#ignore_empty_alt? ⇒ Boolean
123
124
125
|
# File 'lib/html-proofer/element.rb', line 123
def ignore_empty_alt?
@check.options[:empty_alt_ignore]
end
|
#ignores_pattern_check(links) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/html-proofer/element.rb', line 218
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
148
149
150
|
# File 'lib/html-proofer/element.rb', line 148
def internal?
relative_link? || internal_absolute_link?
end
|
#internal_absolute_link? ⇒ Boolean
152
153
154
|
# File 'lib/html-proofer/element.rb', line 152
def internal_absolute_link?
url.start_with?('/')
end
|
#link_points_to_same_page? ⇒ Boolean
162
163
164
|
# File 'lib/html-proofer/element.rb', line 162
def link_points_to_same_page?
hash_link || param_link
end
|
#non_http_remote? ⇒ Boolean
100
101
102
|
# File 'lib/html-proofer/element.rb', line 100
def non_http_remote?
!scheme.nil? && !remote?
end
|
#param_link ⇒ Object
170
171
172
|
# File 'lib/html-proofer/element.rb', line 170
def param_link
url.start_with?('?')
end
|
#parts ⇒ Object
77
78
79
80
81
|
# File 'lib/html-proofer/element.rb', line 77
def parts
@parts ||= Addressable::URI.parse url
rescue URI::Error, Addressable::URI::InvalidURIError
@parts = nil
end
|
#path ⇒ Object
83
84
85
|
# File 'lib/html-proofer/element.rb', line 83
def path
Addressable::URI.unencode parts.path unless parts.nil?
end
|
#relative_link? ⇒ Boolean
156
157
158
159
160
|
# File 'lib/html-proofer/element.rb', line 156
def relative_link?
return false if remote?
hash_link || param_link || url.start_with?('.') || url =~ /^\S/
end
|
#remote? ⇒ Boolean
path is to an external server
96
97
98
|
# File 'lib/html-proofer/element.rb', line 96
def remote?
%w[http https].include? scheme
end
|
#scheme ⇒ Object
91
92
93
|
# File 'lib/html-proofer/element.rb', line 91
def scheme
parts&.scheme
end
|
#unslashed_directory?(file) ⇒ Boolean
230
231
232
|
# File 'lib/html-proofer/element.rb', line 230
def unslashed_directory?(file)
File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location?
end
|
#url ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/html-proofer/element.rb', line 63
def url
return @url if defined?(@url)
@url = (@src || @srcset || @href || '').delete("\u200b").strip
@url = Addressable::URI.join(base.attr('href') || '', url).to_s if base
return @url if @check.options[:url_swap].empty?
@url = swap(@url, @check.options[:url_swap])
end
|
#valid? ⇒ Boolean
73
74
75
|
# File 'lib/html-proofer/element.rb', line 73
def valid?
!parts.nil?
end
|