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

InlineCssPostProcessor overwrites default tag styles #203

Open
TwentyFourMinutes opened this issue Sep 24, 2024 · 10 comments
Open

InlineCssPostProcessor overwrites default tag styles #203

TwentyFourMinutes opened this issue Sep 24, 2024 · 10 comments

Comments

@TwentyFourMinutes
Copy link

Since v4 the new InlineCssPostProcessor causes the default styles defined by mjml-net to be overwritten. I believe its caused on this line:

var currentStyle = element.Owner!.DefaultView.GetStyleCollection().GetDeclarations(element);

From what I can tell this more or less ignores all styles defined on the style= attribute. For example calling element.GetAttribute(TagNames.Style) will correctly return me word-break:break-word. Otherwise with the current implementation the whole InlineCssPostProcessor causes the whole HTML to be more or less broken.

Would be awesome if you could take a look at it :). Also thanks for maintaining this library, it's awesome!

@SebastianStehle
Copy link
Owner

Hi,

what do you mean with "default" styles?

@TwentyFourMinutes
Copy link
Author

Things such as these

.Style("word-break", "break-word");

I came to this conclusion, as texts in my email which are longer than 600px now overflow way outside their container. I then noticed that for example word-break: break-word is no longer present in the html at all when upgrading to v4.

@SebastianStehle
Copy link
Owner

Good point, I think it is easy to fix. Just don#t overwrite styles that already exist I guess (except they are important)

@TwentyFourMinutes
Copy link
Author

@SebastianStehle I already gave it a shot with:

private static void InlineStyle(IElement element, IStyleCollection styleCollection)
{
    var currentStyle = styleCollection.GetDeclarations(element);

    if (!currentStyle.Any())
    {
        return;
    }

    var style = element.GetStyle();

    foreach (var property in currentStyle)
    {
        style.SetProperty(property.Name, property.Value, property.IsImportant ? CssKeywords.Important : null);
    }

However, I still had the issue that element.GetStyle() won't contain the word-break: break-word property 🤔

@SebastianStehle
Copy link
Owner

Weird, perhaps there is another method that helps?

@SebastianStehle
Copy link
Owner

I am not sure if I understand the bug tbh. I have this test:

<mjml-test>
  <mj-head>
    <mj-style inline=""inline"">
      .red-text div {
        color: red !important;
        font-size: 1.5rem;
        font-weight: light;
      }
    </mj-style>
  </mj-head>
  <mj-body>
    <mj-raw>
      <div class=""red-text"">
        <div style=""font-weight: bold""></div>
      </div>
    </mj-raw>
  </mj-body>
</mjml-test>

Here font-weight is not overriden. Because GetDeclarations actually calculates the styles in the correct logic. How would that override styles from mjml?

@TwentyFourMinutes
Copy link
Author

TwentyFourMinutes commented Sep 24, 2024

@SebastianStehle look at this modified variant of the the test

    [Fact]
    public async Task Should_render_inline()
    {
        var source = @"
<mjml-test>
  <mj-head>
    <mj-style inline=""inline"">
      .red-text div {
        color: red !important;
        font-size: 1.5rem;
      }
    </mj-style>
  </mj-head>
  <mj-body>
    <mj-raw>
      <div class=""red-text"">
        <div style=""font-weight: bold; word-break: break-word;""></div>
      </div>
    </mj-raw>
  </mj-body>
</mjml-test>
";

        var (result, _) = await TestHelper.RenderAsync(source, new MjmlOptions
        {
            PostProcessors =
            [
                AngleSharpPostProcessor.Default
            ],
            Beautify = true
        }, helpers: [new StyleHelper()]);

        AssertHelpers.HtmlFileAssert("Components.Outputs.StyleInline.html", result);
    }

I just added the word-break: break-word; style, which I guess simulates what this does:

.Style("word-break", "break-word");

And the test is still green! I can also verify that word-break: break-word; is no longer present in the result.Html`.

I guess it has something to do with the css property itself then?

@SebastianStehle
Copy link
Owner

For me it looks like it just ignores most CSS properties.

@SebastianStehle
Copy link
Owner

I have tried to debug it in AngleSharp and created a bug report there. Lets see what happens

@TwentyFourMinutes
Copy link
Author

@SebastianStehle Thank you very much. :)

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

No branches or pull requests

2 participants