Guess schemes / relative links for images

pull/194/merge
M66B 3 years ago
parent 5d034b7b90
commit 91a78116e8

@ -1301,26 +1301,23 @@ public class HtmlHelper {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
Elements b = document.select("base");
String base = (b.size() > 0 ? b.get(0).attr("href") : null);
for (Element a : document.select("a")) {
String href = a.attr("href");
if (href.contains(" ")) {
href = href.replace(" ", "%20");
a.attr("href", href);
}
for (Element e : document.select("a,img")) {
String attr = ("a".equals(e.tagName()) ? "href" : "src");
String link = e.attr(attr);
if (!TextUtils.isEmpty(base))
try {
// https://developer.android.com/reference/java/net/URI
href = URI.create(base).resolve(href).toString();
a.attr("href", href);
link = URI.create(base).resolve(link.replace(" ", "%20")).toString();
e.attr(attr, link);
} catch (Throwable ex) {
Log.w(ex);
}
if (href.trim().startsWith("#")) {
a.tagName("span");
a.removeAttr("href");
if ("a".equals(e.tagName()) &&
link.trim().startsWith("#")) {
e.tagName("span");
e.removeAttr(attr);
}
}
}
@ -1422,13 +1419,14 @@ public class HtmlHelper {
}
static void guessSchemes(Document document) {
for (Element a : document.select("a"))
for (Element e : document.select("a,img"))
try {
String href = a.attr("href");
if (TextUtils.isEmpty(href))
String attr = ("a".equals(e.tagName()) ? "href" : "src");
String url = e.attr(attr);
if (TextUtils.isEmpty(url))
continue;
Uri uri = UriHelper.guessScheme(Uri.parse(href));
a.attr("href", uri.toString());
Uri uri = UriHelper.guessScheme(Uri.parse(url));
e.attr(attr, uri.toString());
} catch (Throwable ex) {
Log.e(ex);
}

Loading…
Cancel
Save