userscripts icon indicating copy to clipboard operation
userscripts copied to clipboard

FIRE: anchor tags without an href lead to an error

Open PurpleMagick opened this issue 1 year ago • 1 comments

If a post has an anchor tag without any href, for example <a></a>, then there is an error in pointRelativeURLsToSourceSESite().

Code with reproduced here for ease of access:

reportBody.find('a').each(function () {
      const $this = $(this);
      let href = $this.attr('href');
      if (!/^(?:[a-z]+:)?\/\//.test(href)) {
        // It's not a fully qualified or protocol-relative link.
        if (href.startsWith('/')) {

href.startsWith('/') throws an error because href is undefined.

Example report with an anchor that is missing an href: https://m.erwaysoftware.com/posts/uid/stackoverflow/78706588

Chat link for the report: https://chat.stackexchange.com/transcript/message/65904497#65904497

PurpleMagick avatar Jul 04 '24 12:07 PurpleMagick

Thanks for the report. Yep,

let href = $this.attr('href');

should be

let href = $this.attr('href') || '';

This issue also affects the preview tab on MS (FIRE and MS share this code). I've already pushed code to MS, but have not, yet, been able to deploy it.

Interestingly, this error occurs in this post due to an error in parsing caused by SmokeDetector providing MS with a processed version of the HTML with all the HTML entities converted to characters. This results in the "<a>" that's supposed to be actual text (i.e., actually &lt;a&gt;) being seen as a valid <a> tag. Given the processed nature of the text supplied to MS, there is, unfortunately, no way to accurately recover the real text in 100% of all cases.

makyen avatar Jul 04 '24 16:07 makyen