Class: Moxml::Adapter::CustomizedOga::XmlGenerator
- Inherits:
-
Oga::XML::Generator
- Object
- Oga::XML::Generator
- Moxml::Adapter::CustomizedOga::XmlGenerator
- Defined in:
- lib/moxml/adapter/customized_oga/xml_generator.rb
Instance Method Summary collapse
- #on_attribute(attr, output) ⇒ Object
- #on_cdata(node, output) ⇒ Object
- #on_element(element, output) ⇒ Object
- #on_namespace_definition(ns, output) ⇒ Object
- #on_processing_instruction(node, output) ⇒ Object
- #on_xml_declaration(node, output) ⇒ Object
- #self_closing?(_element) ⇒ Boolean
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 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 |
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 47 def on_cdata(node, output) # Escape the end sequence return super unless node.text.include?("]]>") chunks = node.text.split(/(\]\]>)/) chunks = ["]]", ">"] if chunks.size == 1 while (index = chunks.index("]]>")) # the end tag cannot be the first and the last at the same time if index.zero? # it's the first text chunk chunks[index] = "]]" chunks[index + 1] = ">#{chunks[index + 1]}" elsif index - 1 == chunks.size # it's the last text chunk chunks[index - 1] += "]]" chunks[index] = ">" else # it's a chunk in the middle chunks[index - 1] += "]]" chunks[index + 1] = ">#{chunks[index + 1]}" chunks.delete_at(index) end end chunks.each do |chunk| output << "<![CDATA[#{chunk}]]>" end output 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. 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}#{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
80 81 82 83 |
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 80 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
85 86 87 88 89 |
# File 'lib/moxml/adapter/customized_oga/xml_generator.rb', line 85 def on_xml_declaration(node, output) super # remove the space before the closing tag output.gsub!(/ \?>$/, "?>") end |
#self_closing?(_element) ⇒ 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 |