Class: SvgConform::Remediations::ViewboxRemediation

Inherits:
BaseRemediation
  • Object
show all
Defined in:
lib/svg_conform/remediations/viewbox_remediation.rb

Overview

Remediation action for viewBox-related issues that matches svgcheck behavior

Instance Method Summary collapse

Methods inherited from BaseRemediation

#execute, #should_execute?, #to_s

Instance Method Details

#apply(document, _context) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/svg_conform/remediations/viewbox_remediation.rb', line 11

def apply(document, _context)
  changes = []

  # Get the root SVG element directly from the document
  svg_element = document.root

  if svg_element
    # Check if viewBox is missing or malformed
    current_viewbox = get_attribute(svg_element, "viewBox")

    if current_viewbox.nil? || current_viewbox.empty?
      changes.concat(add_viewbox_from_dimensions(svg_element))
    elsif current_viewbox =~ /[()]/ || current_viewbox !~ /^[\d.\s-]+$/
      # Malformed viewBox - try to fix it
      changes.concat(fix_malformed_viewbox(svg_element, current_viewbox))
    end
  end

  changes
end