diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 6ed24164b8..6dadc17167 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -83,6 +83,7 @@ import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.jsoup.safety.Cleaner; import org.jsoup.safety.Whitelist; +import org.jsoup.select.Elements; import org.jsoup.select.NodeFilter; import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeVisitor; @@ -408,41 +409,7 @@ public class HtmlHelper { // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style List sheets = new ArrayList<>(); if (parse_classes) - for (Element style : parsed.head().select("style")) { - if (BuildConfig.DEBUG) - Log.i("Style=" + style.data()); - try { - InputSource source = new InputSource(new StringReader(style.data())); - String media = style.attr("media"); - if (!TextUtils.isEmpty(media)) - source.setMedia(media); - - CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); - parser.setErrorHandler(new ErrorHandler() { - @Override - public void warning(CSSParseException ex) throws CSSException { - Log.i("CSS warning=" + ex.getMessage()); - } - - @Override - public void error(CSSParseException ex) throws CSSException { - Log.i("CSS error=" + ex.getMessage()); - } - - @Override - public void fatalError(CSSParseException ex) throws CSSException { - Log.w(ex); - } - }); - - long start = new Date().getTime(); - sheets.add(parser.parseStyleSheet(source, null, null)); - long elapsed = new Date().getTime() - start; - Log.i("Style parse=" + elapsed + " ms"); - } catch (Throwable ex) { - Log.w(ex); - } - } + sheets = parseStyles(parsed.head().select("style")); Whitelist whitelist = Whitelist.relaxed() .addTags("hr", "abbr", "big", "font", "dfn", "del", "s", "tt") @@ -1020,6 +987,46 @@ public class HtmlHelper { return document; } + static List parseStyles(Elements styles) { + List sheets = new ArrayList<>(); + for (Element style : styles) { + if (BuildConfig.DEBUG) + Log.i("Style=" + style.data()); + try { + InputSource source = new InputSource(new StringReader(style.data())); + String media = style.attr("media"); + if (!TextUtils.isEmpty(media)) + source.setMedia(media); + + CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); + parser.setErrorHandler(new ErrorHandler() { + @Override + public void warning(CSSParseException ex) throws CSSException { + Log.i("CSS warning=" + ex.getMessage()); + } + + @Override + public void error(CSSParseException ex) throws CSSException { + Log.i("CSS error=" + ex.getMessage()); + } + + @Override + public void fatalError(CSSParseException ex) throws CSSException { + Log.w(ex); + } + }); + + long start = new Date().getTime(); + sheets.add(parser.parseStyleSheet(source, null, null)); + long elapsed = new Date().getTime() - start; + Log.i("Style parse=" + elapsed + " ms"); + } catch (Throwable ex) { + Log.w(ex); + } + } + return sheets; + } + private static String processStyles(String tag, String clazz, String style, List sheets) { for (CSSStyleSheet sheet : sheets) if (isScreenMedia(sheet.getMedia())) {