Refactoring

pull/184/head
M66B 4 years ago
parent cfe3d65c31
commit 346b4487a5

@ -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<CSSStyleSheet> 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<CSSStyleSheet> parseStyles(Elements styles) {
List<CSSStyleSheet> 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<CSSStyleSheet> sheets) {
for (CSSStyleSheet sheet : sheets)
if (isScreenMedia(sheet.getMedia())) {

Loading…
Cancel
Save