Class: SvgConform::Requirements::ViewboxRequiredRequirement

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

Overview

Validates that SVG documents have proper viewBox attributes

Instance Method Summary collapse

Methods inherited from BaseRequirement

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

Instance Method Details

#validate_document(document, context) ⇒ Object



17
18
19
20
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
76
77
78
79
80
81
82
83
84
# File 'lib/svg_conform/requirements/viewbox_required_requirement.rb', line 17

def validate_document(document, context)
  root = document.root
  return unless root&.name == "svg"

  viewbox = get_attribute(root, "viewBox")

  if viewbox.nil? || viewbox.empty?
    context.add_error(
      requirement: self,
      node: root,
      message: "SVG root element must have a viewBox attribute",
      data: { missing_attribute: "viewBox" },
    )

    # Add informational message about calculated viewBox if width/height are present
    width = get_attribute(root, "width")
    height = get_attribute(root, "height")

    if width && height && valid_number?(width) && valid_number?(height)
      calculated_viewbox = "0 0 #{width.to_f} #{height.to_f}"
      context.add_error(
        requirement: self,
        node: root,
        message: "Trying to put in the attribute with value '#{calculated_viewbox}'",
        data: {
          calculated_viewbox: calculated_viewbox,
          source_width: width,
          source_height: height,
        },
      )
    end
    return
  end

  # Validate viewBox format (should be "min-x min-y width height")
  # Also accept comma-separated with parentheses like "(0, 0, 100, 100)" (svgcheck is lenient)
  normalized_viewbox = viewbox.strip.gsub(/[(),]/, " ").squeeze(" ")
  parts = normalized_viewbox.strip.split(/\s+/)
  unless parts.length == 4 && parts.all? { |part| valid_number?(part) }
    context.add_error(
      requirement: self,
      node: root,
      message: "viewBox attribute must contain four numeric values (min-x min-y width height)",
      data: {
        viewbox_value: viewbox,
        parsed_parts: parts,
      },
    )
    return
  end

  # Check that width and height are positive
  width = parts[2].to_f
  height = parts[3].to_f

  return unless width <= 0 || height <= 0

  context.add_error(
    requirement: self,
    node: root,
    message: "viewBox width and height must be positive values",
    data: {
      viewbox_value: viewbox,
      width: width,
      height: height,
    },
  )
end

#validate_sax_element(element, context) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/svg_conform/requirements/viewbox_required_requirement.rb', line 86

def validate_sax_element(element, context)
  # Only check the root SVG element
  return unless element.name == "svg" && element.parent.nil?

  viewbox = element.raw_attributes["viewBox"]

  if viewbox.nil? || viewbox.empty?
    context.add_error(
      requirement_id: id,
      message: "SVG root element must have a viewBox attribute",
      node: element,
      severity: :error,
      data: { missing_attribute: "viewBox" },
    )

    # Add informational message about calculated viewBox if width/height are present
    width = element.raw_attributes["width"]
    height = element.raw_attributes["height"]

    if width && height && valid_number?(width) && valid_number?(height)
      calculated_viewbox = "0 0 #{width.to_f} #{height.to_f}"
      context.add_error(
        requirement_id: id,
        message: "Trying to put in the attribute with value '#{calculated_viewbox}'",
        node: element,
        severity: :error,
        data: {
          calculated_viewbox: calculated_viewbox,
          source_width: width,
          source_height: height,
        },
      )
    end
    return
  end

  # Validate viewBox format (should be "min-x min-y width height")
  normalized_viewbox = viewbox.strip.gsub(/[(),]/, " ").squeeze(" ")
  parts = normalized_viewbox.strip.split(/\s+/)
  unless parts.length == 4 && parts.all? { |part| valid_number?(part) }
    context.add_error(
      requirement_id: id,
      message: "viewBox attribute must contain four numeric values (min-x min-y width height)",
      node: element,
      severity: :error,
      data: {
        viewbox_value: viewbox,
        parsed_parts: parts,
      },
    )
    return
  end

  # Check that width and height are positive
  width = parts[2].to_f
  height = parts[3].to_f

  return unless width <= 0 || height <= 0

  context.add_error(
    requirement_id: id,
    message: "viewBox width and height must be positive values",
    node: element,
    severity: :error,
    data: {
      viewbox_value: viewbox,
      width: width,
      height: height,
    },
  )
end