Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email

62 lines
2.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import org.jsoup.Jsoup;
  17. import org.jsoup.nodes.Document;
  18. import org.jsoup.nodes.Element;
  19. import org.jsoup.nodes.Node;
  20. import org.jsoup.nodes.TextNode;
  21. import org.jsoup.safety.Whitelist;
  22. import org.jsoup.select.NodeTraversor;
  23. import org.jsoup.select.NodeVisitor;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. public class HtmlHelper {
  27. private static Pattern pattern = Pattern.compile("([http|https]+://[\\w\\S(\\.|:|/)]+)");
  28. public static String sanitize(String html) {
  29. Document document = Jsoup.parse(Jsoup.clean(html, Whitelist.relaxed().addProtocols("img", "src", "cid")));
  30. for (Element tr : document.select("tr"))
  31. tr.after("<br>");
  32. NodeTraversor.traverse(new NodeVisitor() {
  33. @Override
  34. public void head(Node node, int depth) {
  35. if (node instanceof TextNode) {
  36. String text = ((TextNode) node).text();
  37. Matcher matcher = pattern.matcher(text);
  38. while (matcher.find()) {
  39. String ref = matcher.group();
  40. text = text.replace(ref, String.format("<a href=\"%s\">%s</a>", ref, ref));
  41. }
  42. node.before(text);
  43. ((TextNode) node).text("");
  44. }
  45. }
  46. @Override
  47. public void tail(Node node, int depth) {
  48. }
  49. }, document.body());
  50. return document.body().html();
  51. }
  52. }