Class: SvgConform::Requirements::NamespaceRequirement
- Inherits:
-
BaseRequirement
- Object
- Lutaml::Model::Serializable
- BaseRequirement
- SvgConform::Requirements::NamespaceRequirement
- Defined in:
- lib/svg_conform/requirements/namespace_requirement.rb
Overview
Validates that SVG documents have proper namespace declarations
Constant Summary collapse
- RDF_NAMESPACES =
RDF-related namespaces commonly found in SVG metadata
[ "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "http://creativecommons.org/ns#", "http://purl.org/dc/elements/1.1/", "http://purl.org/dc/dcmitype/", "http://www.w3.org/2000/01/rdf-schema#", ].freeze
Instance Method Summary collapse
- #check(node, context) ⇒ Object
- #validate_document(document, context) ⇒ Object
- #validate_sax_element(element, context) ⇒ Object
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_sax_complete
Instance Method Details
#check(node, context) ⇒ Object
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/svg_conform/requirements/namespace_requirement.rb', line 120 def check(node, context) return unless element?(node) # Debug: Check all elements puts "DEBUG: Checking element: #{node.name}" if node.name.include?(":") # Check if this element has a namespace element_namespace = nil # Try to get namespace from the element if node.respond_to?(:namespace) && node.namespace namespace_str = node.namespace.to_s puts "DEBUG: namespace_str = #{namespace_str}" if node.name.include?(":") # Extract the URI from the namespace string element_namespace = ::Regexp.last_match(1) if namespace_str =~ /xmlns[^=]*="([^"]+)"/ end # If no namespace found, check if element has a prefix (indicating it's namespaced) if element_namespace.nil? && node.name.include?(":") prefix = node.name.split(":").first puts "DEBUG: Found prefixed element #{node.name}, prefix = #{prefix}" element_namespace = find_namespace_uri_for_prefix(node, prefix) puts "DEBUG: Found namespace URI = #{element_namespace}" end # Skip if no namespace (default SVG namespace) if element_namespace.nil? || element_namespace.empty? puts "DEBUG: Skipping #{node.name} - no namespace found" if node.name.include?(":") return end puts "DEBUG: Element #{node.name} has namespace #{element_namespace}" # Check against allowed namespaces if configured # If allow_rdf_metadata is enabled, also allow RDF namespaces effective_allowed_namespaces = allowed_namespaces if effective_allowed_namespaces = allowed_namespaces + RDF_NAMESPACES end if effective_allowed_namespaces && !effective_allowed_namespaces.empty? && !effective_allowed_namespaces.include?(element_namespace) puts "DEBUG: Adding error for disallowed namespace #{element_namespace}" context.add_error( requirement_id: id, message: "The namespace #{element_namespace} is not permitted for svg elements.", node: node, severity: :error, data: { element_name: node.name, namespace: element_namespace, allowed_namespaces: effective_allowed_namespaces, }, ) return end # Check against disallowed namespaces if configured return unless disallowed_namespaces && !disallowed_namespaces.empty? && disallowed_namespaces.include?(element_namespace) puts "DEBUG: Adding error for explicitly disallowed namespace #{element_namespace}" context.add_error( requirement_id: id, message: "The namespace #{element_namespace} is not permitted for svg elements.", node: node, severity: :error, data: { element_name: node.name, namespace: element_namespace, disallowed_namespaces: disallowed_namespaces, }, ) end |
#validate_document(document, context) ⇒ Object
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 110 111 112 113 114 115 116 117 118 |
# File 'lib/svg_conform/requirements/namespace_requirement.rb', line 38 def validate_document(document, context) root = document.root return unless root # Check if root element is svg unless root.name == "svg" context.add_error( requirement: self, node: root, message: "Root element must be 'svg'", data: { element_name: root.name }, ) return end # Check for SVG namespace - Moxml handles namespace differently svg_namespace = nil # Try to get namespace from Moxml's namespace method if root.respond_to?(:namespace) && root.namespace namespace_str = root.namespace.to_s # Extract the URI from the namespace string like 'xmlns="http://www.w3.org/2000/svg"' svg_namespace = ::Regexp.last_match(1) if namespace_str =~ /xmlns="([^"]+)"/ end # Fallback to checking xmlns attribute svg_namespace ||= get_attribute(root, "xmlns") # Default namespace (empty string) should be treated as SVG namespace svg_namespace = "" if svg_namespace.nil? # Check against allowed namespaces if configured if allowed_namespaces && !allowed_namespaces.empty? && !allowed_namespaces.include?(svg_namespace) context.add_error( requirement: self, node: root, message: "Namespace '#{svg_namespace}' is not allowed. Only #{allowed_namespaces.join(', ')} are permitted", data: { current_namespace: svg_namespace, allowed_namespaces: allowed_namespaces, }, ) return end # Check against disallowed namespaces if configured (legacy support) if disallowed_namespaces && !disallowed_namespaces.empty? && disallowed_namespaces.include?(svg_namespace) context.add_error( requirement: self, node: root, message: "Namespace '#{svg_namespace}' is not allowed", data: { current_namespace: svg_namespace, disallowed_namespaces: disallowed_namespaces, }, ) return end # Default behavior: require SVG namespace unless allowed_namespaces.empty? && disallowed_namespaces.empty? # Now check all elements in the document for namespace violations check_all_elements(document, context) return end return if svg_namespace == "http://www.w3.org/2000/svg" context.add_error( requirement: self, node: root, message: "SVG namespace declaration missing or incorrect", data: { current_namespace: svg_namespace, expected_namespace: "http://www.w3.org/2000/svg", }, ) # Also check all elements for namespace violations check_all_elements(document, context) end |
#validate_sax_element(element, context) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/svg_conform/requirements/namespace_requirement.rb', line 193 def validate_sax_element(element, context) # Check if this element has a namespace element_namespace = get_element_namespace_sax(element) # Skip if no namespace (default SVG namespace) return if element_namespace.nil? || element_namespace.empty? # Check against allowed namespaces if configured # If allow_rdf_metadata is enabled, also allow RDF namespaces effective_allowed_namespaces = allowed_namespaces if effective_allowed_namespaces = allowed_namespaces + RDF_NAMESPACES end if effective_allowed_namespaces && !effective_allowed_namespaces.empty? && !effective_allowed_namespaces.include?(element_namespace) context.add_error( requirement_id: id, message: "The namespace #{element_namespace} is not permitted for svg elements.", node: element, severity: :error, data: { element_name: element.name, namespace: element_namespace, allowed_namespaces: effective_allowed_namespaces, }, ) return end # Check against disallowed namespaces if configured return unless disallowed_namespaces && !disallowed_namespaces.empty? && disallowed_namespaces.include?(element_namespace) context.add_error( requirement_id: id, message: "The namespace #{element_namespace} is not permitted for svg elements.", node: element, severity: :error, data: { element_name: element.name, namespace: element_namespace, disallowed_namespaces: disallowed_namespaces, }, ) end |