Class: SvgConform::Requirements::ColorRestrictionsRequirement

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

Overview

Validates color restrictions (e.g., black and white only for IETF profile)

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, #should_check_node?, #text?, #to_s, #validate_document, #validate_sax_complete

Instance Method Details

#check(node, context) ⇒ Object



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
77
78
79
80
81
# File 'lib/svg_conform/requirements/color_restrictions_requirement.rb', line 26

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

  # Skip attribute validation for structurally invalid nodes (e.g., wrong parent-child)
  return if context.node_structurally_invalid?(node)

  # Check color-related attributes
  color_attributes = %w[fill stroke color stop-color flood-color
                        lighting-color]

  color_attributes.each do |attr_name|
    value = get_attribute(node, attr_name)
    next if value.nil? || value.empty?

    next if valid_color?(value)

    context.add_error(
      requirement_id: id,
      message: "Color '#{value}' in attribute '#{attr_name}' is not allowed in this profile",
      node: node,
      severity: :error,
      data: {
        attribute: attr_name,
        value: value,
        element: node.name,
      },
    )
  end

  # Check style attribute for color properties
  style_value = get_attribute(node, "style")
  return unless style_value

  styles = parse_style(style_value)
  color_properties = %w[fill stroke color stop-color flood-color
                        lighting-color]

  color_properties.each do |prop|
    value = styles[prop]
    next if value.nil? || value.empty?

    next if valid_color?(value)

    context.add_error(
      requirement_id: id,
      message: "Color '#{value}' in style property '#{prop}' is not allowed in this profile",
      node: node,
      severity: :error,
      data: {
        attribute: prop,
        value: value,
        element: node.name,
      },
    )
  end
end

#validate_sax_element(element, context) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/svg_conform/requirements/color_restrictions_requirement.rb', line 83

def validate_sax_element(element, context)
  # Skip attribute validation for structurally invalid nodes
  return if context.node_structurally_invalid?(element)

  # Check color-related attributes
  color_attributes = %w[fill stroke color stop-color flood-color
                        lighting-color]

  color_attributes.each do |attr_name|
    value = element.raw_attributes[attr_name]
    next if value.nil? || value.empty?

    next if valid_color?(value)

    context.add_error(
      requirement_id: id,
      message: "Color '#{value}' in attribute '#{attr_name}' is not allowed in this profile",
      node: element,
      severity: :error,
      data: {
        attribute: attr_name,
        value: value,
        element: element.name,
      },
    )
  end

  # Check style attribute for color properties
  style_value = element.raw_attributes["style"]
  return unless style_value

  styles = parse_style(style_value)
  color_properties = %w[fill stroke color stop-color flood-color
                        lighting-color]

  color_properties.each do |prop|
    value = styles[prop]
    next if value.nil? || value.empty?

    next if valid_color?(value)

    context.add_error(
      requirement_id: id,
      message: "Color '#{value}' in style property '#{prop}' is not allowed in this profile",
      node: element,
      severity: :error,
      data: {
        attribute: prop,
        value: value,
        element: element.name,
      },
    )
  end
end