2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-10 15:33:37 -04:00
|
|
|
#
|
|
|
|
# HTML emails don't support CSS, so we can use nokogiri to inline attributes based on
|
|
|
|
# matchers.
|
|
|
|
#
|
|
|
|
module Email
|
|
|
|
class Styles
|
2014-08-21 06:54:05 -04:00
|
|
|
@@plugin_callbacks = []
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2016-05-21 14:13:00 -04:00
|
|
|
attr_accessor :fragment
|
|
|
|
|
|
|
|
delegate :css, to: :fragment
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(html, opts = nil)
|
2013-06-10 15:33:37 -04:00
|
|
|
@html = html
|
2015-10-22 13:10:07 -04:00
|
|
|
@opts = opts || {}
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment = Nokogiri::HTML.fragment(@html)
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
|
|
|
|
2014-08-21 06:54:05 -04:00
|
|
|
def self.register_plugin_style(&block)
|
|
|
|
@@plugin_callbacks.push(block)
|
|
|
|
end
|
|
|
|
|
2014-05-09 14:39:09 -04:00
|
|
|
def add_styles(node, new_styles)
|
|
|
|
existing = node['style']
|
|
|
|
if existing.present?
|
2014-11-07 16:42:57 -05:00
|
|
|
# merge styles
|
|
|
|
node['style'] = "#{new_styles}; #{existing}"
|
2014-05-09 14:39:09 -04:00
|
|
|
else
|
|
|
|
node['style'] = new_styles
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
def format_basic
|
2014-06-13 17:11:04 -04:00
|
|
|
uri = URI(Discourse.base_url)
|
|
|
|
|
2014-10-27 14:21:55 -04:00
|
|
|
# images
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('img').each do |img|
|
2013-11-28 17:20:56 -05:00
|
|
|
next if img['class'] == 'site-logo'
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
if (img['class'] && img['class']['emoji']) || (img['src'] && img['src'][/\/_?emoji\//])
|
|
|
|
img['width'] = img['height'] = 20
|
2013-06-13 12:15:05 -04:00
|
|
|
else
|
2014-11-14 20:33:42 -05:00
|
|
|
# use dimensions of original iPhone screen for 'too big, let device rescale'
|
2017-07-27 21:20:09 -04:00
|
|
|
if img['width'].to_i > (320) || img['height'].to_i > (480)
|
2017-01-30 12:06:48 -05:00
|
|
|
img['width'] = img['height'] = 'auto'
|
2014-11-14 19:23:52 -05:00
|
|
|
end
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
if img['src']
|
|
|
|
# ensure all urls are absolute
|
|
|
|
img['src'] = "#{Discourse.base_url}#{img['src']}" if img['src'][/^\/[^\/]/]
|
|
|
|
# ensure no schemaless urls
|
|
|
|
img['src'] = "#{uri.scheme}:#{img['src']}" if img['src'][/^\/\//]
|
2013-08-26 18:08:38 -04:00
|
|
|
end
|
2013-07-22 15:06:37 -04:00
|
|
|
end
|
2014-10-27 14:21:55 -04:00
|
|
|
|
2015-11-04 06:38:39 -05:00
|
|
|
# add max-width to big images
|
2016-01-29 05:13:59 -05:00
|
|
|
big_images = @fragment.css('img[width="auto"][height="auto"]') -
|
|
|
|
@fragment.css('aside.onebox img') -
|
|
|
|
@fragment.css('img.site-logo, img.emoji')
|
2015-11-04 06:38:39 -05:00
|
|
|
big_images.each do |img|
|
2016-01-29 05:13:59 -05:00
|
|
|
add_styles(img, 'max-width: 100%;') if img['style'] !~ /max-width/
|
2015-11-04 06:38:39 -05:00
|
|
|
end
|
|
|
|
|
2016-12-05 07:31:43 -05:00
|
|
|
# topic featured link
|
|
|
|
@fragment.css('a.topic-featured-link').each do |e|
|
|
|
|
e['style'] = "color:#858585;padding:2px 8px;border:1px solid #e6e6e6;border-radius:2px;box-shadow:0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);"
|
|
|
|
end
|
|
|
|
|
2014-10-27 14:21:55 -04:00
|
|
|
# attachments
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('a.attachment').each do |a|
|
2014-10-27 14:21:55 -04:00
|
|
|
# ensure all urls are absolute
|
|
|
|
if a['href'] =~ /^\/[^\/]/
|
|
|
|
a['href'] = "#{Discourse.base_url}#{a['href']}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# ensure no schemaless urls
|
|
|
|
if a['href'] && a['href'].starts_with?("//")
|
|
|
|
a['href'] = "#{uri.scheme}:#{a['href']}"
|
|
|
|
end
|
|
|
|
end
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2013-07-22 15:06:37 -04:00
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def format_notification
|
2016-07-12 19:26:44 -04:00
|
|
|
style('.previous-discussion', 'font-size: 17px; color: #444; margin-bottom:10px;')
|
2014-05-14 16:40:54 -04:00
|
|
|
style('.notification-date', "text-align:right;color:#999999;padding-right:5px;font-family:'lucida grande',tahoma,verdana,arial,sans-serif;font-size:11px")
|
2018-07-16 12:30:37 -04:00
|
|
|
style('.username', "font-size:13px;font-family:'lucida grande',tahoma,verdana,arial,sans-serif;color:#{SiteSetting.email_link_color};text-decoration:none;font-weight:bold")
|
2015-03-24 11:25:47 -04:00
|
|
|
style('.user-title', "font-size:13px;font-family:'lucida grande',tahoma,verdana,arial,sans-serif;text-decoration:none;margin-left:7px;color: #999;")
|
2018-07-16 12:30:37 -04:00
|
|
|
style('.user-name', "font-size:13px;font-family:'lucida grande',tahoma,verdana,arial,sans-serif;text-decoration:none;margin-left:7px;color: #{SiteSetting.email_link_color};font-weight:normal;")
|
2014-10-14 19:56:43 -04:00
|
|
|
style('.post-wrapper', "margin-bottom:25px;")
|
2013-07-26 03:27:46 -04:00
|
|
|
style('.user-avatar', 'vertical-align:top;width:55px;')
|
|
|
|
style('.user-avatar img', nil, width: '45', height: '45')
|
|
|
|
style('hr', 'background-color: #ddd; height: 1px; border: 1px;')
|
2014-08-27 07:38:03 -04:00
|
|
|
style('.rtl', 'direction: rtl;')
|
2017-05-15 16:24:33 -04:00
|
|
|
style('div.body', 'padding-top:5px;')
|
|
|
|
style('.whisper div.body', 'font-style: italic; color: #9c9c9c;')
|
2016-05-21 09:17:54 -04:00
|
|
|
style('.lightbox-wrapper .meta', 'display: none')
|
2013-07-26 18:08:58 -04:00
|
|
|
correct_first_body_margin
|
2013-07-26 03:27:46 -04:00
|
|
|
correct_footer_style
|
2018-05-03 18:50:06 -04:00
|
|
|
style('div.undecorated-link-footer a', "font-weight: normal;")
|
2018-06-11 18:54:39 -04:00
|
|
|
correct_footer_style_hilight_first
|
2013-07-26 03:27:46 -04:00
|
|
|
reset_tables
|
2014-05-09 14:39:09 -04:00
|
|
|
onebox_styles
|
2014-08-21 06:54:05 -04:00
|
|
|
plugin_styles
|
2014-05-09 14:39:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def onebox_styles
|
|
|
|
# Links to other topics
|
2017-01-02 03:03:03 -05:00
|
|
|
style('aside.quote', 'padding: 12px 25px 2px 12px; margin-bottom: 10px;')
|
2014-05-09 14:39:09 -04:00
|
|
|
style('aside.quote div.info-line', 'color: #666; margin: 10px 0')
|
2017-01-02 16:49:00 -05:00
|
|
|
style('aside.quote .avatar', 'margin-right: 5px; width:20px; height:20px; vertical-align:middle;')
|
2014-05-09 14:39:09 -04:00
|
|
|
|
2015-10-22 16:08:52 -04:00
|
|
|
style('blockquote', 'border-left: 5px solid #e9e9e9; background-color: #f8f8f8; margin: 0;')
|
|
|
|
style('blockquote > p', 'padding: 1em;')
|
|
|
|
|
2014-05-09 14:39:09 -04:00
|
|
|
# Oneboxes
|
2016-12-05 06:00:04 -05:00
|
|
|
style('aside.onebox', "border: 5px solid #e9e9e9; padding: 12px 25px 12px 12px;")
|
2017-12-05 03:38:30 -05:00
|
|
|
style('aside.onebox header img.site-icon', "width: 16px; height: 16px; margin-right: 3px;")
|
2016-12-05 06:00:04 -05:00
|
|
|
style('aside.onebox header a[href]', "color: #222222; text-decoration: none;")
|
|
|
|
style('aside.onebox .onebox-body', "clear: both")
|
|
|
|
style('aside.onebox .onebox-body img', "max-height: 80%; max-width: 20%; height: auto; float: left; margin-right: 10px;")
|
2019-03-05 12:31:50 -05:00
|
|
|
style('aside.onebox .onebox-body img.thumbnail', "width: 60px;")
|
2016-12-05 06:00:04 -05:00
|
|
|
style('aside.onebox .onebox-body h3, aside.onebox .onebox-body h4', "font-size: 1.17em; margin: 10px 0;")
|
|
|
|
style('.onebox-metadata', "color: #919191")
|
2014-05-13 14:44:40 -04:00
|
|
|
|
2017-01-02 03:03:03 -05:00
|
|
|
@fragment.css('aside.quote blockquote > p').each do |p|
|
|
|
|
p['style'] = 'padding: 0;'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert all `aside.quote` tags to `blockquote`s
|
|
|
|
@fragment.css('aside.quote').each do |n|
|
|
|
|
original_node = n.dup
|
|
|
|
original_node.search('div.quote-controls').remove
|
|
|
|
blockquote = original_node.css('blockquote').inner_html.strip.start_with?("<p") ? original_node.css('blockquote').inner_html : "<p style='padding: 0;'>#{original_node.css('blockquote').inner_html}</p>"
|
|
|
|
n.inner_html = original_node.css('div.title').inner_html + blockquote
|
|
|
|
n.name = "blockquote"
|
|
|
|
end
|
|
|
|
|
2014-05-13 14:44:40 -04:00
|
|
|
# Finally, convert all `aside` tags to `div`s
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('aside, article, header').each do |n|
|
2014-05-13 14:44:40 -04:00
|
|
|
n.name = "div"
|
|
|
|
end
|
2014-07-14 16:41:05 -04:00
|
|
|
|
|
|
|
# iframes can't go in emails, so replace them with clickable links
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('iframe').each do |i|
|
2014-07-14 16:41:05 -04:00
|
|
|
begin
|
2016-10-21 06:37:03 -04:00
|
|
|
# sometimes, iframes are blacklisted...
|
|
|
|
if i["src"].blank?
|
|
|
|
i.remove
|
|
|
|
next
|
|
|
|
end
|
2014-07-14 16:41:05 -04:00
|
|
|
|
2019-04-24 04:20:27 -04:00
|
|
|
src_uri = i["data-original-href"].present? ? URI(i["data-original-href"]) : URI(i['src'])
|
2014-07-14 16:41:05 -04:00
|
|
|
# If an iframe is protocol relative, use SSL when displaying it
|
2016-07-04 05:29:12 -04:00
|
|
|
display_src = "#{src_uri.scheme || 'https'}://#{src_uri.host}#{src_uri.path}#{src_uri.query.nil? ? '' : '?' + src_uri.query}#{src_uri.fragment.nil? ? '' : '#' + src_uri.fragment}"
|
|
|
|
i.replace "<p><a href='#{src_uri.to_s}'>#{CGI.escapeHTML(display_src)}</a><p>"
|
2018-08-14 06:23:32 -04:00
|
|
|
rescue URI::Error
|
2016-10-21 06:37:03 -04:00
|
|
|
# If the URL is weird, remove the iframe
|
2014-07-14 16:41:05 -04:00
|
|
|
i.remove
|
|
|
|
end
|
|
|
|
end
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_html
|
2016-12-19 11:19:10 -05:00
|
|
|
style('.with-accent-colors', "background-color: #{SiteSetting.email_accent_bg_color}; color: #{SiteSetting.email_accent_fg_color};")
|
2016-02-27 03:07:15 -05:00
|
|
|
style('h4', 'color: #222;')
|
2014-05-13 14:44:40 -04:00
|
|
|
style('h3', 'margin: 15px 0 20px 0;')
|
2013-07-26 03:27:46 -04:00
|
|
|
style('hr', 'background-color: #ddd; height: 1px; border: 1px;')
|
2016-12-19 11:19:10 -05:00
|
|
|
style('a', "text-decoration: none; font-weight: bold; color: #{SiteSetting.email_link_color};")
|
2013-07-26 03:27:46 -04:00
|
|
|
style('ul', 'margin: 0 0 0 10px; padding: 0 0 0 20px;')
|
|
|
|
style('li', 'padding-bottom: 10px')
|
2014-11-14 03:48:45 -05:00
|
|
|
style('div.footer', 'color:#666; font-size:95%; text-align:center; padding-top:15px;')
|
2013-11-29 13:00:10 -05:00
|
|
|
style('span.post-count', 'margin: 0 5px; color: #777;')
|
2013-12-16 14:41:59 -05:00
|
|
|
style('pre', 'word-wrap: break-word; max-width: 694px;')
|
2013-12-02 10:04:18 -05:00
|
|
|
style('code', 'background-color: #f1f1ff; padding: 2px 5px;')
|
2013-12-16 14:41:59 -05:00
|
|
|
style('pre code', 'display: block; background-color: #f1f1ff; padding: 5px;')
|
2016-12-19 11:19:10 -05:00
|
|
|
style('.featured-topic a', "text-decoration: none; font-weight: bold; color: #{SiteSetting.email_link_color}; line-height:1.5em;")
|
2014-01-22 15:30:30 -05:00
|
|
|
|
2014-05-09 14:39:09 -04:00
|
|
|
onebox_styles
|
2014-08-21 06:54:05 -04:00
|
|
|
plugin_styles
|
2016-12-19 17:05:49 -05:00
|
|
|
|
|
|
|
style('.post-excerpt img', "max-width: 50%; max-height: 400px;")
|
2014-08-21 06:54:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# this method is reserved for styles specific to plugin
|
|
|
|
def plugin_styles
|
2016-01-29 05:13:59 -05:00
|
|
|
@@plugin_callbacks.each { |block| block.call(@fragment, @opts) }
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def to_html
|
|
|
|
strip_classes_and_ids
|
2014-06-13 17:11:04 -04:00
|
|
|
replace_relative_urls
|
2017-05-10 14:01:26 -04:00
|
|
|
@fragment.to_html
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2014-09-13 01:26:31 -04:00
|
|
|
def strip_avatars_and_emojis
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.search('img').each do |img|
|
2017-06-15 10:36:11 -04:00
|
|
|
next unless img['src']
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
if img['src'][/_avatar/]
|
2017-06-15 10:36:11 -04:00
|
|
|
img.parent['style'] = "vertical-align: top;" if img.parent&.name == 'td'
|
2014-09-13 01:26:31 -04:00
|
|
|
img.remove
|
|
|
|
end
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
if img['title'] && img['src'][/\/_?emoji\//]
|
2015-08-18 19:12:08 -04:00
|
|
|
img.add_previous_sibling(img['title'] || "emoji")
|
|
|
|
img.remove
|
|
|
|
end
|
2014-09-13 01:26:31 -04:00
|
|
|
end
|
2015-02-26 06:50:56 -05:00
|
|
|
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.to_s
|
2014-09-13 01:26:31 -04:00
|
|
|
end
|
|
|
|
|
2016-05-21 09:17:54 -04:00
|
|
|
def make_all_links_absolute
|
|
|
|
site_uri = URI(Discourse.base_url)
|
|
|
|
@fragment.css("a").each do |link|
|
|
|
|
begin
|
|
|
|
link["href"] = "#{site_uri}#{link['href']}" unless URI(link["href"].to_s).host.present?
|
2018-08-14 06:23:32 -04:00
|
|
|
rescue URI::Error
|
2016-05-21 09:17:54 -04:00
|
|
|
# leave it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
private
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2014-06-13 17:11:04 -04:00
|
|
|
def replace_relative_urls
|
|
|
|
forum_uri = URI(Discourse.base_url)
|
|
|
|
host = forum_uri.host
|
|
|
|
scheme = forum_uri.scheme
|
|
|
|
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('[href]').each do |element|
|
2014-06-13 17:11:04 -04:00
|
|
|
href = element['href']
|
2017-05-03 00:08:14 -04:00
|
|
|
if href.start_with?("\/\/#{host}")
|
2014-06-13 17:11:04 -04:00
|
|
|
element['href'] = "#{scheme}:#{href}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 18:08:58 -04:00
|
|
|
def correct_first_body_margin
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('.body p').each do |element|
|
2014-06-09 15:28:03 -04:00
|
|
|
element['style'] = "margin-top:0; border: 0;"
|
2013-07-26 18:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def correct_footer_style
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('.footer').each do |element|
|
2013-07-29 02:00:02 -04:00
|
|
|
element['style'] = "color:#666;"
|
2018-06-11 18:54:39 -04:00
|
|
|
element.css('a').each do |inner|
|
|
|
|
inner['style'] = "color:#666;"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def correct_footer_style_hilight_first
|
|
|
|
footernum = 0
|
|
|
|
@fragment.css('.footer.hilight').each do |element|
|
2016-01-08 05:14:58 -05:00
|
|
|
linknum = 0
|
2013-07-26 03:27:46 -04:00
|
|
|
element.css('a').each do |inner|
|
2016-01-08 05:14:58 -05:00
|
|
|
# we want the first footer link to be specially highlighted as IMPORTANT
|
2017-07-27 21:20:09 -04:00
|
|
|
if footernum == (0) && linknum == (0)
|
2018-07-16 12:30:37 -04:00
|
|
|
bg_color = SiteSetting.email_accent_bg_color
|
|
|
|
inner['style'] = "background-color: #{bg_color}; color: #{SiteSetting.email_accent_fg_color}; border-top: 4px solid #{bg_color}; border-right: 6px solid #{bg_color}; border-bottom: 4px solid #{bg_color}; border-left: 6px solid #{bg_color}; display: inline-block; font-weight: bold;"
|
2016-01-08 05:14:58 -05:00
|
|
|
end
|
2018-06-11 18:54:39 -04:00
|
|
|
return
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2018-06-11 18:54:39 -04:00
|
|
|
return
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def strip_classes_and_ids
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css('*').each do |element|
|
2017-05-03 00:08:14 -04:00
|
|
|
element.delete('class'.freeze)
|
|
|
|
element.delete('id'.freeze)
|
2013-06-11 12:27:11 -04:00
|
|
|
end
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
2013-06-11 12:27:11 -04:00
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def reset_tables
|
2014-09-25 01:26:23 -04:00
|
|
|
style('table', nil, cellspacing: '0', cellpadding: '0', border: '0')
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
def style(selector, style, attribs = {})
|
2016-01-29 05:13:59 -05:00
|
|
|
@fragment.css(selector).each do |element|
|
2014-05-09 14:39:09 -04:00
|
|
|
add_styles(element, style) if style
|
2017-07-27 21:20:09 -04:00
|
|
|
attribs.each do |k, v|
|
2016-01-29 05:13:59 -05:00
|
|
|
element[k] = v
|
|
|
|
end
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
|
|
|
end
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|