Class: Itonoko::XML::DocumentFragment

Inherits:
Node
  • Object
show all
Defined in:
lib/itonoko/xml/document_fragment.rb

Constant Summary

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ESCAPE_ATTR, Node::ESCAPE_TEXT, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary

Attributes inherited from Node

#children, #document, #node_name, #node_type, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #[], #[]=, #add_child, #add_next_sibling, #add_previous_sibling, #ancestors, #at, #at_css, #at_xpath, #attribute, #attribute_nodes, #attributes, #cdata_node?, #child, #comment?, #css, #description, #document?, #element?, #element_children, #fragment?, #get_attribute, #has_attribute?, #inner_html, #inner_html=, #inspect, #keys, #matches?, #name, #next_element, #next_sibling, #prepend_child, #previous_element, #previous_sibling, #remove, #remove_attribute, #replace, #root, #search, #set_attribute, #text, #text=, #text?, #xpath

Constructor Details

#initialize(document = nil) ⇒ DocumentFragment

Returns a new instance of DocumentFragment.



6
7
8
# File 'lib/itonoko/xml/document_fragment.rb', line 6

def initialize(document = nil)
  super(DOCUMENT_FRAGMENT_NODE, "#document-fragment", document)
end

Class Method Details

.parse(markup, document = nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/itonoko/xml/document_fragment.rb', line 10

def self.parse(markup, document = nil)
  require_relative "../parser/xml_parser"
  frag = new(document)
  doc  = Parser::XmlParser.new.parse("<_root>#{markup}</_root>")
  doc.root.children.each { |c| frag.add_child(c) }
  frag
end

Instance Method Details

#to_htmlObject



18
19
20
# File 'lib/itonoko/xml/document_fragment.rb', line 18

def to_html
  children.map(&:to_html).join
end

#to_sObject



26
27
28
# File 'lib/itonoko/xml/document_fragment.rb', line 26

def to_s
  to_html
end

#to_xml(options = {}) ⇒ Object



22
23
24
# File 'lib/itonoko/xml/document_fragment.rb', line 22

def to_xml(options = {})
  children.map { |c| c.to_xml(options) }.join
end