Class: SvgConform::Remediations::NamespaceRemediation

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

Overview

Remediation action for namespace-related issues

Instance Method Summary collapse

Methods inherited from BaseRemediation

#execute, #should_execute?, #to_s

Instance Method Details

#apply(document, _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
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
# File 'lib/svg_conform/remediations/namespace_remediation.rb', line 26

def apply(document, _context)
  changes = []
  default_namespace

  # Skip xmlns handling - assume it's already correct in most cases
  # (This avoids duplicate xmlns attribute issues)

  # Remove invalid namespace elements and attributes based on configuration
  nodes_to_remove = []

  document.traverse do |node|
    next unless element?(node)

    # Check if element has invalid namespace using Moxml's namespace API
    if remove_elements && node.respond_to?(:namespace) && node.namespace
      namespace_uri = get_namespace_uri(node.namespace)

      if namespace_uri && !allowed_namespaces.include?(namespace_uri)
        nodes_to_remove << node
        changes << {
          type: :element_removed,
          description: "Removed invalid namespace element: #{node.name} (#{namespace_uri})",
          node_name: node.name,
          namespace_uri: namespace_uri,
        }
        next # Skip attribute checking for elements we're removing
      end
    end

    # Remove invalid namespace attributes from all elements
    if remove_attributes && node.respond_to?(:attributes) && node.attributes
      invalid_attrs = []

      node.attributes.each do |attr_name, attr_value|
        # Handle both string keys and Moxml::Attribute objects
        name_str = attr_name.respond_to?(:name) ? attr_name.name : attr_name.to_s

        # For root SVG element, remove xmlns declarations for disallowed namespaces
        if remove_declarations && node.name == "svg" && name_str.start_with?("xmlns:")
          name_str.sub("xmlns:", "")
          value_str = attr_value.respond_to?(:value) ? attr_value.value : attr_value.to_s

          if !allowed_namespaces.include?(value_str)
            invalid_attrs << name_str
          end
        end

        # For all elements, remove attributes with invalid namespaces
        # Check if attribute has namespace information (Moxml::Attribute objects)
        if attr_name.respond_to?(:namespace) && attr_name.namespace
          attr_namespace_uri = get_namespace_uri(attr_name.namespace)

          if attr_namespace_uri && !allowed_namespaces.include?(attr_namespace_uri)
            invalid_attrs << name_str
          end
        end
      end

      # Remove invalid attributes
      invalid_attrs.each do |attr_name|
        remove_attribute(node, attr_name)
        description = if attr_name.start_with?("xmlns:")
                        "Removed invalid namespace declaration: #{attr_name}"
                      else
                        "Removed invalid namespace attribute: #{attr_name}"
                      end

        changes << {
          type: :attribute_removed,
          description: description,
          node_name: node.name,
          attribute_name: attr_name,
        }
      end
    end
  end

  # Remove invalid namespace elements
  nodes_to_remove.each do |node|
    remove_node(node)
  end

  changes
end