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

[WIP] mj-include implementation #6

Merged
merged 20 commits into from
Dec 25, 2024
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
5 changes: 5 additions & 0 deletions data/circular/circular-1.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<mj-section css-class="bg-white">
<mj-column padding="32px 48px">
<mj-include path="circular-2.mjml" />
</mj-column>
</mj-section>
5 changes: 5 additions & 0 deletions data/circular/circular-2.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<mj-section css-class="bg-white">
<mj-column padding="32px 48px">
<mj-include path="circular-1.mjml" />
</mj-column>
</mj-section>
14 changes: 14 additions & 0 deletions data/circular/include-circular.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<mjml>
<mj-body>

<mj-section css-class="bg-white">
<mj-column>
<mj-text padding="48px 48px 0 48px">
<p>This is the Index!</p>
</mj-text>
</mj-column>
</mj-section>

<mj-include path="/circular/circular-1.mjml" />
</mj-body>
</mjml>
8 changes: 8 additions & 0 deletions data/import-head.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<mjml>
<mj-head>
<mj-include path="include/import-head-styles.mjml" />
</mj-head>
<mj-body>
<mj-text>test</mj-text>
</mj-body>
</mjml>
16 changes: 16 additions & 0 deletions data/include-about.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<mjml>
<mj-body>
<mj-include path="include/styling.mjml" />
<mj-include path="include/header.mjml" />

<mj-section css-class="bg-white">
<mj-column>
<mj-text padding="48px 48px 0 48px">
<p>This is the About!</p>
</mj-text>
</mj-column>
</mj-section>

<mj-include path="include/footer.mjml" />
</mj-body>
</mjml>
17 changes: 17 additions & 0 deletions data/include-index.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<mjml>
<mj-body>
<mj-include path="include/styling.mjml" />
<mj-include path="include/header.mjml" />

<mj-section css-class="bg-white">
<mj-column>
<mj-text padding="48px 48px 0 48px">
<p>This is the Index!</p>
</mj-text>
</mj-column>
</mj-section>

<mj-include path="include/footer.mjml" />
<mj-include path="not-found.mjml" />
</mj-body>
</mjml>
15 changes: 15 additions & 0 deletions data/include-type-css.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-include type="css" path="include/type.css" />
</mj-column>
<mj-column>
<mj-include type="css" path="include/type2.css" />
</mj-column>
<mj-column>
<mj-include type="css" path="include/non-exist.css" />
</mj-column>
</mj-section>
</mj-body>
</mjml>
12 changes: 12 additions & 0 deletions data/include-type-html.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-include type="html" path="include/type.html" />
</mj-column>
<mj-column>
<mj-include type="html" path="include/non-exist.html" />
</mj-column>
</mj-section>
</mj-body>
</mjml>
22 changes: 22 additions & 0 deletions data/include/footer.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<mj-include path="styling.mjml" />

<mj-section css-class="bg-white">
<mj-column padding="32px 48px">
<mj-text>
<p>Cheers,</p>
<p>Me!</p>
</mj-text>
</mj-column>
</mj-section>
<mj-section css-class="bg-white">
<mj-column>
<mj-divider />
</mj-column>
</mj-section>
<mj-section css-class="bg-white">
<mj-column>
<mj-text color="#2F5854" align="center" padding="8px 0">
<p>Something.com ©️ 2023</p>
</mj-text>
</mj-column>
</mj-section>
7 changes: 7 additions & 0 deletions data/include/header.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<mj-include path="styling.mjml" />

<mj-section css-class="bg-dark" padding="16px">
<mj-column>
<mj-spacer height="32px" />
</mj-column>
</mj-section>
1 change: 1 addition & 0 deletions data/include/import-head-styles.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mj-attributes><mj-text color='#ffffff' /></mj-attributes>
24 changes: 24 additions & 0 deletions data/include/styling.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<mjml>
<mj-head>
<mj-font name="Open Sans" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" />
<mj-style>
.bg-dark {
background: #2F5854 !important;
}
.bg-white {
background: #FFFFFF !important;
}
p {
font-size: 16px !important;
font-weight: 400 !important;
line-height: 24px !important;
margin: 0;
}
</mj-style>
<mj-attributes>
<mj-body background-color="#EAEEEE" width="636px" />
<mj-divider border-width="1px" border-style="solid" border-color="#2F5854" padding="0 16px" />
<mj-all font-family="Open Sans" font-size="16px" font-weight="400" color="#1E3432" padding="0" align="left" />
</mj-attributes>
</mj-head>
</mjml>
7 changes: 7 additions & 0 deletions data/include/type.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h1 {
color: red;
}

.my-custom-class {
border: 1px solid red;
}
10 changes: 10 additions & 0 deletions data/include/type.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div>hello world!</div>
<hr>
<table>
<tbody><tr><td>a</td><td>b</td></tr></tbody>
</table>

<ul>
<li>a
<li>b
</ul>
7 changes: 7 additions & 0 deletions data/include/type2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h2 {
color: green;
}

