Skip to content

Commit

Permalink
Changed html5 to non-global option
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 11, 2024
1 parent 1b1a6cc commit b5d0c20
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ AhoyEmail.stats("my-campaign")

By default, this gem uses Nokogiri’s default HTML parser to rewrite `href` attributes for UTM tagging and click analytics. This can cause link tags to be prematurely closed if they wrap `table` elements, because doing so violates the HTML 4 spec.

To use HTML5 parsing, create `config/initializers/ahoy_email.rb` with:
To use HTML5 parsing, create `config/initializers/ahoy_email.rb` with: [unreleased]

```ruby
AhoyEmail.html5 = true
AhoyEmail.default_options[:html5] = true
```

## History
Expand Down
9 changes: 5 additions & 4 deletions lib/ahoy_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
require_relative "ahoy_email/engine" if defined?(Rails)

module AhoyEmail
mattr_accessor :secret_token, :default_options, :subscribers, :invalid_redirect_url, :track_method, :api, :preserve_callbacks, :save_token, :html5
mattr_accessor :secret_token, :default_options, :subscribers, :invalid_redirect_url, :track_method, :api, :preserve_callbacks, :save_token
mattr_writer :message_model

self.api = false
Expand All @@ -48,7 +48,10 @@ module AhoyEmail
click: false,
campaign: nil,
url_options: {},
unsubscribe_links: false
unsubscribe_links: false,

# utm params and click analytics
html5: nil
}

self.track_method = lambda do |data|
Expand Down Expand Up @@ -79,8 +82,6 @@ module AhoyEmail

self.save_token = false

self.html5 = nil

self.subscribers = []

self.preserve_callbacks = []
Expand Down
1 change: 1 addition & 0 deletions lib/ahoy_email/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def call_ahoy_options(options, key)
# only call other options if needed
if options[key]
AhoyEmail::Utils::OPTION_KEYS[key].each do |k|
next if options.key?(k)
v = ahoy_options[k]
options[k] = v.respond_to?(:call) ? instance_exec(&v) : v
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ahoy_email/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def track_links
end

def parser_class
case AhoyEmail.html5
case options[:html5]
when true
Nokogiri::HTML5
when false
Expand Down
4 changes: 2 additions & 2 deletions lib/ahoy_email/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module AhoyEmail
class Utils
OPTION_KEYS = {
message: %i(message mailer user extra),
utm_params: %i(utm_source utm_medium utm_term utm_content utm_campaign),
click: %i(campaign url_options unsubscribe_links)
utm_params: %i(utm_source utm_medium utm_term utm_content utm_campaign html5),
click: %i(campaign url_options unsubscribe_links html5)
}

class << self
Expand Down
6 changes: 0 additions & 6 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ def with_save_token
yield
end
end

def with_html5(html5)
AhoyEmail.stub(:html5, html5) do
yield
end
end
end

class ActionDispatch::IntegrationTest
Expand Down
4 changes: 2 additions & 2 deletions test/utm_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def test_nested
end

def test_nested_table_html5
with_html5(true) do
with_default(html5: true) do
message = UtmParamsMailer.nested_table.deliver_now
assert_body "utm_medium=email", message
assert_body "<table></table></a>", message
end
end

def test_nested_table_html4
with_html5(false) do
with_default(html5: false) do
message = UtmParamsMailer.nested_table.deliver_now
assert_body "utm_medium=email", message
assert_body "</a><table></table>", message
Expand Down

0 comments on commit b5d0c20

Please sign in to comment.