Class: SvgConform::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_conform/document.rb

Overview

Wrapper around Moxml document for SVG validation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_or_path) ⇒ Document

Returns a new instance of Document.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/svg_conform/document.rb', line 11

def initialize(content_or_path)
  if File.exist?(content_or_path.to_s)
    @file_path = content_or_path
    @content = File.read(@file_path)
  else
    @file_path = nil
    @content = content_or_path
  end

  @xpath_cache = {}
  parse_document
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



9
10
11
# File 'lib/svg_conform/document.rb', line 9

def file_path
  @file_path
end

#moxml_documentObject (readonly)

Returns the value of attribute moxml_document.



9
10
11
# File 'lib/svg_conform/document.rb', line 9

def moxml_document
  @moxml_document
end

Class Method Details

.from_content(content) ⇒ Object



28
29
30
# File 'lib/svg_conform/document.rb', line 28

def self.from_content(content)
  new(content)
end

.from_file(file_path) ⇒ Object



24
25
26
# File 'lib/svg_conform/document.rb', line 24

def self.from_file(file_path)
  new(file_path)
end

.from_node(node) ⇒ Object

Create Document from an already-parsed node (Nokogiri or Moxml) Avoids re-parsing when input is already a DOM object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/svg_conform/document.rb', line 34

def self.from_node(node)
  doc = allocate
  doc.instance_variable_set(:@file_path, nil)
  doc.instance_variable_set(:@xpath_cache, {})

  # Convert node to Moxml document
  if node.respond_to?(:to_xml) && node.class.name.start_with?("Moxml")
    # Already a Moxml node, wrap it
    doc.instance_variable_set(:@content, nil) # Don't store XML string
    doc.instance_variable_set(:@moxml_document,
                              node.is_a?(Moxml::Document) ? node : wrap_moxml_element(node))
  elsif defined?(Nokogiri) && node.is_a?(Nokogiri::XML::Node)
    # Nokogiri node - must serialize to convert to Moxml
    # This is unavoidable due to different DOM implementations
    xml_string = node.to_xml
    doc.instance_variable_set(:@content, xml_string)
    doc.send(:parse_document)
  else
    raise ArgumentError,
          "Invalid input type: #{node.class}. Expected Moxml or Nokogiri DOM node."
  end

  doc
end

.wrap_moxml_element(element) ⇒ Object



59
60
61
62
63
64
# File 'lib/svg_conform/document.rb', line 59

def self.wrap_moxml_element(element)
  # If it's an element, we need to wrap it in a document
  # For now, parse its XML representation
  context = Moxml.new
  context.parse(element.to_xml)
end

Instance Method Details

#attribute_namesObject

Get all unique attribute names in the document



195
196
197
198
199
200
201
# File 'lib/svg_conform/document.rb', line 195

def attribute_names
  attributes = []
  traverse do |node|
    attributes.concat(node.attributes.keys) if node.respond_to?(:attributes)
  end
  attributes.uniq.sort
end

#clear_cacheObject



126
127
128
# File 'lib/svg_conform/document.rb', line 126

def clear_cache
  @xpath_cache.clear
end

#dupObject



122
123
124
# File 'lib/svg_conform/document.rb', line 122

def dup
  Document.from_content(to_xml)
end

#element_namesObject

Get all unique element names in the document



190
191
192
# File 'lib/svg_conform/document.rb', line 190

def element_names
  svg_elements.map(&:name).uniq.sort
end

#elementsObject



70
71
72
# File 'lib/svg_conform/document.rb', line 70

def elements
  @moxml_document.elements
end

#elements_with_attribute(attr_name) ⇒ Object

Find all elements with specific attributes



185
186
187
# File 'lib/svg_conform/document.rb', line 185

def elements_with_attribute(attr_name)
  xpath("//*[@#{attr_name}]")
end

#elements_with_styleObject

Find all elements with style attributes



180
181
182
# File 'lib/svg_conform/document.rb', line 180

def elements_with_style
  xpath("//*[@style]")
end

#find_elements(name) ⇒ Object

Find all elements with a specific name



171
172
173
174
175
176
177
# File 'lib/svg_conform/document.rb', line 171

def find_elements(name)
  if has_svg_namespace_prefix?
    xpath("//svg:#{name}", { "svg" => "http://www.w3.org/2000/svg" })
  else
    xpath("//#{name}[namespace-uri()='http://www.w3.org/2000/svg']")
  end
end

#has_external_references?Boolean

Check if document contains external references

Returns:



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
# File 'lib/svg_conform/document.rb', line 204

