Class: SvgConform::Requirements::AllowedElementsRequirement

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

Overview

Validates that only allowed SVG elements and their attributes are used

Constant Summary collapse

RDF_NAMESPACES =

RDF-related namespaces (same as in NamespaceRequirement for consistency)

[
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  "http://creativecommons.org/ns#",
  "http://purl.org/dc/elements/1.1/",
  "http://purl.org/dc/dcmitype/",
  "http://www.w3.org/2000/01/rdf-schema#",
].freeze

Instance Method Summary collapse

Methods inherited from BaseRequirement

#collect_sax_data, #element?, #get_attribute, #get_attributes, #has_attribute?, #needs_deferred_validation?, #remove_attribute, #set_attribute, #text?, #to_s, #validate_document, #validate_sax_complete

Instance Method Details

#check(node, context) ⇒ Object



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
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
118
119
120
121
# File 'lib/svg_conform/requirements/allowed_elements_requirement.rb', line 50

def check(node, context)
  return unless element?(node)

  # Skip foreign namespace elements if configured (let NamespaceRequirement handle them)
  if skip_foreign_namespaces && foreign_namespace?(node)
    return
  end

  element_name = node.name

  # Check if element is explicitly disallowed
  if disallowed_element?(element_name)
    context.add_error(
      requirement_id: id,
      message: "Element '#{element_name}' is not allowed in this profile",
      node: node,
      severity: :error,
      data: { element: element_name },
    )
    return
  end

  # Check parent-child relationships
  if check_parent_child && node.parent && element?(node.parent)
    parent_name = node.parent.name
    if invalid_parent_child?(parent_name, element_name)
      context.add_error(
        requirement_id: id,
        message: "The element '#{element_name}' is not allowed as a child of '#{parent_name}'",
        node: node,
        severity: :error,
        data: { element: element_name, parent: parent_name },
      )
      # Mark node AND descendants as structurally invalid
      # svgcheck does not validate attributes of forbidden children - just reports one error
      context.mark_node_structurally_invalid(node)
      return
    end
  end

  # Check if element is in allowed list
  if element_configs&.any?
    allowed_elements = element_configs.map(&:tag)
    unless allowed_elements.include?(element_name)
      context.add_error(
        requirement_id: id,
        message: "Element '#{element_name}' is not allowed in this profile",
        node: node,
        severity: :error,
        data: { element: element_name },
      )
      # Mark as structurally invalid so children aren't validated
      # (matches svgcheck behavior: invalid element removed with all children)
      context.mark_node_structurally_invalid(node)
      return
    end
  end

  # Collect all potential attribute errors, then apply priority rules
  potential_errors = collect_attribute_errors(node)
  prioritized_errors = prioritize_errors(potential_errors)

  # Add the prioritized errors to the context
  prioritized_errors.each do |error|
    context.add_error(
      requirement_id: id,
      message: error[:message],
      node: node,
      severity: :error,
    )
  end
end

#validate_sax_element(element, context) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/svg_conform/requirements/allowed_elements_requirement.rb', line 123

def validate_sax_element(element, context)
  # Skip if parent is structurally invalid (matches DOM behavior)
  if element.parent && context.node_structurally_invalid?(element.parent)
    # Mark this element as invalid too since it won't be in final document
    context.mark_node_structurally_invalid(element)
    return
  end

  # Skip foreign namespace elements if configured (let NamespaceRequirement handle them)
  if skip_foreign_namespaces && foreign_namespace_sax?(element)
    return
  end

  element_name = element.name

  # Check if element is explicitly disallowed
  if disallowed_element?(element_name)
    context.add_error(
      requirement_id: id,
      message: "Element '#{element_name}' is not allowed in this profile",
      node: element,
      severity: :error,
      data: { element: element_name },
    )
    return
  end

  # Check parent-child relationships
  if check_parent_child && element.parent
    parent_name = element.parent.name
    if invalid_parent_child?(parent_name, element_name)
      context.add_error(
        requirement_id: id,
        message: "The element '#{element_name}' is not allowed as a child of '#{parent_name}'",
        node: element,
        severity: :error,
        data: { element: element_name, parent: parent_name },
      )
      # Mark node as structurally invalid
      context.mark_node_structurally_invalid(element)
      return
    end
  end

  # Check if element is in allowed list
  if element_configs&.any?
    allowed_elements = element_configs.map(&:tag)
    unless allowed_elements.include?(element_name)
      context.add_error(
        requirement_id: id,
        message: "Element '#{element_name}' is not allowed in this profile",
        node: element,
        severity: :error,
        data: { element: element_name },
      )
      # Mark as structurally invalid
      context.mark_node_structurally_invalid(element)
      return
    end
  end

  # Validate attributes
  potential_errors = collect_attribute_errors_sax(element)
  prioritized_errors = prioritize_errors(potential_errors)

  # Add the prioritized errors to the context
  prioritized_errors.each do |error|
    context.add_error(
      requirement_id: id,
      message: error[:message],
      node: element,
      severity: :error,
    )
  end
end