FairEmail/app/src/main/java/eu/faircode/email/HtmlHelper.java

86 lines
2.9 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
2018-08-02 13:33:06 +00:00
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
2018-08-28 12:52:33 +00:00
import org.jsoup.safety.Whitelist;
2018-08-02 13:33:06 +00:00
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2018-08-02 13:33:06 +00:00
public class HtmlHelper {
2018-09-02 11:18:32 +00:00
private static Pattern pattern = Pattern.compile("([http|https]+://[\\w\\S(\\.|:|/)]+)");
2018-08-02 13:33:06 +00:00
2018-12-09 14:49:43 +00:00
static String sanitize(String html) {
2018-12-14 09:05:48 +00:00
Document document = Jsoup.parse(Jsoup.clean(html, Whitelist
.relaxed()
.addProtocols("img", "src", "cid")
.addProtocols("img", "src", "data")));
2018-11-24 11:27:44 +00:00
for (Element tr : document.select("tr"))
tr.after("<br>");
2018-11-24 11:27:44 +00:00
2018-11-24 11:42:21 +00:00
for (Element img : document.select("img")) {
boolean linked = false;
for (Element parent : img.parents())
if ("a".equals(parent.tagName())) {
linked = true;
break;
}
if (!linked) {
2018-11-24 11:27:44 +00:00
String src = img.attr("src");
if (src.startsWith("http://") || src.startsWith("https://")) {
Element a = document.createElement("a");
a.attr("href", src);
img.replaceWith(a);
a.appendChild(img);
}
}
2018-11-24 11:42:21 +00:00
}
2018-11-24 11:27:44 +00:00
NodeTraversor.traverse(new NodeVisitor() {
@Override
public void head(Node node, int depth) {
if (node instanceof TextNode) {
String text = ((TextNode) node).text();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String ref = matcher.group();
text = text.replace(ref, String.format("<a href=\"%s\">%s</a>", ref, ref));
2018-09-02 11:18:32 +00:00
}
node.before(text);
((TextNode) node).text("");
2018-09-02 11:18:32 +00:00
}
}
2018-09-02 11:18:32 +00:00
@Override
public void tail(Node node, int depth) {
}
}, document.body());
return document.body().html();
2018-08-02 13:33:06 +00:00
}
}