Merge pull request #50473 from seanpdoyle/action-text-content-pattern-matching

Delegate `ActionText::Content#deconstruct` to Nokogiri
This commit is contained in:
Rafael Mendonça França 2024-01-03 12:31:22 -05:00 committed by GitHub
commit 664eb0dfc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 0 deletions

@ -1,3 +1,20 @@
* Delegate `ActionText::Content#deconstruct` to `Nokogiri::XML::DocumentFragment#elements`
```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" } }
```
*Sean Doyle*
* Fix all Action Text database related models to respect
`ActiveRecord::Base.table_name_prefix` configuration.

@ -24,6 +24,7 @@ class Content
attr_reader :fragment
delegate :deconstruct, to: :fragment
delegate :blank?, :empty?, :html_safe, :present?, to: :to_html # Delegating to to_html to avoid including the layout
class << self

@ -21,6 +21,8 @@ def from_html(html)
attr_reader :source
delegate :deconstruct, to: "source.elements"
def initialize(source)
@source = source
end

@ -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"
require_relative "./content_test/pattern_matching_test_cases"
end

@ -0,0 +1,19 @@
# frozen_string_literal: true
class ActionText::PatternMatchingTestCases < ActiveSupport::TestCase
test "delegates pattern matching to Nokogiri" do
content = ActionText::Content.new <<~HTML
<h1 id="hello-world">Hello, world</h1>
<div>The body</div>
HTML
# rubocop:disable Lint/Syntax
content => [h1, div]
assert_pattern { h1 => { name: "h1", content: "Hello, world", attributes: [{ name: "id", value: "hello-world" }] } }
refute_pattern { h1 => { name: "h1", content: "Goodbye, world" } }
assert_pattern { div => { content: "The body" } }
# rubocop:enable Lint/Syntax
end
end

@ -9,6 +9,7 @@ class ActionView::PatternMatchingTestCases < ActionView::TestCase
# rubocop:disable Lint/Syntax
assert_pattern { document_root_element.at("h1") => { content: "Eloy", attributes: [{ name: "id", value: "name" }] } }
refute_pattern { document_root_element.at("h1") => { content: "Not Eloy" } }
# rubocop:enable Lint/Syntax
end
test "rendered.html integrates with pattern matching" do