Class: Nokogiri::HTML5::DocumentFragment
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Nokogiri::HTML4::DocumentFragment
|
Defined in: | lib/nokogiri/html5/document_fragment.rb |
Overview
Since v1.12.0
💡 ::Nokogiri::HTML5
functionality is not available when running JRuby.
Constant Summary
::Nokogiri::XML::PP::Node
- Included
::Nokogiri::XML::Searchable
- Included
::Nokogiri::ClassResolver
- Included
::Nokogiri::XML::Node
- Inherited
ATTRIBUTE_DECL, ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DECONSTRUCT_KEYS, DECONSTRUCT_METHODS, DOCB_DOCUMENT_NODE, DOCUMENT_FRAG_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, DTD_NODE, ELEMENT_DECL, ELEMENT_NODE, ENTITY_DECL, ENTITY_NODE, ENTITY_REF_NODE, HTML_DOCUMENT_NODE, IMPLIED_XPATH_CONTEXTS, NAMESPACE_DECL, NOTATION_NODE, PI_NODE, TEXT_NODE, XINCLUDE_END, XINCLUDE_START
Class Method Summary
-
.new(document, input, **options) → HTML5::DocumentFragment) ⇒ DocumentFragment
constructor
Parse HTML5 fragment input from a String, and return a new
DocumentFragment
. -
.parse(input, **options) → HTML5::DocumentFragment)
Parse HTML5 fragment input from a String, and return a new
DocumentFragment
.
::Nokogiri::HTML4::DocumentFragment
- Inherited
.new | Parse HTML4 fragment input from a String, and return a new |
.parse | Parse HTML4 fragment input from a String, and return a new |
::Nokogiri::XML::DocumentFragment
- Inherited
.parse | Parse XML fragment input from a String, and return a new |
.native_new, | |
.new | Wrapper method to separate the concerns of: - the native object allocator’s parameter (it only requires #document) - the initializer’s parameters. |
::Nokogiri::XML::Node
- Inherited
.new | documented in lib/nokogiri/xml/node.rb. |
Instance Attribute Summary
- #document rw
- #errors rw
-
#quirks_mode
readonly
Get the parser’s quirks mode value.
::Nokogiri::XML::DocumentFragment
- Inherited
#errors | A list of |
#parse_options | The options used to parse the document fragment. |
#errors= |
::Nokogiri::XML::Node
- Inherited
#blank? |
|
#cdata? | Returns true if this is a CDATA. |
#children | :category: Traversing Document Structure. |
#children= | Set the content for this |
#comment? | Returns true if this is a Comment. |
#content | [Returns]. |
#content= | Set the content of this node to |
#default_namespace= | Adds a default namespace supplied as a string |
#document | :category: Traversing Document Structure. |
#document? | Returns true if this is a |
#elem? | Alias for XML::Node#element?. |
#element? | Returns true if this is an Element node. |
#fragment? | Returns true if this is a |
#html? | Returns true if this is an |
#inner_html | Get the inner_html for this node’s |
#inner_html= | Set the content for this |
#inner_text | Alias for XML::Node#content. |
#lang | Searches the language of a node, i.e. |
#lang= | Set the language of a node, i.e. |
#line |
|
#line= | Sets the line for this |
#name | Alias for XML::Node#node_name. |
#namespace |
|
#namespace= | Set the default namespace on this node (as would be defined with an “xmlns=” attribute in |
#native_content= | Set the content of this node to |
#next | Alias for XML::Node#next_sibling. |
#next= | Alias for XML::Node#add_next_sibling. |
#node_name | Returns the name for this |
#node_name= | Set the name for this |
#parent | |
#parent= | |
#previous | Alias for XML::Node#previous_sibling. |
#previous= | Alias for XML::Node#add_previous_sibling. |
#processing_instruction? | Returns true if this is a ProcessingInstruction node. |
#read_only? | Is this a read only node? |
#text | Alias for XML::Node#content. |
#text? | Returns true if this is a Text node. |
#to_str | Alias for XML::Node#content. |
#xml? | Returns true if this is an |
#prepend_newline?, #data_ptr? |
Instance Method Summary
- #extract_params(params) Internal use only
- #serialize(options = {}, &block) Internal use only
::Nokogiri::XML::DocumentFragment
- Inherited
#css | Search this fragment for |
#deconstruct | Returns the root nodes of this document fragment as an array, to use in pattern matching. |
#dup, #fragment, | |
#initialize | Parse XML fragment input from a String, and return a new |
#name | return the name for |
#search | Search this fragment for |
#serialize | Alias for XML::DocumentFragment#to_s. |
#to_html | Convert this |
#to_s | Convert this |
#to_xhtml | Convert this |
#to_xml | Convert this |
#namespace_declarations | fix for issue 770. |
::Nokogiri::XML::Node
- Inherited
#<< | Add |
#<=> | |
#== |
|
#[] | Fetch an attribute from this node. |
#[]= | Update the attribute |
#accept | Accept a visitor. |
#add_child | Add |
#add_class | Ensure HTML |
#add_namespace | Alias for XML::Node#add_namespace_definition. |
#add_namespace_definition | :category: Manipulating Document Structure. |
#add_next_sibling | Insert |
#add_previous_sibling | Insert |
#after | Insert |
#ancestors | |
#append_class | Add HTML |
#attr | Alias for XML::Node#[]. |
#attribute | :category: Working With |
#attribute_nodes | :category: Working With |
#attribute_with_ns | :category: Working With |
#attributes | Fetch this node’s attributes. |
#before | Insert |
#canonicalize, | |
#child | :category: Traversing Document Structure. |
#classes | Fetch CSS class names of a |
#clone | Clone this node. |
#create_external_subset | Create an external subset. |
#create_internal_subset | Create the internal subset of a document. |
#css_path | Get the path to this node as a |
#deconstruct_keys | Returns a hash describing the |
#decorate! | Decorate this node with the decorators set up in this node’s |
#delete | Alias for XML::Node#remove_attribute. |
#description | Fetch the |
#do_xinclude | Do xinclude substitution on the subtree below node. |
#dup | Duplicate this node. |
#each | Iterate over each attribute name and value pair for this |
#element_children | [Returns]. |
#elements | Alias for XML::Node#element_children. |
#encode_special_chars | Encode any special characters in |
#external_subset | Get the external subset. |
#first_element_child |
|
#fragment | Create a |
#get_attribute | Alias for XML::Node#[]. |
#has_attribute? | Alias for XML::Node#key?. |
#initialize | Create a new node with |
#internal_subset | Get the internal subset. |
#key? | Returns true if |
#keys | Get the attribute names for this |
#kwattr_add | Ensure that values are present in a keyword attribute. |
#kwattr_append | Add keywords to a Node’s keyword attribute, regardless of duplication. |
#kwattr_remove | Remove keywords from a keyword attribute. |
#kwattr_values | Fetch values from a keyword attribute of a |
#last_element_child |
|
#matches? | Returns true if this |
#namespace_definitions | [Returns]. |
#namespace_scopes |
|
#namespaced_key? | Returns true if |
#namespaces | Fetch all the namespaces on this node and its ancestors. |
#next_element | Returns the next |
#next_sibling | Returns the next sibling node. |
#node_type | Get the type for this |
#parse | Parse |
#path | Returns the path associated with this |
#pointer_id | [Returns]. |
#prepend_child | Add |
#previous_element | Returns the previous |
#previous_sibling | Returns the previous sibling node. |
#remove | Alias for XML::Node#unlink. |
#remove_attribute | Remove the attribute named |
#remove_class | Remove HTML |
#replace | Replace this |
#serialize | Serialize Node using |
#set_attribute | Alias for XML::Node#[]=. |
#swap | Swap this |
#to_html | Serialize this |
#to_s | Turn this node in to a string. |
#to_xhtml | Serialize this |
#to_xml | Serialize this |
#traverse | Yields self and all children to |
#type | Alias for XML::Node#node_type. |
#unlink | Unlink this node from its current context. |
#value? | Does this Node’s attributes include <value>. |
#values | Get the attribute values for this |
#wrap | Wrap this |
#write_html_to | Write Node as |
#write_to | Serialize this node or document to |
#write_xhtml_to | Write Node as XHTML to |
#write_xml_to | Write Node as |
#add_child_node_and_reparent_attrs, #add_sibling, | |
#compare | |
#dump_html | Returns the |
#get | Get the value for |
#html_standard_serialize, | |
#in_context | TODO: DOCUMENT ME. |
#inspect_attributes, #keywordify, | |
#native_write_to | Write this |
#process_xincludes | Loads and substitutes all xinclude elements below the node. |
#set | Set the |
#set_namespace | Set the namespace to |
#to_format, #write_format_to, #add_child_node, #add_next_sibling_node, #add_previous_sibling_node, #replace_node |
::Nokogiri::ClassResolver
- Included
#related_class | Find a class constant within the. |
::Nokogiri::XML::Searchable
- Included
#% | Alias for XML::Searchable#at. |
#/ | Alias for XML::Searchable#search. |
#> | Search this node’s immediate children using |
#at | Search this object for |
#at_css | Search this object for |
#at_xpath | Search this node for XPath |
#css | Search this object for |
#search | Search this object for |
#xpath | Search this node for XPath |
#css_internal, #css_rules_to_xpath, #xpath_impl, #xpath_internal, #xpath_query_from_css_rule, #extract_params |
::Nokogiri::XML::PP::Node
- Included
Constructor Details
.new(document, input, **options) → HTML5::DocumentFragment) ⇒ DocumentFragment
Parse HTML5 fragment input from a String, and return a new DocumentFragment
.
💡 It’s recommended to use either .parse or 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::Nokogiri::HTML5
element, in which to parse the document fragment. (default“body”
) -
max_errors:
(Integer) The maximum number of parse errors to record. (default Gumbo::DEFAULT_MAX_ERRORS which is currently 0) -
max_tree_depth:
(Integer) The maximum depth of the parse tree. (default Gumbo::DEFAULT_MAX_TREE_DEPTH) -
max_attributes:
(Integer) The maximum number of attributes allowed on an element. (default Gumbo::DEFAULT_MAX_ATTRIBUTES) -
parse_noscript_content_as_text:
(Boolean) Whether to parse the content ofnoscript
elements as text. (defaultfalse
)
See HTML5@Parsing+options for a complete description of these parsing options.
- Returns
-
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.
# 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
Class Method Details
.parse(input, **options) → HTML5::DocumentFragment)
Parse HTML5 fragment input from a String, and return a new DocumentFragment
. This method creates a new, empty 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::Nokogiri::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. (default Gumbo::DEFAULT_MAX_ERRORS which is currently 0) -
max_tree_depth:
(Integer) The maximum depth of the parse tree. (default Gumbo::DEFAULT_MAX_TREE_DEPTH) -
max_attributes:
(Integer) The maximum number of attributes allowed on an element. (default Gumbo::DEFAULT_MAX_ATTRIBUTES) -
parse_noscript_content_as_text:
(Boolean) Whether to parse the content ofnoscript
elements as text. (defaultfalse
)
See HTML5@Parsing+options for a complete description of these parsing options.
- Returns
-
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.
# 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 Attribute Details
#document (rw)
[ GitHub ]# File 'lib/nokogiri/html5/document_fragment.rb', line 88
attr_accessor :document
#errors (rw)
[ GitHub ]# File 'lib/nokogiri/html5/document_fragment.rb', line 89
attr_accessor :errors
#quirks_mode (readonly)
Get the parser’s quirks mode value. See QuirksMode
.
This method returns nil
if the parser was not invoked (e.g., ‘Nokogiri::HTML5::DocumentFragment.new(doc)`).
Since v1.14.0
# File 'lib/nokogiri/html5/document_fragment.rb', line 97
attr_reader :quirks_mode
Instance Method Details
#extract_params(params)
# 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