Charcoal-SE / userscripts

Collection of userscripts that are used by/are useful to Charcoal.
https://charcoal-se.org/scripts
Apache License 2.0
28 stars 27 forks source link

FIRE: anchor tags without an href lead to an error #239

Open PurpleMagick opened 6 days ago

PurpleMagick commented 6 days ago

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

makyen commented 6 days ago

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.