Class: Rfcxml::V3::Rfc

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/rfcxml/v3/rfc.rb

Instance Method Summary collapse

Instance Method Details

#to_xml(options = {}) ⇒ Object

Override to_xml to fix SVG elements SVG elements need xmlns="http://www.w3.org/2000/svg" attribute lutaml-model doesn't properly output xmlns when serializing SVG and it incorrectly handles elements (treats them as text content)



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
# File 'lib/rfcxml/v3/rfc.rb', line 89

def to_xml(options = {})
  xml = super
  return xml unless xml

  # Post-process to ensure SVG elements have xmlns attribute
  doc = Nokogiri::XML(xml)
  svg_needs_fix = doc.xpath("//svg[not(@xmlns)]").any? ||
    doc.xpath("//svg//*[@xmlns='http://www.w3.org/2000/svg']").any?

  return xml unless svg_needs_fix

  doc.xpath("//svg[not(@xmlns)]").each do |svg_elem|
    svg_elem["xmlns"] = "http://www.w3.org/2000/svg"
  end

  # Remove redundant xmlns from child elements within SVG
  doc.xpath("//svg//*[@xmlns='http://www.w3.org/2000/svg']").each do |child|
    child.delete("xmlns")
  end

  # Preserve XML declaration if it was in the original output
  if xml.start_with?("<?xml")
    doc.to_xml
  else
    doc.root.to_xml
  end
end