Skip to content

Commit

Permalink
improved html -> text will replace <br> and <p> by new line in text v…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
melistik committed Jul 17, 2018
1 parent 1a5b27e commit cc63995
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
8 changes: 1 addition & 7 deletions src/main/java/io/rocketbase/commons/email/FooterLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.Getter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

@Getter
Expand Down Expand Up @@ -36,12 +35,7 @@ public class FooterLine implements TemplateLine {
});
this.text = doc.body().html();

links.forEach(e -> {
if (!e.attr("href").equals("")) {
e.appendChild(new TextNode(" -> " + e.attr("href")));
}
});
this.escapedText = doc.wholeText();
this.escapedText = extractHtml(doc);
}

public FooterLine alignment(Alignment alignment) {
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/io/rocketbase/commons/email/TextLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.Getter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

@Getter
Expand Down Expand Up @@ -36,12 +35,7 @@ public class TextLine implements TemplateLine {
});
this.text = doc.body().html();

links.forEach(e -> {
if (!e.attr("href").equals("")) {
e.appendChild(new TextNode(" -> " + e.attr("href")));
}
});
this.escapedText = doc.wholeText();
this.escapedText = extractHtml(doc);
}

public TextLine alignment(Alignment alignment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.rocketbase.commons.email.EmailTemplateBuilder.EmailTemplateConfigBuilder;
import io.rocketbase.commons.email.model.HtmlTextEmail;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

public interface TemplateLine {

Expand All @@ -16,5 +19,31 @@ enum TemplateLineType {
TEXT, BUTTON, IMAGE, FOOTER;
}

/**
* will replace <br>
* links to => text -> url<br>
* &lt;br&gt; => newline<br>
* &lt;p&gt;..&lt;/p&gt; => ...newline<br>
*
* @param doc that could get modified
* @return correctly escaped txt version of doc
*/
default String extractHtml(Document doc) {
Elements links = doc.body().getElementsByTag("a");
links.forEach(e -> {
if (!e.attr("href").equals("")) {
e.appendChild(new TextNode(" -> " + e.attr("href")));
}
});
Elements brs = doc.body().getElementsByTag("br");
brs.forEach(e -> {
e.replaceWith(new TextNode("\n"));
});
Elements ps = doc.body().getElementsByTag("p");
ps.forEach(e -> {
e.replaceWith(new TextNode(String.format("%s\n", e.wholeText())));
});
return doc.wholeText().replaceAll("\n$", "");
}

}
21 changes: 16 additions & 5 deletions src/test/java/io/rocketbase/commons/email/TextLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@
public class TextLineTest {

@Test
public void getTextWithHtml() {
public void injectStylingAndExtractBr() {
// given
String input = "sample text<br><a href=\"http://sample.io\" class=\"btn-button\"> sample button<i class=\"icon-click\"></i></a>";
String input = "sample text<br><a href=\"http://sample.io\" class=\"btn-button\">sample button<i class=\"icon-click\"></i></a>";
// when
TextLine textLine = new TextLine(null, input);
// then
assertThat(textLine.asHtml, equalTo(true));
assertThat(textLine.text.replaceAll(System.getProperty("line.separator"), ""), equalTo("sample text<br><a href=\"http://sample.io\" class=\"btn-button\" style=\"font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #348eda; text-decoration: underline; margin: 0;\"> sample button<i class=\"icon-click\"></i></a>"));
assertThat(textLine.escapedText, equalTo("sample text sample button -> http://sample.io"));
assertThat(textLine.text.replaceAll(System.getProperty("line.separator"), ""), equalTo("sample text<br><a href=\"http://sample.io\" class=\"btn-button\" style=\"font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #348eda; text-decoration: underline; margin: 0;\">sample button<i class=\"icon-click\"></i></a>"));
assertThat(textLine.escapedText, equalTo("sample text\nsample button -> http://sample.io"));
}

@Test
public void extratPWithNewLine() {
// given
String input = "<p>sample text</p><p>Another Line</p>";
// when
TextLine textLine = new TextLine(null, input);
// then
assertThat(textLine.asHtml, equalTo(true));
assertThat(textLine.text.replaceAll("\n", ""), equalTo(input));
assertThat(textLine.escapedText, equalTo("sample text\nAnother Line"));
}

@Test
public void getTextWithText() {
public void simpleText() {
// given
String input = "sample text";
// when
Expand Down

0 comments on commit cc63995

Please sign in to comment.