Class: SvgConform::Requirements::IdReferenceRequirement
- Inherits:
-
BaseRequirement
- Object
- Lutaml::Model::Serializable
- BaseRequirement
- SvgConform::Requirements::IdReferenceRequirement
show all
- Defined in:
- lib/svg_conform/requirements/id_reference_requirement.rb
Instance Method Summary
collapse
#check, #element?, #get_attribute, #get_attributes, #has_attribute?, #remove_attribute, #set_attribute, #should_check_node?, #text?, #to_s, #validate_sax_element
Instance Method Details
#collect_sax_data(element, _context) ⇒ Object
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
68
69
70
71
72
73
74
75
|
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 21
def collect_sax_data(element, _context)
@collected_ids ||= Set.new
@collected_url_refs ||= []
@collected_href_refs ||= []
@collected_other_refs ||= []
id_value = element.raw_attributes["id"]
@collected_ids.add(id_value) if id_value && !id_value.empty?
url_attributes = %w[fill stroke marker-start marker-mid marker-end
clip-path mask filter]
url_attributes.each do |attr_name|
attr_value = element.raw_attributes[attr_name]
next unless attr_value
url_refs = (attr_value)
url_refs.each do |ref_id|
@collected_url_refs << [element, ref_id, attr_name]
end
end
style_attr = element.raw_attributes["style"]
if style_attr
url_refs = (style_attr)
url_refs.each do |ref_id|
@collected_url_refs << [element, ref_id, "style"]
end
end
href_value = element.raw_attributes["href"] || element.raw_attributes["xlink:href"]
if href_value&.start_with?("#")
ref_id = href_value[1..]
@collected_href_refs << [element, ref_id]
end
id_ref_attributes = %w[for aria-labelledby aria-describedby
aria-controls aria-owns]
id_ref_attributes.each do |attr_name|
attr_value = element.raw_attributes[attr_name]
next unless attr_value
ref_ids = attr_value.split(/\s+/)
ref_ids.each do |ref_id|
next if ref_id.empty?
@collected_other_refs << [element, ref_id, attr_name]
end
end
end
|
#needs_deferred_validation? ⇒ Boolean
9
10
11
|
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 9
def needs_deferred_validation?
true
end
|
#reset_state ⇒ Object
Reset state before each validation run to prevent state leakage
14
15
16
17
18
19
|
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 14
def reset_state
@collected_ids = Set.new
@collected_url_refs = []
@collected_href_refs = []
@collected_other_refs = []
end
|
#validate_document(document, context) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 119
def validate_document(document, context)
ids = Set.new
document.xpath("//*[@id]").each do |element|
id_value = element["id"]
ids.add(id_value) if id_value && !id_value.empty?
end
check_url_references(document, ids, context)
check_href_references(document, ids, context)
check_other_references(document, ids, context)
end
|
#validate_sax_complete(context) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 77
def validate_sax_complete(context)
return unless @collected_url_refs && @collected_href_refs && @collected_other_refs && @collected_ids
@collected_url_refs.each do |element, ref_id, attr_name|
next if @collected_ids.include?(ref_id)
message = if attr_name == "style"
"Reference to undefined ID '#{ref_id}' in style attribute"
else
"Reference to undefined ID '#{ref_id}' in attribute '#{attr_name}'"
end
context.add_error(
node: element,
message: message,
requirement_id: id,
)
end
@collected_href_refs.each do |element, ref_id|
next if @collected_ids.include?(ref_id)
context.add_error(
node: element,
message: "Reference to undefined ID '#{ref_id}' in href attribute",
requirement_id: id,
)
end
@collected_other_refs.each do |element, ref_id, attr_name|
next if @collected_ids.include?(ref_id)
context.add_error(
node: element,
message: "Reference to undefined ID '#{ref_id}' in #{attr_name} attribute",
requirement_id: id,
)
end
end
|