Class: Dommy::XMLSerializer
- Inherits:
-
Object
- Object
- Dommy::XMLSerializer
- Includes:
- Bridge::Methods
- Defined in:
- lib/dommy/dom_parser.rb
Overview
‘XMLSerializer` — round-trip a node tree to a string. Used for XML output, SVG inlining, and “serialize this Element” patterns. For HTML, prefer `Element#outer_html` directly.
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(_key) ⇒ Object
-
#serialize_to_string(node) ⇒ Object
(also: #serializeToString)
WHATWG “XML serialization” — produce XML (self-closing empty tags, real XML escaping) rather than the HTML serialization ‘outer_html` gives.
Methods included from Bridge::Methods
Instance Method Details
#__js_call__(method, args) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/dommy/dom_parser.rb', line 95 def __js_call__(method, args) case method when "serializeToString" serialize_to_string(args[0]) end end |
#__js_get__(_key) ⇒ Object
89 90 91 |
# File 'lib/dommy/dom_parser.rb', line 89 def __js_get__(_key) nil end |
#serialize_to_string(node) ⇒ Object Also known as: serializeToString
WHATWG “XML serialization” — produce XML (self-closing empty tags, real XML escaping) rather than the HTML serialization ‘outer_html` gives. Delegates to the backend’s XML serializer (Nokogiri); namespace handling is whatever the backend produces. A Document serializes its root element.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dommy/dom_parser.rb', line 71 def serialize_to_string(node) return "" unless node backend = if node.respond_to?(:nokogiri_doc) && !node.respond_to?(:__dommy_backend_node__) node.nokogiri_doc # a Document elsif node.respond_to?(:__dommy_backend_node__) node.__dommy_backend_node__ end return node.to_s unless backend.respond_to?(:to_xml) opts = ::Nokogiri::XML::Node::SaveOptions::AS_XML | ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION target = backend.respond_to?(:root) && backend.respond_to?(:document?) && backend.document? ? backend.root : backend target ? target.to_xml(save_with: opts) : "" end |