Class: EmailTemplate::ButtonPaddingNormalizer

Inherits:
Object
  • Object
show all
Defined in:
app/services/email_template/button_padding_normalizer.rb

Overview

Post-processes a compiled email body_v4_email so CTA buttons keep real,
INLINE horizontal padding on every mail client — not just Outlook.

Redactor's getEmail() (the wyemailblocks plugin's _buildButton) compiles a
.email-button into a full-width button whose horizontal padding lives ONLY
in mso-padding-alt (Outlook): the <a> is forced to width:100% with its
left/right padding zeroed. Mobile webmail (Gmail app, etc.) ignore mso-* and
strip <style>/@media unreliably, so they render the button with no
horizontal padding — the "no padding / left-aligned on mobile" defect.

We can't fix this from a stylesheet (clients don't fetch ours, and many drop
<style>), and patching the vendored Redactor plugin would be wiped on the
next plugin update. So we mirror the Outlook padding into a real inline
padding on the anchor here, server-side, where it survives both the plugin
and the client. Runs from EmailTemplate's before_save, a sibling of
ensure_v4_email_backgrounds, on the same regex-rewrite principle (no full
Nokogiri re-serialization — the compiled email markup is left byte-for-byte
intact except the button anchors).

Examples:

EmailTemplate::ButtonPaddingNormalizer.call(body_v4_email) # => html

Constant Summary collapse

BUTTON_CELL =

A compiled WY button cell:

/
  (<td\b[^>]*?\bmso-padding-alt:\s*)
  ([^;"]+?)
  ([;"][^>]*>\s*<a\b[^>]*?\bclass="[^"]*\bemail-button\b[^"]*"[^>]*?\bstyle=")
  ([^"]*)
  (")
/imx

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ ButtonPaddingNormalizer

Returns a new instance of ButtonPaddingNormalizer.



44
45
46
# File 'app/services/email_template/button_padding_normalizer.rb', line 44

def initialize(html)
  @html = html.to_s
end

Class Method Details

.call(html) ⇒ String

Returns the html with every button anchor given real inline padding.

Parameters:

  • html (String, nil)

    a compiled body_v4_email

Returns:

  • (String)

    the html with every button anchor given real inline padding



40
41
42
# File 'app/services/email_template/button_padding_normalizer.rb', line 40

def self.call(html)
  new(html).call
end

Instance Method Details

#callObject



48
49
50
51
52
53
54
55
# File 'app/services/email_template/button_padding_normalizer.rb', line 48

def call
  return @html unless @html.include?('email-button')

  @html.gsub(BUTTON_CELL) do
    td_head, padding, td_tail_and_anchor_head, anchor_style, quote = Regexp.last_match.captures
    "#{td_head}#{padding}#{td_tail_and_anchor_head}#{rewrite_anchor(anchor_style, padding.strip)}#{quote}"
  end
end