From 97af9c737f7e19041145fea3d0b881c4001bb12b Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Sat, 18 Jan 2025 23:29:03 +0100 Subject: [PATCH] Accept fragments containing characters (),. such as the ones found in JavaDoc --- .../io/quarkiverse/antorassured/LinkValidator.java | 14 +++++++++++++- docs/modules/ROOT/pages/test-page.adoc | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/antorassured/src/main/java/io/quarkiverse/antorassured/LinkValidator.java b/antorassured/src/main/java/io/quarkiverse/antorassured/LinkValidator.java index 6c1ac3f..c54d6ae 100644 --- a/antorassured/src/main/java/io/quarkiverse/antorassured/LinkValidator.java +++ b/antorassured/src/main/java/io/quarkiverse/antorassured/LinkValidator.java @@ -7,6 +7,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.regex.Pattern; import org.jboss.logging.Logger; import org.jsoup.Connection.Method; @@ -41,6 +42,8 @@ public static LinkValidator defaultValidator() { static class LinkValidatorImpl implements LinkValidator { private static final Logger log = Logger.getLogger(AntorAssured.class); + private static final Pattern JAVADOC_FRAGMENT_CHARS = Pattern.compile("[\\(\\)\\,\\.]"); + /** JSoup documents by fragment-less URI */ private final Map documents = new ConcurrentHashMap<>(); /** Locks by fragment-less URI */ @@ -128,6 +131,15 @@ public ValidationResult validate(Link link) { /* Find the fragment */ final Document doc = entry.document; + + if (JAVADOC_FRAGMENT_CHARS.matcher(fragment).find()) { + /* Those chars are illegal in CSS selectors, so Tagsoup will fail at parsing the selector */ + final String id = fragment.substring(1); + if (doc.getElementById(id) != null) { + return logAndReturn(uri, ValidationResult.valid(link)); + } + } + try { Elements foundElems = doc.select(fragment); if (foundElems.isEmpty()) { @@ -140,7 +152,7 @@ public ValidationResult validate(Link link) { return logAndReturn(uri, result); } } catch (SelectorParseException e) { - log.error("Bad fragment: fragment", e); + log.error("Bad fragment: " + fragment + " in URI " + link.originalUri(), e); throw e; } } finally { diff --git a/docs/modules/ROOT/pages/test-page.adoc b/docs/modules/ROOT/pages/test-page.adoc index d635f8a..83d5873 100644 --- a/docs/modules/ROOT/pages/test-page.adoc +++ b/docs/modules/ROOT/pages/test-page.adoc @@ -19,4 +19,5 @@ Some valid links: * https://quarkus.io/guides/building-native-image#prerequisites[good fragment] * {test-link-quarkus-docs-base}/getting-started[link with an AsciiDoc attribute on line 21] +* https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Duration.html#parse(java.lang.CharSequence)[Bad CSS selector, but still a good fragment]