Class: SvgConform::Requirements::IdReferenceRequirement

Inherits:
BaseRequirement
  • Object
show all
Defined in:
lib/svg_conform/requirements/id_reference_requirement.rb

Defined Under Namespace

Classes: State

Instance Method Summary collapse

Methods inherited from BaseRequirement

#check, #get_attributes, #should_check_node?, #skip_attribute_validation?, #to_s, #validate_sax_element

Methods included from Interfaces::RequirementInterface

#check, #reset_state, #should_check_node?, #to_s, #validate_sax_element

Methods included from NodeHelpers

#element?, #get_attribute, #has_attribute?, #remove_attribute, #set_attribute, #text?

Instance Method Details

#collect_sax_data(element, context) ⇒ Object



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
76
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 25

def collect_sax_data(element, context)
  state = context.state_for(self)
  # Collect IDs
  id_value = element.raw_attributes["id"]
  if id_value && !id_value.empty?
    state.collected_ids.add(id_value)
  end

  # Collect url() references
  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 = extract_url_references(attr_value)
    url_refs.each do |ref_id|
      state.url_refs << [element, ref_id, attr_name]
    end
  end

  # Check style attribute for url() references
  style_attr = element.raw_attributes["style"]
  if style_attr
    url_refs = extract_url_references(style_attr)
    url_refs.each do |ref_id|
      state.url_refs << [element, ref_id, "style"]
    end
  end

  # Collect href references
  href_value = element.raw_attributes["href"] || element.raw_attributes["xlink:href"]
  if href_value&.start_with?("#")
    ref_id = href_value[1..] # Remove #
    state.href_refs << [element, ref_id]
  end

  # Collect other ID references
  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?

      state.other_refs << [element, ref_id, attr_name]
    end
  end
end

#needs_deferred_validation?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 21

def needs_deferred_validation?
  true
end

#validate_document(document, context) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 120

def validate_document(document, context)
  # Collect all IDs in the document
  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 references to IDs
  check_url_references(document, ids, context)
  check_href_references(document, ids, context)
  check_other_references(document, ids, context)
end

#validate_sax_complete(context) ⇒ Object



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
118
# File 'lib/svg_conform/requirements/id_reference_requirement.rb', line 78

def validate_sax_complete(context)
  state = context.state_for(self)
  return unless state.url_refs && state.href_refs && state.other_refs && state.collected_ids

  # Validate all collected references
  state.url_refs.each do |element, ref_id, attr_name|
    next if state.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

  state.href_refs.each do |element, ref_id|
    next if state.collected_ids.include?(ref_id)

    context.add_error(
      node: element,
      message: "Reference to undefined ID '#{ref_id}' in href attribute",
      requirement_id: id,
    )
  end

  state.other_refs.each do |element, ref_id, attr_name|
    next if state.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