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

Add HTML source view tab #526

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions assets/javascripts/mailcatcher.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,31 @@ class MailCatcher
when "html"
body = $("#message iframe").contents().find("body")
$("a", body).attr("target", "_blank")
when "html-source"
message_iframe = $("#message iframe").contents()
text = message_iframe.text()

text = @escape_html_reserved_characters text

message_iframe.find("html").html("<body><pre>#{text}</pre></body>")
when "plain"
message_iframe = $("#message iframe").contents()
text = message_iframe.text()

# Escape special characters
text = text.replace(/&/g, "&amp;")
text = text.replace(/</g, "&lt;")
text = text.replace(/>/g, "&gt;")
text = text.replace(/"/g, "&quot;")
text = @escape_html_reserved_characters text

# Autolink text
text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, """<a href="$1" target="_blank">$1</a>""")

message_iframe.find("html").html("""<body style="font-family: sans-serif; white-space: pre-wrap">#{text}</body>""")

escape_html_reserved_characters: (text) ->
text
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\"", "&quot;")

refresh: ->
$.getJSON "messages", (messages) =>
$.each messages, (i, message) =>
Expand Down
11 changes: 11 additions & 0 deletions lib/mail_catcher/web/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def asset_path(filename)
"formats" => [
"source",
("html" if Mail.message_has_html? id),
("html-source" if Mail.message_has_html? id),
("plain" if Mail.message_has_plain? id)
].compact,
"attachments" => Mail.message_attachments(id),
Expand All @@ -141,6 +142,16 @@ def asset_path(filename)
end
end

get "/messages/:id.html-source" do
id = params[:id].to_i
if part = Mail.message_part_html(id)
content_type "text/plain", :charset => (part["charset"] || "utf8")
part["body"]
else
not_found
end
end

get "/messages/:id.plain" do
id = params[:id].to_i
if part = Mail.message_part_plain(id)
Expand Down
26 changes: 26 additions & 0 deletions spec/delivery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def html_tab_element
page.find("#message header .format.html a")
end

def html_source_tab_element
page.find("#message header .format.html-source a")
end

def plain_tab_element
page.find("#message header .format.plain a")
end
Expand Down Expand Up @@ -123,6 +127,28 @@ def body_element
end
end

it "catches and displays an html message as html source" do
deliver_example("htmlmail")

# Do not reload, make sure that the message appears via websockets

expect(page).to have_selector("#messages table tbody tr:first-of-type", text: "Test HTML Mail")

message_row_element.click

expect(html_tab_element).to be_visible
expect(html_source_tab_element).to be_visible

html_source_tab_element.click

# Load the HTML mail but discard the headers
html_source = read_example("htmlmail").split("\n\n", 2).last.chomp

within_frame do
expect(page.text).to eq(html_source)
end
end

it "catches and displays a multipart message as text, html and source" do
deliver_example("multipartmail")

Expand Down
1 change: 1 addition & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<nav class="views">
<ul>
<li class="format tab html selected" data-message-format="html"><a href="#">HTML</a></li>
<li class="format tab html-source" data-message-format="html-source"><a href="#">HTML Source</a></li>
<li class="format tab plain" data-message-format="plain"><a href="#">Plain Text</a></li>
<li class="format tab source" data-message-format="source"><a href="#">Source</a></li>
<li class="action download" data-message-format="html"><a href="#" class="button"><span>Download</span></a></li>
Expand Down