Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: document official support for the pattern matching API #2967

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

---

## next / unreleased

### Notable Changes

#### Pattern matching

This version marks _official support_ for the pattern matching API in `XML::Attr`, `XML::Document`, `XML::DocumentFragment`, `XML::Namespace`, `XML::Node`, and `XML::NodeSet` (and their subclasses), originally introduced as an experimental feature in v1.14.0.

Documentation on what can be matched:

* [`XML::Attr#deconstruct_keys`](https://nokogiri.org/rdoc/Nokogiri/XML/Attr.html?h=deconstruct#method-i-deconstruct_keys)
* [`XML::Document#deconstruct_keys`](https://nokogiri.org/rdoc/Nokogiri/XML/Document.html?h=deconstruct#method-i-deconstruct_keys)
* [`XML::Namespace#deconstruct_keys`](https://nokogiri.org/rdoc/Nokogiri/XML/Namespace.html?h=deconstruct+namespace#method-i-deconstruct_keys)
* [`XML::Node#deconstruct_keys`](https://nokogiri.org/rdoc/Nokogiri/XML/Node.html?h=deconstruct#method-i-deconstruct_keys)
* [`XML::DocumentFragment#deconstruct`](https://nokogiri.org/rdoc/Nokogiri/XML/DocumentFragment.html?h=deconstruct#method-i-deconstruct)
* [`XML::NodeSet#deconstruct`](https://nokogiri.org/rdoc/Nokogiri/XML/NodeSet.html?h=deconstruct#method-i-deconstruct)



## 1.15.4 / 2023-08-11

### Dependencies
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/xml/attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Attr < Node
# - +value+ → (String) The value of the attribute.
# - +namespace+ → (Namespace, nil) The Namespace of the attribute, or +nil+ if there is no namespace.
#
# ⚡ This is an experimental feature, available since v1.14.0
#
# *Example*
#
# doc = Nokogiri::XML.parse(<<~XML)
Expand Down Expand Up @@ -52,6 +50,8 @@ class Attr < Node
# # href = "http://nokogiri.org/ns/noko"
# # })}
#
# Since v1.14.0
#
def deconstruct_keys(keys)
{ name: name, value: value, namespace: namespace }
end
Expand Down
7 changes: 3 additions & 4 deletions lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def empty_doc?(string_or_io)
# Since v1.12.4
attr_accessor :namespace_inheritance

# :nodoc:
def initialize(*args) # rubocop:disable Lint/MissingSuper
def initialize(*args) # :nodoc: # rubocop:disable Lint/MissingSuper
@errors = []
@decorators = nil
@namespace_inheritance = false
Expand Down Expand Up @@ -427,8 +426,6 @@ def xpath_doctype
# instructions. If you have a use case and would like this functionality, please let us know
# by opening an issue or a discussion on the github project.
#
# ⚡ This is an experimental feature, available since v1.14.0
#
# *Example*
#
# doc = Nokogiri::XML.parse(<<~XML)
Expand All @@ -455,6 +452,8 @@ def xpath_doctype
# doc.deconstruct_keys([:root])
# # => {:root=>nil}
#
# Since v1.14.0
#
def deconstruct_keys(keys)
{ root: root }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/xml/document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ def fragment(data)
# root elements, you should deconstruct the array returned by
# <tt>DocumentFragment#elements</tt>.
#
# ⚡ This is an experimental feature, available since v1.14.0
#
# *Example*
#
# frag = Nokogiri::HTML5.fragment(<<~HTML)
Expand Down Expand Up @@ -187,6 +185,8 @@ def fragment(data)
# # }),
# # #(Element:0x398 { name = "div", children = [ #(Text "End")] })]
#
# Since v1.14.0
#
def deconstruct
children.to_a
end
Expand Down
3 changes: 1 addition & 2 deletions lib/nokogiri/xml/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Namespace
# - +prefix+ → (String, nil) The namespace's prefix, or +nil+ if there is no prefix (e.g., default namespace).
# - +href+ → (String) The namespace's URI
#
# ⚡ This is an experimental feature, available since v1.14.0
#
# *Example*
#
# doc = Nokogiri::XML.parse(<<~XML)
Expand All @@ -43,6 +41,7 @@ class Namespace
# doc.root.elements.last.namespace.deconstruct_keys([:prefix, :href])
# # => {:prefix=>"noko", :href=>"http://nokogiri.org/ns/noko"}
#
# Since v1.14.0
#
def deconstruct_keys(keys)
{ prefix: prefix, href: href }
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,6 @@ def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comm
# - +content+ → (String) The contents of all the text nodes in this node's subtree. See #content.
# - +inner_html+ → (String) The inner markup for the children of this node. See #inner_html.
#
# ⚡ This is an experimental feature, available since v1.14.0
#
# *Example*
#
# doc = Nokogiri::XML.parse(<<~XML)
Expand Down Expand Up @@ -1466,6 +1464,8 @@ def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comm
# # value = "def"
# # })]}
#
# Since v1.14.0
#
def deconstruct_keys(keys)
requested_keys = DECONSTRUCT_KEYS & keys
{}.tap do |values|
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/xml/node_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def inspect
#
# Returns the members of this NodeSet as an array, to use in pattern matching.
#
# ⚡ This is an experimental feature, available since v1.14.0
# Since v1.14.0
#
def deconstruct
to_a
Expand Down