-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from 6 commits
6c84748
435b236
c55a3de
fd7090c
e29bf56
a7d2423
91510c5
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 |
---|---|---|
|
@@ -13,19 +13,30 @@ class CrawlableLinks extends Gatherer { | |
* @param {LH.Gatherer.PassContext} passContext | ||
* @return {Promise<LH.Artifacts['CrawlableLinks']>} | ||
*/ | ||
afterPass(passContext) { | ||
async afterPass(passContext) { | ||
const expression = `(function() { | ||
${pageFunctions.getElementsInDocumentString}; // define function on page | ||
const resolveURLOrNull = url => { | ||
try { return new URL(url, window.location.href).href; } | ||
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. __nativeURL 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. I'll do ya one better, I'll enable |
||
catch (_) { return null; } | ||
}; | ||
|
||
const selector = 'a[href]:not([rel~="nofollow"])'; | ||
const elements = getElementsInDocument(selector); | ||
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. how about getElementsInDocument(selector).filter(e => __ElementMatches.call(e, 'svg a')) 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. not 100% sure what you're requesting, like we separate them in two different arrays? or just that we use 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. 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. 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. yeah I agree it's all terrible, I started with |
||
return elements | ||
.map(node => ({ | ||
href: node.href, | ||
text: node.innerText | ||
href: node.href instanceof SVGAnimatedString ? | ||
resolveURLOrNull(node.href.baseVal) : | ||
node.href, | ||
text: node.href instanceof SVGAnimatedString ? | ||
node.textContent : | ||
node.innerText, | ||
})); | ||
})()`; | ||
|
||
return passContext.driver.evaluateAsync(expression); | ||
/** @type {LH.Artifacts['CrawlableLinks']} */ | ||
const links = await passContext.driver.evaluateAsync(expression, {useIsolation: true}); | ||
return links.filter(link => typeof link.href === 'string' && link.href); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'll revert 👍