Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOID] fixes #3477: apoc.load.html does not always report href #3478

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions full/src/main/java/apoc/load/LoadHtml.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import apoc.util.MissingDependencyException;
import apoc.util.FileUtils;
import java.nio.charset.UnsupportedCharsetException;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -74,7 +76,7 @@ private Stream<MapResult> readHtmlPage(String url, Map<String, String> query, Ma
}

return Stream.of(new MapResult(output));
} catch ( UnsupportedCharsetException e) {
} catch (UnsupportedCharsetException e) {
throw new RuntimeException("Unsupported charset: " + config.getCharset());
} catch (IllegalArgumentException | ClassCastException e) {
throw new RuntimeException("Invalid config: " + config);
Expand Down Expand Up @@ -137,7 +139,16 @@ private static Map<String, String> getAttributes(Element element) {
final String key = attribute.getKey();
// with href/src attribute we prepend baseUri path
final boolean attributeHasLink = key.equals("href") || key.equals("src");
attributes.put(key, attributeHasLink ? element.absUrl(key) : attribute.getValue());
String attr = null;
if (attributeHasLink) {
attr = element.absUrl(key);
if (StringUtils.isBlank(attr)) {
attr = attribute.getValue();
}
} else {
attr = attribute.getValue();
}
attributes.put(key, attr);
}
}

Expand Down
14 changes: 14 additions & 0 deletions full/src/test/java/apoc/load/LoadHtmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import apoc.ApocSettings;
import apoc.util.TestUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -353,6 +354,19 @@ public void testQueryWithFailsSilentlyWithLog() {
});
}

@Test
public void testHref() {
Map<String, Object> query = Map.of("a", "a.image");

testResult(db, "CALL apoc.load.html($url, $query) YIELD value UNWIND value.a AS row RETURN row",
map("url", new File("src/test/resources/wikipedia.html").toURI().toString(), "query", query),
result -> {
Map<String, Object> row = (Map<String, Object>) result.next().get("row");
Map<String, Object> attributes = (Map<String, Object>) row.get("attributes");
Assert.assertEquals("/wiki/File:Aap_Kaa_Hak_titles.jpg", attributes.get("href"));
});
}

@Test
public void testQueryWithFailsSilentlyWithList() {
Map<String, Object> query = map("a", "a", "invalid", "invalid", "h6", "h6");
Expand Down