fix: deduplicate regex capture group in HTML link attribute parser#640
fix: deduplicate regex capture group in HTML link attribute parser#640fix2015 wants to merge 1 commit into
Conversation
The HTML_LINK_ATTR_RE regex in asset-references.ts had an unreachable third capture group that was identical to the first (both matching ASCII double-quoted attribute values). The regex engine always matches group 1 first, making group 3 dead code. Replace the duplicate with a curly/smart quote alternative (\u201c...\u201d) so pasted HTML with typographic quotes is handled correctly.
|
@fix2015, thank you for your contribution. Can you provide an example of the HTML pasted from a word processor that would verify your fix? |
|
Hey, good question! So the issue is about HTML that comes from word processors like Microsoft Word or Google Docs — they tend to replace straight quotes with curly/smart quotes (the For example, if someone pastes this kind of markup: <a href="https://example.com/report.pdf">Download Report</a>
<img src="./images/chart.png" alt="Chart" />The old regex had group 3 as The fix replaces that dead group with Quick way to verify: const old_re = /<[\w:-]+\b[^>]*?\s+(?:href|src)\s*=\s*(?:"([^"\n]*)"|'([^'\n]*)'|"([^"\n]*)"|([^\s"'=<>`]+))/gi;
const new_re = /<[\w:-]+\b[^>]*?\s+(?:href|src)\s*=\s*(?:"([^"\n]*)"|'([^'\n]*)'|\u201c([^\u201d\n]*)\u201d|([^\s"'=<>`]+))/gi;
const html = '<a href=\u201chttps://example.com/report.pdf\u201d>Download</a>';
old_re.exec(html)?.[3]; // undefined — group never matches
new_re.exec(html)?.[3]; // "https://example.com/report.pdf" ✓Hope that helps! |
|
Thanks for the contribution! A maintainer will review and merge your PR. Your commit attribution is preserved as @fix2015. What happens next:
This comment will be updated as the status changes. |
|
Hey @fix2015, thanks so much for taking the time on this and for the detailed writeup. I went and ran it through our extractor and tests so I could really understand what's going on, and I think there's a fun little gotcha here plus a genuinely useful thread to pull on. Good news first: the smart-quote case you're after already works on The reason it looked like a duplicate is a really easy trap to fall into: when the "old" regex got pasted into the earlier comment, the curly quotes in group 3 got auto-corrected to straight ones along the way (which is, funnily enough, the exact smart-quote behavior this code exists to handle). So group 3 reads like dead code in the comment, but in the file it's been the word-processor branch all along. There's even a test for it that's green today, in One thing I did want to flag, gently: I tried the branch's regex locally and it looks like it accidentally trips up the ordinary straight-quote case. Same suite, swapping in this PR's regex: They all trace back to one thing: the new group 1 expects a So it's a bit of a tradeoff we'd want to avoid, since the smart-quote handling it adds is already there. That said, you did surface something real: curly single quotes (U+2018 And honestly the thing I'm most curious about: how did you hit this in the first place? If you've got a real doc or paste where an |
HTML_LINK_ATTR_RE in asset-references.ts had group 3 as an exact duplicate of group 1 (both matched ASCII double-quoted strings). Replaced the duplicate with a curly/smart quote alternative so HTML pasted from word processors correctly extracts attribute values.