Class: Itonoko::HTML::DocumentFragment

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

Constant Summary

Constants inherited from XML::Node

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

Instance Attribute Summary

Attributes inherited from XML::Node

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XML::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?, #to_xml, #xpath

Constructor Details

#initialize(document = nil) ⇒ DocumentFragment

Returns a new instance of DocumentFragment.



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

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

Class Method Details

.parse(markup, document = nil) ⇒ Object



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

def self.parse(markup, document = nil)
  require_relative "../parser/html_parser"
  frag = new(document)
  doc  = Parser::HtmlParser.new.parse("<_itonoko_frag>#{markup}</_itonoko_frag>")
  wrapper = doc.root || doc.children.first
  source = wrapper&.name == "_itonoko_frag" ? wrapper.children : (doc.children)
  source.each { |c| frag.add_child(c) }
  frag
end

Instance Method Details

#to_htmlObject



20
21
22
# File 'lib/itonoko/html/document_fragment.rb', line 20

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

#to_sObject



24
25
26
# File 'lib/itonoko/html/document_fragment.rb', line 24

def to_s
  to_html
end