Class: Nokogiri::HTML5::DocumentFragment

Inherits:
Nokogiri::HTML4::DocumentFragment show all
Defined in:
lib/nokogiri/html5/document_fragment.rb

Overview

Since v1.12.0

💡 HTML5 functionality is not available when running JRuby.

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::DECONSTRUCT_KEYS, XML::Node::DECONSTRUCT_METHODS, 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

Constants included from ClassResolver

ClassResolver::VALID_NAMESPACES

Constants included from XML::Searchable

XML::Searchable::LOOKS_LIKE_XPATH

Constants included from XML::PP::Node

XML::PP::Node::COLLECTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XML::DocumentFragment

#css, #deconstruct, #dup, #fragment, #name, new, #search, #to_html, #to_s, #to_xhtml, #to_xml

Methods inherited from XML::Node

#<<, #<=>, #==, #[], #[]=, #accept, #add_child, #add_class, #add_namespace_definition, #add_next_sibling, #add_previous_sibling, #after, #ancestors, #append_class, #attribute, #attribute_nodes, #attribute_with_ns, #attributes, #before, #blank?, #canonicalize, #cdata?, #child, #children, #children=, #classes, #comment?, #content, #content=, #create_external_subset, #create_internal_subset, #css_path, #deconstruct_keys, #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, #kwattr_add, #kwattr_append, #kwattr_remove, #kwattr_values, #lang, #lang=, #last_element_child, #line, #line=, #matches?, #namespace, #namespace=, #namespace_definitions, #namespace_scopes, #namespaced_key?, #namespaces, #native_content=, new, #next_element, #next_sibling, #node_name, #node_name=, #node_type, #parent, #parent=, #parse, #path, #pointer_id, #prepend_child, #previous_element, #previous_sibling, #processing_instruction?, #read_only?, #remove_attribute, #remove_class, #replace, #swap, #text?, #to_html, #to_s, #to_xhtml, #to_xml, #traverse, #unlink, #value?, #values, #wrap, #write_html_to, #write_to, #write_xhtml_to, #write_xml_to, #xml?

Methods included from ClassResolver

#related_class

Methods included from XML::Searchable

#>, #at, #at_css, #at_xpath, #css, #search, #xpath

Methods included from XML::PP::Node

#inspect, #pretty_print

Methods included from Node

#fragment, #inner_html, #write_to

Constructor Details

#initialize(doc, tags = nil, ctx = nil, options = {}) ⇒ DocumentFragment

Create a document fragment.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nokogiri/html5/document_fragment.rb', line 39

def initialize(doc, tags = nil, ctx = nil, options = {}) # rubocop:disable Lint/MissingSuper
  self.document = doc
  self.errors = []
  return self unless tags

  max_attributes = options[:max_attributes] || Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES
  max_errors = options[:max_errors] || Nokogiri::Gumbo::DEFAULT_MAX_ERRORS
  max_depth = options[:max_tree_depth] || Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH
  tags = Nokogiri::HTML5.read_and_encode(tags, nil)
  Nokogiri::Gumbo.fragment(self, tags, ctx, max_attributes, max_errors, max_depth)
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



28
29
30
# File 'lib/nokogiri/html5/document_fragment.rb', line 28

def document
  @document
end

#errorsObject

Returns the value of attribute errors.



29
30
31
# File 'lib/nokogiri/html5/document_fragment.rb', line 29

def errors
  @errors
end

#quirks_modeObject (readonly)

Get the parser’s quirks mode value. See HTML5::QuirksMode.

This method returns ‘nil` if the parser was not invoked (e.g., `Nokogiri::HTML5::DocumentFragment.new(doc)`).

Since v1.14.0



36
37
38
# File 'lib/nokogiri/html5/document_fragment.rb', line 36

def quirks_mode
  @quirks_mode
end

Class Method Details

.parse(tags, encoding = nil, options = {}) ⇒ Object

Parse a document fragment from tags, returning a Nodeset.



58
59
60
61
62
63
# File 'lib/nokogiri/html5/document_fragment.rb', line 58

def self.parse(tags, encoding = nil, options = {})
  doc = HTML5::Document.new
  tags = HTML5.read_and_encode(tags, encoding)
  doc.encoding = "UTF-8"
  new(doc, tags, nil, options)
end

Instance Method Details

#extract_params(params) ⇒ Object

:nodoc:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nokogiri/html5/document_fragment.rb', line 65

def extract_params(params) # :nodoc:
  handler = params.find do |param|
    ![Hash, String, Symbol].include?(param.class)
  end
  params -= [handler] if handler

  hashes = []
  while Hash === params.last || params.last.nil?
    hashes << params.pop
    break if params.empty?
  end
  ns, binds = hashes.reverse

  ns ||=
    begin
      ns = {}
      children.each { |child| ns.merge!(child.namespaces) }
      ns
    end

  [params, handler, ns, binds]
end

#serialize(options = {}, &block) ⇒ Object

:nodoc:



51
52
53
54
55
# File 'lib/nokogiri/html5/document_fragment.rb', line 51

def serialize(options = {}, &block) # :nodoc:
  # Bypass XML::Document.serialize which doesn't support options even
  # though XML::Node.serialize does!
  XML::Node.instance_method(:serialize).bind_call(self, options, &block)
end