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

Fix for escaped leftover css rules #18

Merged
merged 1 commit into from
Apr 20, 2023
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
20 changes: 20 additions & 0 deletions premailer/data/leftover_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>

<head>
<style>
div {
background-color: red;
padding: 10px;
}

.a:hover {
background-color: green;
Copy link
Owner

@vanng822 vanng822 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add one more property here. Look like it may be a bug when we make string of all properties?
I unfortunately doesn't have a proper computer with Golang installed :-(

Copy link
Contributor Author

@nikolagava nikolagava Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean when we add another unmergable class or two parameters in the same class?
Something like this works? Although, class b remains on div element.

.a:hover {
    background-color: green;
    border-radius: 8px;
}

.b {
    border: 2px solid blue;
}

Resulting in something like this

<html>

<head>
    <style type="text/css">
        .a:hover {
            background-color: green !important;
            border-radius: 8px !important
        }
    </style>
</head>

<body>
    <div class="a b" style="background-color:red;padding:10px;border:2px solid blue">Hover me!</div>
</body>

</html>

Note that both a and b classes are still applied, even though b shouldn't be.
Edit and lack of ; on line border-radius: 8px !important

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, it would still be ok if the last one is missing ;

}
</style>
</head>

<body>
<div class="a">Hover me!</div>
</body>

</html>
10 changes: 10 additions & 0 deletions premailer/premailer_from_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func TestBasicHTMLFromFile(t *testing.T) {
assert.Contains(t, resultHTML, "<div style=\"background-color:green\">Green color</div>")
}

func TestLeftoverCssRules(t *testing.T) {
p, err := NewPremailerFromFile("data/leftover_test.html", nil)
assert.Nil(t, err)
resultHTML, err := p.Transform()
assert.Nil(t, err)

assert.Contains(t, resultHTML, "<div class=\"a\" style=\"background-color:red;padding:10px\">Hover me!</div>")
assert.Contains(t, resultHTML, "<style type=\"text/css\">.a:hover {\nbackground-color: green !important\n}</style>")
}

func TestFromFileNotFound(t *testing.T) {
p, err := NewPremailerFromFile("data/blablabla.html", nil)
assert.NotNil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion premailer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func copyRule(selector string, rule *css.CSSRule) *css.CSSRule {
for _, s := range rule.Style.Styles {
styles = append(styles, css.NewCSSStyleDeclaration(s.Property, s.Value.Text(), s.Important))
}
copiedStyle := css.CSSStyleRule{Selector: css.NewCSSValueString(selector), Styles: styles}
copiedStyle := css.CSSStyleRule{Selector: css.NewCSSValue(selector), Styles: styles}
copiedRule := &css.CSSRule{Type: rule.Type, Style: copiedStyle}
return copiedRule
}
Expand Down