Skip to content
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

core(seo): properly handle anchors in SVG #6483

Merged
merged 7 commits into from
Nov 6, 2018

Conversation

patrickhulce
Copy link
Collaborator

Summary
Links inside SVG here throwing the error here and links without any text content were passing the audit too, which seemed wrong, @rviscomi does that check out to you too? I couldn't find any discussion in the original PR (#3490) about it.

Related Issues/PRs
fixes #6481

const expression = `(function() {
${pageFunctions.getElementsInDocumentString}; // define function on page
const resolveURLOrNull = url => {
try { return new URL(url, window.location.href).href; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__nativeURL

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do ya one better, I'll enable useIsolation :)

try { return new URL(url, window.location.href).href; }
catch (_) { return null; }
};

const selector = 'a[href]:not([rel~="nofollow"])';
const elements = getElementsInDocument(selector);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

getElementsInDocument(selector).filter(e => __ElementMatches.call(e, 'svg a'))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure what you're requesting, like we separate them in two different arrays? or just that we use .matches to replace the instanceof check?

Copy link
Member

@paulirish paulirish Nov 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm that was the idea but the fact that resolving these baseVal is so annoying makes it kinda not worth it.

actually how about

const elements = getElementsInDocument(selector).filter(e => __ElementMatches.call(e, 'svg a'))
return elements.map(node => ({
  href: new __URL(node.getAttribute('href'), window.location.href).toString(),  // but with try/catch
  text: // ...
        // oh but then links in SVG don't have innerText
        // node.innerText || node.textContent  ??

okay yeah this is all terrible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree it's all terrible, I started with node.innerText || node.textContent but then got worried about potential accidental fallbacks with script/style tag contents when it's empty and whatnot, wanted to keep it as narrow as possible.

Copy link
Member

@paulirish paulirish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

godspeed

try { return new URL(url, window.location.href).href; }
catch (_) { return null; }
};

const selector = 'a[href]:not([rel~="nofollow"])';
const elements = getElementsInDocument(selector);
Copy link
Member

@paulirish paulirish Nov 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm that was the idea but the fact that resolving these baseVal is so annoying makes it kinda not worth it.

actually how about

const elements = getElementsInDocument(selector).filter(e => __ElementMatches.call(e, 'svg a'))
return elements.map(node => ({
  href: new __URL(node.getAttribute('href'), window.location.href).toString(),  // but with try/catch
  text: // ...
        // oh but then links in SVG don't have innerText
        // node.innerText || node.textContent  ??

okay yeah this is all terrible.

@@ -8,6 +8,7 @@
const Audit = require('../audit');
const URL = require('../../lib/url-shim');
const BLOCKLIST = new Set([
'', // no description is also bad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will have a big effect on a lot of anchors. Not sure if we want to pull this trigger without doing more research first. For now can we fix the svg > a failure without changing the audit scoring? We can open a separate FR to weigh in on auditing for empty anchor text.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'll revert 👍

@patrickhulce patrickhulce changed the title core(seo): consider links with no text non-descriptive core(seo): properly handle anchors in SVG Nov 6, 2018
@patrickhulce patrickhulce merged commit abf7c4d into master Nov 6, 2018
@patrickhulce patrickhulce deleted the fix_seo_crawlable_links branch November 6, 2018 23:23
@cmoss3
Copy link

cmoss3 commented Nov 8, 2018

I see that some changes have been merged here but the issue I brought up in my original ticket (#6481) is still occurring. I'm wondering how long it will be before this fix is available for the lighthouse software that ships with Chrome. Do I need to wait until the next release? If so, can I just download the master branch and use it as-is with NPM?

@patrickhulce
Copy link
Collaborator Author

You'll have to wait for the next release with this commit merged, it will likely hit Chrome Canary in a few weeks and Chrome stable in a few months.

If you're feeling especially brave you could try installing LH HEAD directly from GitHub, but proceed at your own risk :) npm install GoogleChrome/lighthouse#master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SEO Audit Failure: link.href.toLowerCase is not a function
4 participants