.my-custom-class-2 {
border: 1px solid green;
}
14 changes: 14 additions & 0 deletions data/mj-divider-mj-all.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<mjml>
<mj-head>
<mj-attributes>
<mj-all font-size="16px" font-weight="400" color="#1E3432" padding="0" align="left" />
</mj-attributes>
</mj-head>
<mj-body>
<mj-section>
<mj-column>
<mj-divider />
</mj-column>
</mj-section>
</mj-body>
</mjml>
12 changes: 3 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>23.1.2</version>
<version>24.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js-community</artifactId>
<version>23.1.2</version>
<version>24.1.0</version>
<type>pom</type>
<scope>test</scope>
</dependency>
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/ch/digitalfondue/mjml4j/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
abstract class BaseComponent {

private final Element element;
private final BaseComponent parent;
private final List<BaseComponent> children = new ArrayList<>();
private BaseComponent parent;

final GlobalContext context;

Expand All @@ -31,6 +31,10 @@ final void doSetupPostConstruction() {
}
}

public void setParent(BaseComponent parent) {
this.parent = parent;
}

void setupPostConstruction() {
}

Expand All @@ -39,9 +43,10 @@ LinkedHashMap<String, AttributeValueType> allowedAttributes() {
}

private LinkedHashMap<String, String> defaultAttributeValues() {
var kvt = allowedAttributes();
var res = new LinkedHashMap<String, String>();
kvt.forEach((k, vt) -> res.put(k, vt.value()));
for (var kvt : allowedAttributes().entrySet()) {
res.put(kvt.getKey(), kvt.getValue().value());
}
return res;
}

Expand All @@ -65,8 +70,9 @@ final void setupComponentAttributes() {

void setAttributes() {
var attributesLength = element.getAttributes().getLength();
var elemAttr = element.getAttributes();
for (int i = 0; i < attributesLength; i++) {
var attr = element.getAttributes().item(i);
var attr = elemAttr.item(i);
var userAttributeName = attr.getNodeName().toLowerCase(Locale.ROOT);
var userAttributeValue = attr.getNodeValue();
if (!attributes.containsKey(userAttributeName)) {
Expand Down Expand Up @@ -252,14 +258,14 @@ CssBoxModel getBoxModel() {
parent.getContainerInnerWidth(),
borders,
paddings,
containerWidth.value
containerWidth.value()
);
}
return new CssBoxModel(
containerWidth.value,
containerWidth.value(),
borders,
paddings,
containerWidth.value);
containerWidth.value());
}


Expand All @@ -270,7 +276,7 @@ CssBoxModel getBoxModel() {
var mjAttribute = getAttribute("border");

if (!Utils.isNullOrWhiteSpace(mjAttributeDirection))
return CssUnitParser.parse(mjAttributeDirection).value;
return CssUnitParser.parse(mjAttributeDirection).value();

if (Utils.isNullOrWhiteSpace(mjAttribute))
return 0;
Expand Down Expand Up @@ -300,7 +306,7 @@ CssBoxModel getBoxModel() {
var mjAttribute = getAttribute(attribute);

if (!Utils.isNullOrWhiteSpace(mjAttributeDirection))
return CssUnitParser.parse(mjAttributeDirection).value;
return CssUnitParser.parse(mjAttributeDirection).value();

if (Utils.isNullOrWhiteSpace(mjAttribute))
return 0;
Expand Down Expand Up @@ -337,10 +343,10 @@ CssBoxModel getBoxModel() {
break;
case 1:
default:
return CssUnitParser.parse(mjAttribute).value;
return CssUnitParser.parse(mjAttribute).value();
}

return CssUnitParser.parse(splittedCssValue[directions.get(direction)]).value;
return CssUnitParser.parse(splittedCssValue[directions.get(direction)]).value();
}

StringBuilder htmlAttributes(LinkedHashMap<String, String> htmlAttributes) {
Expand Down
19 changes: 6 additions & 13 deletions src/main/java/ch/digitalfondue/mjml4j/CssUnitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@
class CssUnitParser {


static class CssParsedUnit {
final String unit;
final double value;

final double valueFullPrecision;

CssParsedUnit(String unit, double value, double valueFullPrecision) {
this.unit = unit;
this.value = value;
this.valueFullPrecision = valueFullPrecision;
}
record CssParsedUnit(String unit, double value, double valueFullPrecision) {

boolean isPercent() {
return "%".equals(unit);
}

boolean isPx() { return "px".equals(unit); }
boolean isPx() {
return "px".equals(unit);
}

@Override
public String toString() {
Expand All @@ -48,8 +40,9 @@ static CssParsedUnit parse(String cssValue) {

var match = UNIT_PATTERN.matcher(cssValue);

if (!match.find())
if (!match.find()) {
throw new IllegalStateException("CssWidthParser could not parse " + cssValue + " due to invalid format");
}

var widthValue = match.group(1);
var widthUnit = match.groupCount() != 2 ? "px" : match.group(2);
Expand Down
Loading
Loading