Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,40 @@
# frozen_string_literal: true
module Nokogiri
module HTML5
###
# Nokogiri HTML5 builder is used for building HTML documents. It is very similar to the
# Nokogiri::XML::Builder. In fact, you should go read the documentation for
# Nokogiri::XML::Builder before reading this documentation.
#
# The construction behavior is identical to HTML4::Builder, but HTML5 documents implement the
# [HTML5 standard's serialization
# algorithm](https://www.w3.org/TR/2008/WD-html5-20080610/serializing.html).
#
# == Synopsis:
#
# Create an HTML5 document with a body that has an onload attribute, and a
# span tag with a class of "bold" that has content of "Hello world".
#
# builder = Nokogiri::HTML5::Builder.new do |doc|
# doc.html {
# doc.body(:onload => 'some_func();') {
# doc.span.bold {
# doc.text "Hello world"
# }
# }
# }
# end
# puts builder.to_html
#
# The HTML5 builder inherits from the XML builder, so make sure to read the
# Nokogiri::XML::Builder documentation.
class Builder < Nokogiri::XML::Builder
###
# Convert the builder to HTML
def to_html
@doc.to_html
end
end
end
end
@@ -0,0 +1,199 @@
# coding: utf-8
# frozen_string_literal: true
#
# Copyright 2013-2021 Sam Ruby, Stephen Checkoway
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../html4/document"
module Nokogiri
module HTML5
# Enum for the HTML5 parser quirks mode values. Values returned by HTML5::Document#quirks_mode
#
# See https://dom.spec.whatwg.org/#concept-document-quirks for more information on HTML5 quirks
# mode.
#
# Since v1.14.0
module QuirksMode
NO_QUIRKS = 0 # The document was parsed in "no-quirks" mode
QUIRKS = 1 # The document was parsed in "quirks" mode
LIMITED_QUIRKS = 2 # The document was parsed in "limited-quirks" mode
end
# Since v1.12.0
#
# 💡 HTML5 functionality is not available when running JRuby.
class Document < Nokogiri::HTML4::Document
# Get the url name for this document, as passed into Document.parse, Document.read_io, or
# Document.read_memory
attr_reader :url
# Get the parser's quirks mode value. See HTML5::QuirksMode.
#
# This method returns +nil+ if the parser was not invoked (e.g., Nokogiri::HTML5::Document.new).
#
# Since v1.14.0
attr_reader :quirks_mode
class << self
# :call-seq:
# parse(input) { |options| ... } → HTML5::Document
# parse(input, url: encoding:) { |options| ... } → HTML5::Document
# parse(input, **options) → HTML5::Document
#
# Parse \HTML input with a parser compliant with the HTML5 spec. This method uses the
# encoding of +input+ if it can be determined, or else falls back to the +encoding:+
# parameter.
#
# [Required Parameters]
# - +input+ (String | IO) the \HTML content to be parsed.
#
# [Optional Parameters]
# - +url:+ (String) the base URI of the document.
#
# [Optional Keyword Arguments]
# - +encoding:+ (Encoding) The 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.
#
# - +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+)
#
# See rdoc-ref:HTML5@Parsing+options for a complete description of these parsing options.
#
# [Yields]
# If present, the block will be passed a Hash object to modify with parse options before the
# input is parsed. See rdoc-ref:HTML5@Parsing+options for a list of available options.
#
# ⚠ Note that +url:+ and +encoding:+ cannot be set by the configuration block.
#
# [Returns] Nokogiri::HTML5::Document
#
# *Example:* Parse a string with a specific encoding and custom max errors limit.
#
# Nokogiri::HTML5::Document.parse(socket, encoding: "ISO-8859-1", max_errors: 10)
#
# *Example:* Parse a string setting the +:parse_noscript_content_as_text+ option using the
# configuration block parameter.
#
# Nokogiri::HTML5::Document.parse(input) { |c| c[:parse_noscript_content_as_text] = true }
#
def parse(
string_or_io,
url_ = nil, encoding_ = nil,
url: url_, encoding: encoding_,
**options, &block
)
yield options if block
string_or_io = "" unless string_or_io
if string_or_io.respond_to?(:encoding) && string_or_io.encoding != Encoding::ASCII_8BIT
encoding ||= string_or_io.encoding.name
end
if string_or_io.respond_to?(:read) && string_or_io.respond_to?(:path)
url ||= string_or_io.path
end
unless string_or_io.respond_to?(:read) || string_or_io.respond_to?(:to_str)
raise ArgumentError, "not a string or IO object"
end
do_parse(string_or_io, url, encoding, **options)
end
# Create a new document from an IO object.
#
# 💡 Most users should prefer Document.parse to this method.
def read_io(io, url_ = nil, encoding_ = nil, url: url_, encoding: encoding_, **options)
raise ArgumentError, "io object doesn't respond to :read" unless io.respond_to?(:read)
do_parse(io, url, encoding, **options)
end
# Create a new document from a String.
#
# 💡 Most users should prefer Document.parse to this method.
def read_memory(string, url_ = nil, encoding_ = nil, url: url_, encoding: encoding_, **options)
raise ArgumentError, "string object doesn't respond to :to_str" unless string.respond_to?(:to_str)
do_parse(string, url, encoding, **options)
end
private
def do_parse(string_or_io, url, encoding, **options)
string = HTML5.read_and_encode(string_or_io, encoding)
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
doc = Nokogiri::Gumbo.parse(string, url, self, **options)
doc.encoding = "UTF-8"
doc
end
end
def initialize(*args) # :nodoc:
super
@url = nil
@quirks_mode = nil
end
# :call-seq:
# fragment() → Nokogiri::HTML5::DocumentFragment
# fragment(markup) → Nokogiri::HTML5::DocumentFragment
#
# Parse a HTML5 document fragment from +markup+, returning a Nokogiri::HTML5::DocumentFragment.
#
# [Properties]
# - +markup+ (String) The HTML5 markup fragment to be parsed
#
# [Returns]
# Nokogiri::HTML5::DocumentFragment. This object's children will be empty if +markup+ is not
# passed, is empty, or is +nil+.
#
def fragment(markup = nil)
DocumentFragment.new(self, markup)
end
def to_xml(options = {}, &block) # :nodoc:
# Bypass XML::Document#to_xml which doesn't add
# XML::Node::SaveOptions::AS_XML like XML::Node#to_xml does.
XML::Node.instance_method(:to_xml).bind_call(self, options, &block)
end
# :call-seq:
# xpath_doctype() → Nokogiri::CSS::XPathVisitor::DoctypeConfig
#
# [Returns] The document type which determines CSS-to-XPath translation.
#
# See CSS::XPathVisitor for more information.
def xpath_doctype
Nokogiri::CSS::XPathVisitor::DoctypeConfig::HTML5
end
end
end
end
@@ -0,0 +1,200 @@
# coding: utf-8
# frozen_string_literal: true
#
# Copyright 2013-2021 Sam Ruby, Stephen Checkoway
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../html4/document_fragment"
module Nokogiri
module HTML5
# Since v1.12.0
#
# 💡 HTML5 functionality is not available when running JRuby.
class DocumentFragment < Nokogiri::HTML4::DocumentFragment
class << self
# :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. (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+)
#
# 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.
#
def parse(
input,
encoding_ = nil, positional_options_hash = nil,
encoding: encoding_, **options
)
unless positional_options_hash.nil? || positional_options_hash.empty?
options.merge!(positional_options_hash)
end
context = options.delete(:context)
document = HTML5::Document.new
document.encoding = "UTF-8"
input = HTML5.read_and_encode(input, encoding)
new(document, input, context, options)
end
end
attr_accessor :document
attr_accessor :errors
# 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
attr_reader :quirks_mode
#
# :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. (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+)
#
# 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.
#
def initialize(
doc, input = nil,
context_ = nil, positional_options_hash = nil,
context: context_,
**options
) # rubocop:disable Lint/MissingSuper
unless positional_options_hash.nil? || positional_options_hash.empty?
options.merge!(positional_options_hash)
end
@document = doc
@errors = []
return self unless input
input = Nokogiri::HTML5.read_and_encode(input, 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, input, context, **options)
end
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
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
end
end
end
# vim: set shiftwidth=2 softtabstop=2 tabstop=8 expandtab:
@@ -0,0 +1,103 @@
# coding: utf-8
# frozen_string_literal: true
#
# Copyright 2013-2021 Sam Ruby, Stephen Checkoway
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# TODO: this whole file should go away. maybe make it a decorator?
#
require_relative "../xml/node"
module Nokogiri
module HTML5
# Since v1.12.0
#
# 💡 HTML5 functionality is not available when running JRuby.
module Node
def inner_html(options = {})
return super unless document.is_a?(HTML5::Document)
result = options[:preserve_newline] && prepend_newline? ? +"\n" : +""
result << children.map { |child| child.to_html(options) }.join
result
end
def write_to(io, *options)
return super unless document.is_a?(HTML5::Document)
options = options.first.is_a?(Hash) ? options.shift : {}
encoding = options[:encoding] || options[0]
if Nokogiri.jruby?
save_options = options[:save_with] || options[1]
indent_times = options[:indent] || 0
else
save_options = options[:save_with] || options[1] || XML::Node::SaveOptions::FORMAT
indent_times = options[:indent] || 2
end
indent_string = (options[:indent_text] || " ") * indent_times
config = XML::Node::SaveOptions.new(save_options.to_i)
yield config if block_given?
encoding = encoding.is_a?(Encoding) ? encoding.name : encoding
config_options = config.options
if config_options & (XML::Node::SaveOptions::AS_XML | XML::Node::SaveOptions::AS_XHTML) != 0
# Use Nokogiri's serializing code.
native_write_to(io, encoding, indent_string, config_options)
else
# Serialize including the current node.
html = html_standard_serialize(options[:preserve_newline] || false)
encoding ||= document.encoding || Encoding::UTF_8
io << html.encode(encoding, fallback: lambda { |c| "&#x#{c.ord.to_s(16)};" })
end
end
def fragment(tags)
return super unless document.is_a?(HTML5::Document)
DocumentFragment.new(document, tags, self)
end
private
# HTML elements can have attributes that contain colons.
# Nokogiri::XML::Node#[]= treats names with colons as a prefixed QName
# and tries to create an attribute in a namespace. This is especially
# annoying with attribute names like xml:lang since libxml2 will
# actually create the xml namespace if it doesn't exist already.
def add_child_node_and_reparent_attrs(node)
return super unless document.is_a?(HTML5::Document)
# I'm not sure what this method is supposed to do. Reparenting
# namespaces is handled by libxml2, including child namespaces which
# this method wouldn't handle.
# https://github.com/sparklemotion/nokogiri/issues/1790
add_child_node(node)
# node.attribute_nodes.find_all { |a| a.namespace }.each do |attr|
# attr.remove
# ns = attr.namespace
# a["#{ns.prefix}:#{attr.name}"] = attr.value
# end
end
end
# Monkey patch
XML::Node.prepend(HTML5::Node)
end
end
# vim: set shiftwidth=2 softtabstop=2 tabstop=8 expandtab: