Class: Moxml::Adapter::CustomizedOga::XmlGenerator

Inherits:
Oga::XML::Generator
  • Object
show all
Defined in:
lib/moxml/adapter/customized_oga/xml_generator.rb

Instance Method Summary collapse

Instance Method Details

#on_attribute(attr, output) ⇒ Object



41
42
43
44
45
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 41

def on_attribute(attr, output)
  return super unless attr.value&.include?("'")

  output << %(#{attr.expanded_name}="#{encode(attr.value)}")
end

#on_cdata(node, output) ⇒ Object



47
48
49
50
51
52
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 47

def on_cdata(node, output)
  # Split any embedded end sequence into adjacent sections
  return super unless node.text.include?("]]>")

  output << ::Moxml::XmlEmitter.cdata(node.text)
end

#on_element(element, output) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 16

def on_element(element, output)
  name = element.expanded_name

  attrs = []
  element.attributes.each do |attr|
    attrs << " "
    on_attribute(attr, attrs)
  end

  closing_tag = if self_closing?(element)
                  html_void_element?(element) ? ">" : " />"
                else
                  ">"
                end

  output << "<#{name}#{attrs.join}#{closing_tag}"
end

#on_namespace_definition(ns, output) ⇒ Object



34
35
36
37
38
39
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 34

def on_namespace_definition(ns, output)
  name = "xmlns"
  name += ":#{ns.name}" unless ns.name.nil?

  output << %(#{name}="#{ns.uri}")
end

#on_processing_instruction(node, output) ⇒ Object



54
55
56
57
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 54

def on_processing_instruction(node, output)
  # put the space between the name and text
  output << "<?#{node.name} #{node.text}?>"
end

#on_xml_declaration(node, output) ⇒ Object



59
60
61
62
63
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 59

def on_xml_declaration(node, output)
  super
  # remove the space before the closing tag
  output.gsub!(/ \?>$/, "?>")
end

#self_closing?(_element) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 11

def self_closing?(_element)
  # Always expand tags
  false
end