Class: SvgConform::Document
- Inherits:
-
Object
- Object
- SvgConform::Document
- Defined in:
- lib/svg_conform/document.rb
Overview
Wrapper around Moxml document for SVG validation
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#moxml_document ⇒ Object
readonly
Returns the value of attribute moxml_document.
Class Method Summary collapse
- .from_content(content) ⇒ Object
- .from_file(file_path) ⇒ Object
-
.from_node(node) ⇒ Object
Create Document from an already-parsed node (Nokogiri or Moxml) Avoids re-parsing when input is already a DOM object.
- .wrap_moxml_element(element) ⇒ Object
Instance Method Summary collapse
-
#attribute_names ⇒ Object
Get all unique attribute names in the document.
- #clear_cache ⇒ Object
- #dup ⇒ Object
-
#element_names ⇒ Object
Get all unique element names in the document.
- #elements ⇒ Object
-
#elements_with_attribute(attr_name) ⇒ Object
Find all elements with specific attributes.
-
#elements_with_style ⇒ Object
Find all elements with style attributes.
-
#find_elements(name) ⇒ Object
Find all elements with a specific name.
-
#has_external_references? ⇒ Boolean
Check if document contains external references.
- #has_svg_namespace_prefix? ⇒ Boolean
- #has_viewbox? ⇒ Boolean
- #height ⇒ Object
-
#initialize(content_or_path) ⇒ Document
constructor
A new instance of Document.
- #namespace_uri ⇒ Object
- #root ⇒ Object
- #svg_elements ⇒ Object
- #svg_namespace? ⇒ Boolean
- #to_xml ⇒ Object
- #traverse ⇒ Object
- #valid_xml? ⇒ Boolean
- #version ⇒ Object
- #viewbox ⇒ Object
- #width ⇒ Object
- #xpath(path, namespaces = {}) ⇒ Object
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_path ⇒ Object (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_document ⇒ Object (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_names ⇒ Object
Get all unique attribute names in the document
171 172 173 174 175 176 177 |
# File 'lib/svg_conform/document.rb', line 171 def attribute_names attributes = [] traverse do |node| attributes.concat(node.attributes.keys) if node.respond_to?(:attributes) end attributes.uniq.sort end |
#clear_cache ⇒ Object
102 103 104 |
# File 'lib/svg_conform/document.rb', line 102 def clear_cache @xpath_cache.clear end |
#dup ⇒ Object
98 99 100 |
# File 'lib/svg_conform/document.rb', line 98 def dup Document.from_content(to_xml) end |
#element_names ⇒ Object
Get all unique element names in the document
166 167 168 |
# File 'lib/svg_conform/document.rb', line 166 def element_names svg_elements.map(&:name).uniq.sort end |
#elements ⇒ Object
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
161 162 163 |
# File 'lib/svg_conform/document.rb', line 161 def elements_with_attribute(attr_name) xpath("//*[@#{attr_name}]") end |
#elements_with_style ⇒ Object
Find all elements with style attributes
156 157 158 |
# File 'lib/svg_conform/document.rb', line 156 def elements_with_style xpath("//*[@style]") end |
#find_elements(name) ⇒ Object
Find all elements with a specific name
147 148 149 150 151 152 153 |
# File 'lib/svg_conform/document.rb', line 147 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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/svg_conform/document.rb', line 180 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
120 121 122 123 124 |
# File 'lib/svg_conform/document.rb', line 120 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
126 127 128 |
# File 'lib/svg_conform/document.rb', line 126 def has_viewbox? root&.attribute("viewBox") end |
#height ⇒ Object
138 139 140 |
# File 'lib/svg_conform/document.rb', line 138 def height root&.attribute("height")&.value end |
#namespace_uri ⇒ Object
112 113 114 |
# File 'lib/svg_conform/document.rb', line 112 def namespace_uri root&.namespace&.uri end |
#root ⇒ Object
66 67 68 |
# File 'lib/svg_conform/document.rb', line 66 def root @moxml_document.root end |
#svg_elements ⇒ Object
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
116 117 118 |
# File 'lib/svg_conform/document.rb', line 116 def svg_namespace? namespace_uri == "http://www.w3.org/2000/svg" end |
#to_xml ⇒ Object
92 93 94 95 96 |
# 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 @moxml_document.to_xml end |
#traverse ⇒ Object
79 80 81 |
# File 'lib/svg_conform/document.rb', line 79 def traverse(&) traverse_node(root, &) if root end |
#valid_xml? ⇒ Boolean
106 107 108 109 110 |
# File 'lib/svg_conform/document.rb', line 106 def valid_xml? !@moxml_document.nil? rescue StandardError false end |
#version ⇒ Object
142 143 144 |
# File 'lib/svg_conform/document.rb', line 142 def version root&.attribute("version")&.value end |
#viewbox ⇒ Object
130 131 132 |
# File 'lib/svg_conform/document.rb', line 130 def viewbox root&.attribute("viewBox")&.value end |
#width ⇒ Object
134 135 136 |
# File 'lib/svg_conform/document.rb', line 134 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 |