Skip to content

Commit

Permalink
mj-social: handle correctly custom social #7 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer authored Jan 15, 2025
1 parent ad586db commit 286da6f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
13 changes: 13 additions & 0 deletions data/bug-mj-social.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-social font-size="15px" icon-size="30px" mode="horizontal">
<mj-social-element name="mastodon" href="https://mjml.io/" src="https://upload.wikimedia.org/wikipedia/commons/4/48/Mastodon_Logotype_%28Simple%29.svg">
Mastodon
</mj-social-element>
</mj-social>
</mj-column>
</mj-section>
</mj-body>
</mjml>
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,11 @@ private record SocialAttributes(String href, String iconSize, String iconHeight,

private SocialAttributes getSocialAttributes() {
var socialNetworkName = getAttribute("name");


if (!DEFAULT_SOCIAL_NETWORKS.containsKey(socialNetworkName))
return new SocialAttributes(null, null, null, null, null, null, null);

var socialNetwork = DEFAULT_SOCIAL_NETWORKS.get(socialNetworkName);


var href = getAttribute("href");

if (!Utils.isNullOrEmpty(href) && !Utils.isNullOrEmpty(socialNetwork.shareUrl)) {
if (socialNetwork != null && !Utils.isNullOrEmpty(href) && !Utils.isNullOrEmpty(socialNetwork.shareUrl)) {
href = socialNetwork.shareUrl.replace("[[URL]]", href);
}

Expand All @@ -172,8 +166,8 @@ private SocialAttributes getSocialAttributes() {
getAttribute("icon-size"),
getAttribute("srcset"),
getAttribute("sizes"),
hasAttribute("src") ? getAttribute("src") : socialNetwork.src,
hasAttribute("background-color") ? getAttribute("background-color") : socialNetwork.backgroundColor
hasAttribute("src") ? getAttribute("src") : socialNetwork != null ? socialNetwork.src : null,
hasAttribute("background-color") ? getAttribute("background-color") : socialNetwork != null ? socialNetwork.backgroundColor : null
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/ch/digitalfondue/mjml4j/BugTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.digitalfondue.mjml4j;

import org.junit.jupiter.api.Test;

import static ch.digitalfondue.mjml4j.Helpers.testTemplate;

class BugTests {

// see https://github.com/digitalfondue/mjml4j/issues/7
@Test
void checkMjSocialNpe() {
testTemplate("bug-mj-social");
}
}

0 comments on commit 286da6f

Please sign in to comment.