Class: Nokogiri::HTML::DocumentFragment

Inherits:
XML::DocumentFragment show all
Defined in:
lib/nokogiri/html/document_fragment.rb

Constant Summary

Constants inherited from XML::Node

XML::Node::ATTRIBUTE_DECL, XML::Node::ATTRIBUTE_NODE, XML::Node::CDATA_SECTION_NODE, XML::Node::COMMENT_NODE, XML::Node::DOCB_DOCUMENT_NODE, XML::Node::DOCUMENT_FRAG_NODE, XML::Node::DOCUMENT_NODE, XML::Node::DOCUMENT_TYPE_NODE, XML::Node::DTD_NODE, XML::Node::ELEMENT_DECL, XML::Node::ELEMENT_NODE, XML::Node::ENTITY_DECL, XML::Node::ENTITY_NODE, XML::Node::ENTITY_REF_NODE, XML::Node::HTML_DOCUMENT_NODE, XML::Node::NAMESPACE_DECL, XML::Node::NOTATION_NODE, XML::Node::PI_NODE, XML::Node::TEXT_NODE, XML::Node::XINCLUDE_END, XML::Node::XINCLUDE_START

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XML::DocumentFragment

#css, #name, new, #to_html, #to_s, #to_xhtml, #to_xml

Methods inherited from XML::Node

#<<, #<=>, #==, #>, #[], #[]=, #accept, #add_child, #add_namespace_definition, #add_next_sibling, #add_previous_sibling, #after, #ancestors, #at, #at_css, #at_xpath, #attribute, #attribute_nodes, #attribute_with_ns, #attributes, #before, #blank?, #canonicalize, #cdata?, #child, #children, #children=, #comment?, #content, #content=, #create_external_subset, #create_internal_subset, #css, #css_path, #decorate!, #default_namespace=, #description, #do_xinclude, #document, #dup, #each, #element?, #element_children, #encode_special_chars, #external_subset, #first_element_child, #fragment, #fragment?, #html?, #inner_html, #inner_html=, #internal_subset, #key?, #keys, #last_element_child, #line, #matches?, #namespace, #namespace=, #namespace_definitions, #namespace_scopes, #namespaced_key?, #namespaces, new, #next_element, #next_sibling, #node_name, #node_name=, #node_type, #parent, #parent=, #parse, #path, #pointer_id, #previous_element, #previous_sibling, #read_only?, #remove_attribute, #replace, #search, #serialize, #swap, #text?, #to_html, #to_s, #to_xhtml, #to_xml, #traverse, #unlink, #values, #write_html_to, #write_to, #write_xhtml_to, #write_xml_to, #xml?, #xpath

Methods included from XML::PP::Node

#inspect, #pretty_print

Constructor Details

#initialize(document, tags = nil, ctx = nil) ⇒ DocumentFragment

Returns a new instance of DocumentFragment.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nokogiri/html/document_fragment.rb', line 17

def initialize document, tags = nil, ctx = nil
  return self unless tags

  if ctx
    preexisting_errors = document.errors.dup
    node_set = ctx.parse("<div>#{tags}</div>")
    node_set.first.children.each { |child| child.parent = self } unless node_set.empty?
    self.errors = document.errors - preexisting_errors
  else
    # This is a horrible hack, but I don't care
    if tags.strip =~ /^<body/i
      path = "/html/body"
    else
      path = "/html/body/node()"
    end

    temp_doc = HTML::Document.parse "<html><body>#{tags}", nil, document.encoding
    temp_doc.xpath(path).each { |child| child.parent = self }
    self.errors = temp_doc.errors
  end
  children
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/nokogiri/html/document_fragment.rb', line 4

def errors
  @errors
end

Class Method Details

.parse(tags, encoding = nil) ⇒ Object

Create a Nokogiri::XML::DocumentFragment from tags, using encoding



8
9
10
11
12
13
14
15
# File 'lib/nokogiri/html/document_fragment.rb', line 8

def self.parse tags, encoding = nil
  doc = HTML::Document.new

  encoding ||= tags.respond_to?(:encoding) ? tags.encoding.name : 'UTF-8'
  doc.encoding = encoding

  new(doc, tags)
end