Class: SvgConform::Remediations::InvalidIdReferencesRemediation

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

Overview

Remediation action for fixing invalid ID references Based on the Lucid SVG fix script behavior

Instance Method Summary collapse

Methods inherited from BaseRemediation

#execute, #should_execute?, #to_s

Instance Method Details

#apply(document, _context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/svg_conform/remediations/invalid_id_references_remediation.rb', line 16

def apply(document, _context)
  changes = []

  # Find all use elements with invalid href references
  document.traverse do |node|
    next unless node.respond_to?(:name) && node.name == "use"

    href = get_attribute(node,
                         "href") || get_attribute(node, "xlink:href")
    next unless href&.start_with?("#")

    referenced_id = href[1..] # Remove the #
    next if document.xpath("//*[@id='#{referenced_id}']").any? # ID exists, skip

    # Handle invalid ID reference
    change = handle_use_element(node,
                                { href: href, invalid_id: referenced_id })
    changes << change if change
  end

  changes
end