Skip to content

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Dec 28, 2023

Motivation / Background

Since ActionText::Content wraps an ActionText::Fragment, and ActionText::Fragment wraps a Nokogiri::XML::DocumentFragment, then ActionText::Content should be able to rely on the newer Ruby pattern matching introduced by nokogiri@1.16.0 (mainly the DocumentFragment#deconstruct method):

action_text_content = ActionText::Content.new <<~HTML
  <h1>Hello, world</h1>

  <div>The body</div>
HTML

action_text_content => [h1, div]

assert_pattern { h1 => { content: "Hello, world" } }
assert_pattern { div => { content: "The body" } }

Detail

The implementation change relies on delegating from Content to Fragment, and from Fragment to DocumentFragment#elements (to deliberately exclude text nodes).

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Since `ActionText::Content` wraps an `ActionText::Fragment`, and
`ActionText::Fragment` wraps a `Nokogiri::XML::DocumentFragment`, then
`ActionText::Content` should be able to rely on the newer Ruby pattern
matching introduced by [nokogiri@1.16.0][] (mainly the
[DocumentFragment#deconstruct][] method):

```ruby
content = ActionText::Content.new <<~HTML
  <h1>Hello, world</h1>

  <div>The body</div>
HTML

content => [h1, div]

assert_pattern { h1 => { content: "Hello, world" } }
assert_pattern { div => { content: "The body" } }
```

The implementation change relies on delegating from `Content` to
`Fragment`, and from `Fragment` to `DocumentFragment#elements` (to
deliberately exclude text nodes).

[nokogiri@1.16.0]: https://nokogiri.org/CHANGELOG.html?h=pattern
[DocumentFragment#deconstruct]: https://nokogiri.org/rdoc/Nokogiri/XML/DocumentFragment.html?h=deconstruct#method-i-deconstruct
@@ -21,6 +21,8 @@ def from_html(html)

attr_reader :source

delegate :deconstruct, to: "source.elements"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative to making design decisions around the #deconstruct method, this change could instead forward all unknown methods from Content straight to DocumentFragment, through a chain of delegate_missing_to definitions.

That would mean that callers could be able to invoke content.elements directly. That would drastically broaden the public API of the class, but would remove the burden of making pattern matching interface considerations.

@@ -210,3 +210,7 @@ def with_attachment_tag_name(tag_name)
ActionText::Attachment.tag_name = previous_tag_name
end
end

if RUBY_VERSION >= "3.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed now

@rafaelfranca rafaelfranca merged commit 664eb0d into rails:main Jan 3, 2024
@seanpdoyle seanpdoyle deleted the action-text-content-pattern-matching branch January 3, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants