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

Loading…
Cancel
Save