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

Attributes inherited from XML::DocumentFragment

#parse_options

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, #clone, #comment?, #content, #content=, #create_external_subset, #create_internal_subset, #css_path, #data_ptr?, #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, context = nil, options = {}) ⇒ DocumentFragment

Create a document fragment.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nokogiri/html5/document_fragment.rb', line 80

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

  tags = Nokogiri::HTML5.read_and_encode(tags, nil)

  context = options.delete(:context) if options.key?(:context)

  options[:max_attributes] ||= Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES
  options[:max_errors] ||= options.delete(:max_parse_errors) || Nokogiri::Gumbo::DEFAULT_MAX_ERRORS
  options[:max_tree_depth] ||= Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH

  Nokogiri::Gumbo.fragment(self, tags, context, **options)
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



69
70
71
# File 'lib/nokogiri/html5/document_fragment.rb', line 69

def document
  @document
end

#errorsObject

Returns the value of attribute errors.



70
71
72
# File 'lib/nokogiri/html5/document_fragment.rb', line 70

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



77
78
79
# File 'lib/nokogiri/html5/document_fragment.rb', line 77

def quirks_mode
  @quirks_mode
end

Class Method Details

.parse(tags, encoding = nil, positional_options_hash = nil, **options) ⇒ Object

:call-seq:

parse(tags, **options)
parse(tags, encoding = nil, **options)

Parse an HTML5 document fragment from tags, returning a Nodeset.

Parameters
  • tags [String, IO] The HTML5 document fragment to parse.

  • encoding [String] The name of the encoding to use when parsing the document fragment. (default nil)

Also see Nokogiri::HTML5 for a longer explanation of how encoding is handled by the parser.

Options
  • :context [String, Nokogiri::XML::Node] The context in which to parse the document fragment. (default “body”)

  • :max_errors [Integer] The maximum number of parse errors to record. (default Nokogiri::Gumbo::DEFAULT_MAX_ERRORS which is currently 0)

  • :max_tree_depth [Integer] The maximum depth of the parse tree. (default Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH)

  • :max_attributes [Integer] The maximum number of attributes allowed on an element. (default Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES)

  • :parse_noscript_content_as_text [Boolean] Whether to parse the content of noscript elements as text. (default false)

Also see Nokogiri::HTML5 for a longer explanation of the options.

Returns
  • Nokogiri::XML::NodeSet

    A node set containing the root nodes of the parsed fragment.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nokogiri/html5/document_fragment.rb', line 53

def parse(tags, encoding = nil, positional_options_hash = nil, **options)
  unless positional_options_hash.nil?
    warn("Nokogiri::HTML5::DocumentFragment.parse: Passing options as an explicit hash is deprecated. Use keyword arguments instead. This will become an error in a future release.", uplevel: 1, category: :deprecated)
    options.merge!(positional_options_hash)
  end

  context = options.delete(:context)

  document = HTML5::Document.new
  document.encoding = "UTF-8"
  tags = HTML5.read_and_encode(tags, encoding)

  new(document, tags, context, options)
end

Instance Method Details

#extract_params(params) ⇒ Object

:nodoc:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nokogiri/html5/document_fragment.rb', line 102

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:



96
97
98
99
100
# File 'lib/nokogiri/html5/document_fragment.rb', line 96

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