Class: Nokogiri::HTML5::DocumentFragment
- Inherits:
-
Nokogiri::HTML4::DocumentFragment
- Object
- Nokogiri::HTML4::DocumentFragment
- Nokogiri::HTML5::DocumentFragment
- Defined in:
- lib/nokogiri/html5/document_fragment.rb
Overview
Since v1.12.0
💡 HTML5 functionality is not available when running JRuby.
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#quirks_mode ⇒ Object
readonly
Get the parser's quirks mode value.
Class Method Summary collapse
-
.parse(input, encoding_ = nil, positional_options_hash = nil, encoding: encoding_, **options) ⇒ Object
:call-seq: parse(input, **options) → HTML5::DocumentFragment.
Instance Method Summary collapse
-
#extract_params(params) ⇒ Object
:nodoc:.
-
#initialize(doc, input = nil, context_ = nil, positional_options_hash = nil, context: context_, **options) ⇒ DocumentFragment
constructor
:call-seq: new(document, input, **options) → HTML5::DocumentFragment.
-
#serialize(options = {}, &block) ⇒ Object
:nodoc:.
Constructor Details
#initialize(doc, input = nil, context_ = nil, positional_options_hash = nil, context: context_, **options) ⇒ DocumentFragment
:call-seq:
new(document, input, **options) → HTML5::DocumentFragment
Parse HTML5 fragment input from a String, and return a new HTML5::DocumentFragment.
💡 It's recommended to use either HTML5::DocumentFragment.parse or HTML5::Node#fragment rather than call this method directly.
[Required Parameters]
document(HTML5::Document) The parent document to associate the returned fragment with.
[Optional Parameters]
input(String) The content to be parsed.
[Optional Keyword Arguments]
-
encoding:(String | Encoding) The encoding, or name of the encoding, that should be used when processing the document. When not provided, the encoding will be determined based on the document content. Also see Nokogiri::HTML5 for a longer explanation of how encoding is handled by the parser. -
context:(String | Nokogiri::XML::Node) The node, or the name of an HTML5 element, in which to parse the document fragment. (default"body") -
max_errors:(Integer) The maximum number of parse errors to record. (defaultNokogiri::Gumbo::DEFAULT_MAX_ERRORSwhich is currently 0) -
max_tree_depth:(Integer) The maximum depth of the parse tree. (defaultNokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH) -
max_attributes:(Integer) The maximum number of attributes allowed on an element. (defaultNokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES) -
parse_noscript_content_as_text:(Boolean) Whether to parse the content ofnoscriptelements as text. (defaultfalse)
See rdoc-ref:HTML5@Parsing+options for a complete description of these parsing options.
[Returns] HTML5::DocumentFragment
Context Node
If a context node is specified using context:, then the parser will behave as if that
Node, or a hypothetical tag named as specified, is the parent of the fragment subtree.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 144 def initialize( doc, input = nil, context_ = nil, = nil, context: context_, ** ) # rubocop:disable Lint/MissingSuper unless .nil? || .empty? .merge!() end @document = doc @errors = [] return self unless input input = Nokogiri::HTML5.read_and_encode(input, nil) context = .delete(:context) if .key?(:context) [:max_attributes] ||= Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES [:max_errors] ||= .delete(:max_parse_errors) || Nokogiri::Gumbo::DEFAULT_MAX_ERRORS [:max_tree_depth] ||= Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH Nokogiri::Gumbo.fragment(self, input, context, **) end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
88 89 90 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 88 def document @document end |
#errors ⇒ Object
Returns the value of attribute errors.
89 90 91 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 89 def errors @errors end |
#quirks_mode ⇒ Object (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
97 98 99 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 97 def quirks_mode @quirks_mode end |
Class Method Details
.parse(input, encoding_ = nil, positional_options_hash = nil, encoding: encoding_, **options) ⇒ Object
:call-seq:
parse(input, **options) → HTML5::DocumentFragment
Parse HTML5 fragment input from a String, and return a new HTML5::DocumentFragment. This method creates a new, empty HTML5::Document to contain the fragment.
[Parameters]
input(String | IO) The HTML5 document fragment to parse.
[Optional Keyword Arguments]
-
encoding:(String | Encoding) The encoding, or name of the encoding, that should be used when processing the document. When not provided, the encoding will be determined based on the document content. Also see Nokogiri::HTML5 for a longer explanation of how encoding is handled by the parser. -
context:(String | Nokogiri::XML::Node) The node, or the name of an HTML5 element, "in context" of which to parse the document fragment. See below for more information. (default"body") -
max_errors:(Integer) The maximum number of parse errors to record. (defaultNokogiri::Gumbo::DEFAULT_MAX_ERRORSwhich is currently 0) -
max_tree_depth:(Integer) The maximum depth of the parse tree. (defaultNokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH) -
max_attributes:(Integer) The maximum number of attributes allowed on an element. (defaultNokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES) -
parse_noscript_content_as_text:(Boolean) Whether to parse the content ofnoscriptelements as text. (defaultfalse)
See rdoc-ref:HTML5@Parsing+options for a complete description of these parsing options.
[Returns] Nokogiri::HTML5::DocumentFragment
Context Node
If a context node is specified using context:, then the parser will behave as if that
Node, or a hypothetical tag named as specified, is the parent of the fragment subtree.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 69 def parse( input, encoding_ = nil, = nil, encoding: encoding_, ** ) unless .nil? || .empty? .merge!() end context = .delete(:context) document = HTML5::Document.new document.encoding = "UTF-8" input = HTML5.read_and_encode(input, encoding) new(document, input, context, ) end |
Instance Method Details
#extract_params(params) ⇒ Object
:nodoc:
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 175 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:
169 170 171 172 173 |
# File 'lib/nokogiri/html5/document_fragment.rb', line 169 def serialize( = {}, &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, , &block) end |