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
clean_content, create_nokogiri, #pluralize, swap
Constructor Details
#initialize(obj, check) ⇒ Element
Returns a new instance of Element.
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
|
# File 'lib/html-proofer/element.rb', line 11
def initialize(obj, check)
obj.attributes.each_pair do |attribute, value|
name = "#{attribute.tr('-:.', '_')}".to_sym
(class << self; self; end).send(:attr_reader, name)
instance_variable_set("@#{name}", value.value)
end
@aria_hidden = @aria_hidden == "true" ? true : false
@text = obj.content
@check = check
@checked_paths = {}
@type = check.class.name
@line = obj.line
@html = check.html
parent_attributes = obj.ancestors.map { |a| a.try(:attributes) }
parent_attributes.pop
@parent_ignorable = parent_attributes.any? { |a| !a["data-proofer-ignore"].nil? }
@href.insert 0, 'http:' if @href =~ %r{^//}
@src.insert 0, 'http:' if @src =~ %r{^//}
@srcset.insert 0, 'http:' if @srcset =~ %r{^//}
end
|
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def alt
@alt
end
|
#data_proofer_ignore ⇒ Object
Returns the value of attribute data_proofer_ignore.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def data_proofer_ignore
@data_proofer_ignore
end
|
#href ⇒ Object
Returns the value of attribute href.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def href
@href
end
|
#id ⇒ Object
Returns the value of attribute id.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def id
@id
end
|
#line ⇒ Object
Returns the value of attribute line.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def line
@line
end
|
#link ⇒ Object
Returns the value of attribute link.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def link
@link
end
|
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def name
@name
end
|
#src ⇒ Object
Returns the value of attribute src.
9
10
11
|
# File 'lib/html-proofer/element.rb', line 9
def src
@src
end
|
Instance Method Details
#absolute_path ⇒ Object
174
175
176
177
|
# File 'lib/html-proofer/element.rb', line 174
def absolute_path
path = file_path || @check.path
File.expand_path path, Dir.pwd
end
|
#allow_hash_href? ⇒ Boolean
103
104
105
|
# File 'lib/html-proofer/element.rb', line 103
def allow_hash_href?
@check.options[:allow_hash_href]
end
|
#base ⇒ Object
199
200
201
|
# File 'lib/html-proofer/element.rb', line 199
def base
@base ||= @html.at_css('base')
end
|
#check_img_http? ⇒ Boolean
107
108
109
|
# File 'lib/html-proofer/element.rb', line 107
def check_img_http?
@check.options[:check_img_http]
end
|
#check_sri? ⇒ Boolean
111
112
113
|
# File 'lib/html-proofer/element.rb', line 111
def check_sri?
@check.options[:check_sri]
end
|
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
169
170
171
172
|
# File 'lib/html-proofer/element.rb', line 169
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
116
117
118
|
# File 'lib/html-proofer/element.rb', line 116
def external?
!internal?
end
|
#file_path ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/html-proofer/element.rb', line 137
def file_path
return if path.nil?
path_dot_ext = ''
if @check.options[:assume_extension]
path_dot_ext = path + @check.options[:extension]
end
if path =~ %r{^/}
base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src)
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 File.directory?(file) && !unslashed_directory?(file)
file = File.join file, @check.options[:directory_index_file]
elsif @check.options[:assume_extension] && File.file?("#{file}#{@check.options[:extension]}")
file = "#{file}#{@check.options[:extension]}"
end
file
end
|
#follow_location? ⇒ Boolean
195
196
197
|
# File 'lib/html-proofer/element.rb', line 195
def follow_location?
@check.options[:typhoeus] && @check.options[:typhoeus][:followlocation]
end
|
#hash ⇒ Object
63
64
65
|
# File 'lib/html-proofer/element.rb', line 63
def hash
parts.fragment unless parts.nil?
end
|
#hash_link ⇒ Object
125
126
127
|
# File 'lib/html-proofer/element.rb', line 125
def hash_link
url.start_with?('#')
end
|
#html ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/html-proofer/element.rb', line 203
def html
if internal?
if hash_link || param_link
@html
elsif slash_link
create_nokogiri(absolute_path)
end
end
end
|
#ignore? ⇒ Boolean
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/html-proofer/element.rb', line 80
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
95
96
97
|
# File 'lib/html-proofer/element.rb', line 95
def ignore_alt?
return true if ignores_pattern_check(@check.options[:alt_ignore]) || @aria_hidden
end
|
#ignore_empty_alt? ⇒ Boolean
99
100
101
|
# File 'lib/html-proofer/element.rb', line 99
def ignore_empty_alt?
@check.options[:empty_alt_ignore]
end
|
#ignores_pattern_check(links) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/html-proofer/element.rb', line 179
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
121
122
123
|
# File 'lib/html-proofer/element.rb', line 121
def internal?
hash_link || param_link || slash_link
end
|
#non_http_remote? ⇒ Boolean
76
77
78
|
# File 'lib/html-proofer/element.rb', line 76
def non_http_remote?
!scheme.nil? && !remote?
end
|
#param_link ⇒ Object
129
130
131
|
# File 'lib/html-proofer/element.rb', line 129
def param_link
url.start_with?('?')
end
|
#parts ⇒ Object
53
54
55
56
57
|
# File 'lib/html-proofer/element.rb', line 53
def parts
@parts ||= Addressable::URI.parse url
rescue URI::Error, Addressable::URI::InvalidURIError => e
@parts = nil
end
|
#path ⇒ Object
59
60
61
|
# File 'lib/html-proofer/element.rb', line 59
def path
Addressable::URI.unencode parts.path unless parts.nil?
end
|
#remote? ⇒ Boolean
path is to an external server
72
73
74
|
# File 'lib/html-proofer/element.rb', line 72
def remote?
%w( http https ).include? scheme
end
|
#scheme ⇒ Object
67
68
69
|
# File 'lib/html-proofer/element.rb', line 67
def scheme
parts.scheme unless parts.nil?
end
|
#slash_link ⇒ Object
133
134
135
|
# File 'lib/html-proofer/element.rb', line 133
def slash_link
url.start_with?('|')
end
|
#unslashed_directory?(file) ⇒ Boolean
191
192
193
|
# File 'lib/html-proofer/element.rb', line 191
def unslashed_directory?(file)
File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location?
end
|
#url ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/html-proofer/element.rb', line 39
def url
return @url if defined?(@url)
@url = (@src || @srcset || @href || '').gsub("\u200b", '')
if base
@url = Addressable::URI.join(base.attr('href'), url).to_s
end
return @url if @check.options[:url_swap].empty?
@url = swap(@url, @check.options[:url_swap])
end
|
#valid? ⇒ Boolean
49
50
51
|
# File 'lib/html-proofer/element.rb', line 49
def valid?
!parts.nil?
end
|