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 optional custom markupformatter parameter #198

Merged
merged 2 commits into from
Apr 8, 2020

Conversation

CaptainStack
Copy link

All I've done in this change is add an optional parameter for a custom IMarkupFormatter that can be supplied to PreMailer.Net for special circumstances where the provided MarkupFormatters are not working for the client's needs.

The issue was caused by the default behavior used in AngleSharp, which provides an interface for custom formatters in case that behavior does not work. This change simply exposes that interface as an option in PreMailer.Net and passes it onto AngleSharp.

I implemented this specifically to address the problem I was having in this issue:
#193

I also collaborated with @FlorianRappl - the primary author of AngleSharp to reach this implementation. Please let me know if you have any questions or concerns about this feature :)

@martinnormark martinnormark merged commit 1f5eab1 into milkshakesoftware:master Apr 8, 2020
@martinnormark
Copy link
Contributor

Thanks for this PR 🙏

Release is cut, and will hit nuget fairly soon :shipit:

@jonedmiston
Copy link

@CaptainStack do you have example code on how to use the override to prevent encoding '&' in the querystring (#193)?

@DarthSonic
Copy link

@CaptainStack do you have example code on how to use the override to prevent encoding '&' in the querystring (#193)?

@DarthSonic
Copy link

Unfortunately my custom formatter is called but & is still escaped to &amp:

var customFormatter = new NoEscapeFormatter();
using var pm = new PreMailer.Net.PreMailer(mail.body, new Uri(layoutUri));
mail.body = pm.MoveCssInline(removeComments: true, removeStyleElements: true, customFormatter: customFormatter).ToString();
    public class NoEscapeFormatter : IMarkupFormatter
    {
        public string CloseTag(IElement element, bool selfClosing) => HtmlMarkupFormatter.Instance.CloseTag(element, selfClosing);
        public string Comment(IComment comment) => HtmlMarkupFormatter.Instance.Comment(comment);
        public string Doctype(IDocumentType doctype) => HtmlMarkupFormatter.Instance.Doctype(doctype);
        public string LiteralText(ICharacterData text) => HtmlMarkupFormatter.Instance.LiteralText(text);
        public string OpenTag(IElement element, bool selfClosing) => HtmlMarkupFormatter.Instance.OpenTag(element, selfClosing);
        public string Processing(IProcessingInstruction processing) => HtmlMarkupFormatter.Instance.Processing(processing);
        public string Text(ICharacterData text) => text.Data;
    }

@FlorianRappl
Copy link

Hm not sure what you expect - from the code example you just use the standard formatter for pretty much everything.

What you need / want to do is to override the OpenTag to avoid having the attributes written with the escaping. Note that the escaping is part of the HTML spec and your links should not break by having & in them. The browser parses the entities in there like a text - so & will be &.

Try this in your favorite browser:

<a href="http://example.com/?foo=bar&amp;qxz=123">Example.</a>

you will see that the link is applied correctly. Attributes are always HTML escaped.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants