Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "Fix newsletter html containing style tag content" to 0.23-stable #6963

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions decidim-core/lib/decidim/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require "omniauth-google-oauth2"
require "invisible_captcha"
require "premailer/rails"
require "premailer/adapter/decidim"
require "geocoder"
require "paper_trail"
require "cells/rails"
Expand Down Expand Up @@ -507,6 +508,10 @@ class Engine < ::Rails::Engine
NOBSPW.configuration.use_ruby_grep = true
end

initializer "decidim.premailer" do
Premailer::Adapter.use = :decidim
end

config.to_prepare do
FoundationRailsHelper::FlashHelper.send(:include, Decidim::FlashHelperExtensions)
end
Expand Down
33 changes: 33 additions & 0 deletions decidim-core/lib/premailer/adapter/decidim.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

class Premailer
module Adapter
# Decidim adapter for Premailer
module Decidim
include ::Premailer::Adapter::Nokogiri

# Converts the HTML document to a format suitable for plain-text e-mail.
#
# If present, uses the <body> element as its base; otherwise uses the whole document.
#
# Customized for Decidim in order to strip the inline <style> tags away
# from the plain text body.
#
# @return [String] a plain text.
def to_plain_text
html_src = begin
@doc.at("body").inner_html
rescue StandardError
""
end

html_src = @doc.to_html unless html_src && html_src.present?

# remove style tags and content
html_src.gsub!(%r{<style.*?/style>}m, "")

convert_to_text(html_src, @options[:line_length], @html_encoding)
end
end
end
end
61 changes: 61 additions & 0 deletions decidim-core/spec/lib/premailer/adapter/decidim_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "spec_helper"

describe Premailer::Adapter::Decidim do
let(:document) { ::Nokogiri::HTML(document_template, nil, "UTF-8", &:recover) }
let(:document_template) do
<<~HTML
<html>
<head>
<title>Test document</title>
</head>
<body>
<div class="container">
#{document_content}
</div>
</body>
</html>
HTML
end
let(:document_content) { "" }
let(:utility_class) do
adapter = described_class

Class.new do
include HtmlToPlainText
include adapter

def initialize(doc)
@doc = doc
@options = { line_length: 65 }
end
end
end
let(:utility) { utility_class.new(document) }

describe "#to_plain_text" do
let(:document_content) do
<<~HTML
<style>
table.button table td {
background: #f0f0f0 !important
}
</style>
<p>This is a document with an inline style tag inside the content node.</p>
HTML
end

it "strips out the style tags from the document" do
expect(utility.to_plain_text).to eq("This is a document with an inline style tag inside the content\nnode.")
end

context "when the document is not wrapped within HTML body" do
let(:document_template) { document_content }

it "strips out the style tags from the document" do
expect(utility.to_plain_text).to eq("This is a document with an inline style tag inside the content\nnode.")
end
end
end
end