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
Constant Summary
collapse
- IMAGE_CANDIDATE_REGEX =
/\s*([^,]\S*[^,](?:\s+[^,]+)?)\s*(?:,|$)/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#blank?, #create_nokogiri, #pluralize
Constructor Details
#initialize(runner, node, base_url: nil) ⇒ Element
Returns a new instance of Element.
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/html_proofer/element.rb', line 12
def initialize(runner, node, base_url: nil)
@runner = runner
@node = node
swap_attributes!
@base_url = base_url
@url = Attribute::Url.new(runner, link_attribute, base_url: base_url, source: @runner.current_source, filename: @runner.current_filename)
@line = node.line
@content = node.content
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
10
11
12
|
# File 'lib/html_proofer/element.rb', line 10
def base_url
@base_url
end
|
#content ⇒ Object
Returns the value of attribute content.
10
11
12
|
# File 'lib/html_proofer/element.rb', line 10
def content
@content
end
|
#line ⇒ Object
Returns the value of attribute line.
10
11
12
|
# File 'lib/html_proofer/element.rb', line 10
def line
@line
end
|
#node ⇒ Object
Returns the value of attribute node.
10
11
12
|
# File 'lib/html_proofer/element.rb', line 10
def node
@node
end
|
#url ⇒ Object
Returns the value of attribute url.
10
11
12
|
# File 'lib/html_proofer/element.rb', line 10
def url
@url
end
|
Instance Method Details
#a_tag? ⇒ Boolean
69
70
71
|
# File 'lib/html_proofer/element.rb', line 69
def a_tag?
@node.name == "a"
end
|
#aria_hidden? ⇒ Boolean
77
78
79
|
# File 'lib/html_proofer/element.rb', line 77
def aria_hidden?
@node.attributes["aria-hidden"]&.value == "true"
end
|
#href ⇒ Object
63
64
65
66
67
|
# File 'lib/html_proofer/element.rb', line 63
def href
return if !a_tag? && !link_tag?
@node["href"]
end
|
#ignore? ⇒ Boolean
122
123
124
125
126
127
128
129
|
# File 'lib/html_proofer/element.rb', line 122
def ignore?
return true if @node.attributes["data-proofer-ignore"]
return true if ancestors_ignorable?
return true if url&.ignore?
false
end
|
#img_tag? ⇒ Boolean
45
46
47
|
# File 'lib/html_proofer/element.rb', line 45
def img_tag?
@node.name == "img"
end
|
#link_attribute ⇒ Object
25
26
27
|
# File 'lib/html_proofer/element.rb', line 25
def link_attribute
meta_content || src || srcset || href
end
|
#link_tag? ⇒ Boolean
73
74
75
|
# File 'lib/html_proofer/element.rb', line 73
def link_tag?
@node.name == "link"
end
|
#meta_content ⇒ Object
29
30
31
32
33
|
# File 'lib/html_proofer/element.rb', line 29
def meta_content
return unless meta_tag?
@node["content"]
end
|
35
36
37
|
# File 'lib/html_proofer/element.rb', line 35
def meta_tag?
@node.name == "meta"
end
|
#multiple_sizes? ⇒ Boolean
106
107
108
109
110
111
112
|
# File 'lib/html_proofer/element.rb', line 106
def multiple_sizes?
return false if blank?(srcsets)
srcsets.any? do |srcset|
!blank?(srcset) && srcset.split(" ").size > 1
end
end
|
#multiple_srcsets? ⇒ Boolean
81
82
83
|
# File 'lib/html_proofer/element.rb', line 81
def multiple_srcsets?
!blank?(srcset) && srcset.split(",").size > 1
end
|
#script_tag? ⇒ Boolean
49
50
51
|
# File 'lib/html_proofer/element.rb', line 49
def script_tag?
@node.name == "script"
end
|
#source_tag? ⇒ Boolean
59
60
61
|
# File 'lib/html_proofer/element.rb', line 59
def source_tag?
@node.name == "source"
end
|
#src ⇒ Object
39
40
41
42
43
|
# File 'lib/html_proofer/element.rb', line 39
def src
return if !img_tag? && !script_tag? && !source_tag?
@node["src"]
end
|
#srcset ⇒ Object
53
54
55
56
57
|
# File 'lib/html_proofer/element.rb', line 53
def srcset
return if !img_tag? && !source_tag?
@node["srcset"]
end
|
#srcsets ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/html_proofer/element.rb', line 98
def srcsets
return if blank?(srcset)
srcset.split(IMAGE_CANDIDATE_REGEX).select.with_index do |_part, idx|
idx.odd?
end.map(&:strip)
end
|
#srcsets_wo_sizes ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/html_proofer/element.rb', line 114
def srcsets_wo_sizes
return if blank?(srcsets)
srcsets.map do |srcset|
srcset.split(" ").first
end
end
|