-
Notifications
You must be signed in to change notification settings - Fork 166
Normalize links in HTML subset formatting #190
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,12 @@ public class Utilities { | |
| private static final Pattern CLOSING_A_TAG_PATTERN = Pattern.compile( | ||
| "</a>", Pattern.CASE_INSENSITIVE); | ||
| private static final Pattern OPENING_A_TAG_PATTERN = Pattern.compile( | ||
| "<a href=.*?>", Pattern.CASE_INSENSITIVE); | ||
| "<a\\s+href\\s*=.*?>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); | ||
| private static final Pattern A_HREF_PATTERN = Pattern.compile( | ||
| "<a\\s+href\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s\"'=<>`]+))\\s*>", | ||
| Pattern.CASE_INSENSITIVE); | ||
|
snoopdave marked this conversation as resolved.
|
||
| private static final Pattern SUBSET_LINK_PATTERN = Pattern.compile( | ||
| "(?:https?://|mailto:)[^\\x00-\\x20\\x7f]+", Pattern.CASE_INSENSITIVE); | ||
| private static final Pattern QUOTE_PATTERN = Pattern.compile(""", | ||
| Pattern.CASE_INSENSITIVE); | ||
|
|
||
|
|
@@ -975,23 +980,39 @@ public static String transformToHTMLSubset(String s) { | |
| s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>"); | ||
| s = replace(s, QUOTE_PATTERN, "\""); | ||
|
|
||
| // HTTP links | ||
| // Normalize supported links while retaining the surrounding text. | ||
| s = replace(s, CLOSING_A_TAG_PATTERN, "</a>"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐞Claude Issue: Important: This is pre-existing, but the loop is being rewritten here and already has an "unsupported anchor" output (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖Claude: fixed by broadening The pattern requires whitespace or the closing delimiter after |
||
| Matcher m = OPENING_A_TAG_PATTERN.matcher(s); | ||
| StringBuilder result = new StringBuilder(s.length()); | ||
| int end = 0; | ||
| while (m.find()) { | ||
| int start = m.start(); | ||
| int end = m.end(); | ||
| String link = s.substring(start, end); | ||
| link = "<" + link.substring(4, link.length() - 4) + ">"; | ||
| s = s.substring(0, start) + link + s.substring(end, s.length()); | ||
| m = OPENING_A_TAG_PATTERN.matcher(s); | ||
| result.append(restoreSubsetEntities(s.substring(end, m.start()))); | ||
| Matcher hrefMatcher = A_HREF_PATTERN.matcher(m.group()); | ||
| String link = "<a>"; | ||
| if (hrefMatcher.matches()) { | ||
| String href = hrefMatcher.group(1); | ||
| if (href == null) { | ||
| href = hrefMatcher.group(2); | ||
| } | ||
| if (href == null) { | ||
| href = hrefMatcher.group(3); | ||
| } | ||
| href = StringEscapeUtils.unescapeHtml4(restoreSubsetEntities(href)); | ||
| if (SUBSET_LINK_PATTERN.matcher(href).matches()) { | ||
| link = "<a href=\"" + StringEscapeUtils.escapeHtml4(href) + "\">"; | ||
| } | ||
| } | ||
| result.append(link); | ||
| end = m.end(); | ||
| } | ||
| result.append(restoreSubsetEntities(s.substring(end))); | ||
| return result.toString(); | ||
| } | ||
|
|
||
| // escaped angle brackets | ||
| private static String restoreSubsetEntities(String s) { | ||
| s = s.replace("&lt;", "<"); | ||
| s = s.replace("&gt;", ">"); | ||
| s = s.replace("&#", "&#"); | ||
|
|
||
| return s; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.