def has_external_references?
  # Check for external stylesheets
  link_elements = if has_svg_namespace_prefix?
                    xpath('//svg:link[@rel="stylesheet"]',
                          { "svg" => "http://www.w3.org/2000/svg" })
                  else
                    xpath('//link[@rel="stylesheet"][namespace-uri()="http://www.w3.org/2000/svg"]')
                  end
  return true if link_elements.any?

  # Check for @import in style elements
  style_elements = if has_svg_namespace_prefix?
                     xpath("//svg:style",
                           { "svg" => "http://www.w3.org/2000/svg" })
                   else
                     xpath("//style[namespace-uri()='http://www.w3.org/2000/svg']")
                   end

  style_elements.each do |style|
    content = style.text
    return true if content&.include?("@import")
  end

  # Check for external references in style attributes
  elements_with_style.each do |element|
    style_value = element.attribute("style")&.value
    return true if style_value&.include?("url(")
  end

  false
end

#has_svg_namespace_prefix?Boolean

Returns:



144
145
146
147
148
# File 'lib/svg_conform/document.rb', line 144

def has_svg_namespace_prefix?
  # Check the current document state, not cached content
  xml = @moxml_document.to_xml
  xml.include?("xmlns:svg=") || xml.include?("svg:")
end

#has_viewbox?Boolean

Returns:



150
151
152
# File 'lib/svg_conform/document.rb', line 150

def has_viewbox?
  root&.attribute("viewBox")
end

#heightObject



162
163
164
# File 'lib/svg_conform/document.rb', line 162

def height
  root&.attribute("height")&.value
end

#namespace_uriObject



136
137
138
# File 'lib/svg_conform/document.rb', line 136

def namespace_uri
  root&.namespace&.uri
end

#remove_namespace_declarations(xml, prefixes) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/svg_conform/document.rb', line 111

def remove_namespace_declarations(xml, prefixes)
  # Remove xmlns:prefix="uri" declarations from the XML string
  # This works around Moxml/Nokogiri's inability to remove namespace declarations
  prefixes.reduce(xml) do |result, prefix|
    # Match xmlns:prefix="..." with various quote styles and surrounding whitespace
    # The pattern matches: xmlns:prefix="uri" or xmlns:prefix='uri'
    # It captures leading whitespace to remove it too
    result.gsub(%r{\s+xmlns:#{prefix}=["'][^"']*["']}, "")
  end
end

#rootObject



66
67
68
# File 'lib/svg_conform/document.rb', line 66

def root
  @moxml_document.root
end

#svg_elementsObject



83
84
85
86
87
88
89
90
# File 'lib/svg_conform/document.rb', line 83

def svg_elements
  # Handle both default namespace and prefixed namespace
  if has_svg_namespace_prefix?
    xpath("//svg:*", { "svg" => "http://www.w3.org/2000/svg" }).select { |node| node.respond_to?(:name) }
  else
    xpath("//*[namespace-uri()='http://www.w3.org/2000/svg']").select { |node| node.respond_to?(:name) }
  end
end

#svg_namespace?Boolean

Returns:



140
141
142
# File 'lib/svg_conform/document.rb', line 140

def svg_namespace?
  namespace_uri == "http://www.w3.org/2000/svg"
end

#to_xmlObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/svg_conform/document.rb', line 92

def to_xml
  # Always generate from current moxml_document state
  # This ensures modifications are reflected in the output
  xml = @moxml_document.to_xml

  # Clean up unused namespace declarations if marked by remediations
  if instance_variable_defined?(:@unused_namespace_prefixes)
    prefixes = @unused_namespace_prefixes
    if prefixes && !prefixes.empty?
      xml = remove_namespace_declarations(xml,
                                          prefixes)
    end
    # Clear the marker after cleanup
    remove_instance_variable(:@unused_namespace_prefixes)
  end

  xml
end

#traverseObject



79
80
81
# File 'lib/svg_conform/document.rb', line 79

def traverse(&)
  traverse_node(root, &) if root
end

#valid_xml?Boolean

Returns:



130
131
132
133
134
# File 'lib/svg_conform/document.rb', line 130

def valid_xml?
  !@moxml_document.nil?
rescue StandardError
  false
end

#versionObject



166
167
168
# File 'lib/svg_conform/document.rb', line 166

def version
  root&.attribute("version")&.value
end

#viewboxObject



154
155
156
# File 'lib/svg_conform/document.rb', line 154

def viewbox
  root&.attribute("viewBox")&.value
end

#widthObject



158
159
160
# File 'lib/svg_conform/document.rb', line 158

def width
  root&.attribute("width")&.value
end

#xpath(path, namespaces = {}) ⇒ Object



74
75
76
77
# File 'lib/svg_conform/document.rb', line 74

def xpath(path, namespaces = {})
  cache_key = [path, namespaces].hash
  @xpath_cache[cache_key] ||= @moxml_document.xpath(path, namespaces)
end