From 1a3fa4cff53911eb9f8b30a12f04d6f81481a525 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Tue, 28 Apr 2020 22:33:37 +0100 Subject: [PATCH 01/15] new_audit(anchor-href): adds anchor-href audit --- .../test/cli/__snapshots__/index-test.js.snap | 8 ++ lighthouse-core/audits/seo/anchor-href.js | 86 +++++++++++++++ lighthouse-core/config/default-config.js | 2 + .../gather/gatherers/anchor-elements.js | 2 + lighthouse-core/lib/i18n/locales/en-US.json | 12 ++ lighthouse-core/lib/i18n/locales/en-XL.json | 12 ++ .../test/audits/seo/anchor-href-test.js | 44 ++++++++ .../test/report/report-generator-test.js | 2 +- .../test/results/artifacts/artifacts.json | 103 +++++++++++++++--- lighthouse-core/test/results/sample_v2.json | 100 ++++++++++++++--- types/artifacts.d.ts | 3 + 11 files changed, 341 insertions(+), 33 deletions(-) create mode 100644 lighthouse-core/audits/seo/anchor-href.js create mode 100644 lighthouse-core/test/audits/seo/anchor-href-test.js diff --git a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap index e106723d1b59..be5a56018aad 100644 --- a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap +++ b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap @@ -402,6 +402,9 @@ Object { Object { "path": "seo/link-text", }, + Object { + "path": "seo/anchor-href", + }, Object { "path": "seo/is-crawlable", }, @@ -1084,6 +1087,11 @@ Object { "id": "link-text", "weight": 1, }, + Object { + "group": "seo-crawl", + "id": "anchor-href", + "weight": 1, + }, Object { "group": "seo-crawl", "id": "is-crawlable", diff --git a/lighthouse-core/audits/seo/anchor-href.js b/lighthouse-core/audits/seo/anchor-href.js new file mode 100644 index 000000000000..804e23b102d0 --- /dev/null +++ b/lighthouse-core/audits/seo/anchor-href.js @@ -0,0 +1,86 @@ +/** + * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const Audit = require('../audit.js'); +const i18n = require('../../lib/i18n/i18n.js'); + +const UIStrings = { + /** Title of a Lighthouse audit that provides detail on whether anchors have hyperlinks which can be crawled by search engines. This descriptive title is shown when all hyperlinks on the page are crawlable. */ + title: 'Anchors have crawlable hyperlinks', + /** Descriptive title of a Lighthouse audit that provides detail on whether anchors have hyperlinks which can be crawled by search engines. This descriptive title is shown when there are hyperlinks which are not crawlable by search engines. */ + failureTitle: 'Anchors do not have crawlable hyperlinks', + /** Description of a Lighthouse audit that tells the user why hyperlinks should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ + description: 'Search engines use hyperlinks to crawl websites', + /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ + columnFailingAnchors: 'Failing Anchor Elements', +}; + +const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); + +class AnchorHref extends Audit { + /** + * @return {LH.Audit.Meta} + */ + static get meta() { + return { + id: 'anchor-href', + title: str_(UIStrings.title), + failureTitle: str_(UIStrings.failureTitle), + description: str_(UIStrings.description), + requiredArtifacts: ['AnchorElements'], + }; + } + + /** + * @param {LH.Artifacts} artifacts + * @return {LH.Audit.Product} + */ + static audit({AnchorElements: anchorElements}) { + const failingAnchorHrefs = anchorElements.filter(({rawHref}) => { + if (!rawHref) { + return true; + } + + if (rawHref === '#') { + return true; + } + + if (rawHref.startsWith('javascript:')) { + return true; + } + + if (rawHref.startsWith('file:')) { + return true; + } + }); + + /** @type {LH.Audit.Details.Table['headings']} */ + const headings = [{ + key: 'node', + itemType: 'node', + text: str_(UIStrings.columnFailingAnchors), + }]; + + /** @type {LH.Audit.Details.Table['items']} */ + const itemsToDisplay = failingAnchorHrefs.map(node => { + return { + node: { + type: 'node', + snippet: node.outerHTML, + }, + }; + }); + + return { + score: Number(failingAnchorHrefs.length === 0), + details: Audit.makeTableDetails(headings, itemsToDisplay), + }; + } +} + +module.exports = AnchorHref; +module.exports.UIStrings = UIStrings; diff --git a/lighthouse-core/config/default-config.js b/lighthouse-core/config/default-config.js index b1f282c1c36e..eb987da7bdba 100644 --- a/lighthouse-core/config/default-config.js +++ b/lighthouse-core/config/default-config.js @@ -306,6 +306,7 @@ const defaultConfig = { 'seo/http-status-code', 'seo/font-size', 'seo/link-text', + 'seo/anchor-href', 'seo/is-crawlable', 'seo/robots-txt', 'seo/tap-targets', @@ -534,6 +535,7 @@ const defaultConfig = { {id: 'meta-description', weight: 1, group: 'seo-content'}, {id: 'http-status-code', weight: 1, group: 'seo-crawl'}, {id: 'link-text', weight: 1, group: 'seo-content'}, + {id: 'anchor-href', weight: 1, group: 'seo-crawl'}, {id: 'is-crawlable', weight: 1, group: 'seo-crawl'}, {id: 'robots-txt', weight: 1, group: 'seo-crawl'}, {id: 'image-alt', weight: 1, group: 'seo-content'}, diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index 21caa97eefa5..8df1c281707f 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -47,6 +47,7 @@ function collectAnchorElements() { if (node instanceof HTMLAnchorElement) { return { href: node.href, + rawHref: node.getAttribute('href') || '', text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, target: node.target, @@ -59,6 +60,7 @@ function collectAnchorElements() { return { href: resolveURLOrEmpty(node.href.baseVal), + rawHref: node.getAttribute('href') || '', text: node.textContent || '', rel: '', target: node.target.baseVal || '', diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 086a81467816..46e5f27996c6 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -965,6 +965,18 @@ "lighthouse-core/audits/resource-summary.js | title": { "message": "Keep request counts low and transfer sizes small" }, + "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": { + "message": "Failing Anchor Elements" + }, + "lighthouse-core/audits/seo/anchor-href.js | description": { + "message": "Search engines use hyperlinks to crawl websites" + }, + "lighthouse-core/audits/seo/anchor-href.js | failureTitle": { + "message": "Anchors do not have crawlable hyperlinks" + }, + "lighthouse-core/audits/seo/anchor-href.js | title": { + "message": "Anchors have crawlable hyperlinks" + }, "lighthouse-core/audits/seo/canonical.js | description": { "message": "Canonical links suggest which URL to show in search results. [Learn more](https://web.dev/canonical)." }, diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index cc845329f82d..ecf6aba42f3d 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -965,6 +965,18 @@ "lighthouse-core/audits/resource-summary.js | title": { "message": "K̂éêṕ r̂éq̂úêśt̂ ćôún̂t́ŝ ĺôẃ âńd̂ t́r̂án̂śf̂ér̂ śîźêś ŝḿâĺl̂" }, + "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": { + "message": "F̂áîĺîńĝ Án̂ćĥór̂ Él̂ém̂én̂t́ŝ" + }, + "lighthouse-core/audits/seo/anchor-href.js | description": { + "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ" + }, + "lighthouse-core/audits/seo/anchor-href.js | failureTitle": { + "message": "Âńĉh́ôŕŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́ŷṕêŕl̂ín̂ḱŝ" + }, + "lighthouse-core/audits/seo/anchor-href.js | title": { + "message": "Âńĉh́ôŕŝ h́âv́ê ćr̂áŵĺâb́l̂é ĥýp̂ér̂ĺîńk̂ś" + }, "lighthouse-core/audits/seo/canonical.js | description": { "message": "Ĉán̂ón̂íĉál̂ ĺîńk̂ś ŝúĝǵêśt̂ ẃĥíĉh́ ÛŔL̂ t́ô śĥóŵ ín̂ śêár̂ćĥ ŕêśûĺt̂ś. [L̂éâŕn̂ ḿôŕê](https://web.dev/canonical)." }, diff --git a/lighthouse-core/test/audits/seo/anchor-href-test.js b/lighthouse-core/test/audits/seo/anchor-href-test.js new file mode 100644 index 000000000000..f03006c0520a --- /dev/null +++ b/lighthouse-core/test/audits/seo/anchor-href-test.js @@ -0,0 +1,44 @@ +/** + * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const AnchorHrefAudit = require('../../../audits/seo/anchor-href.js'); +const assert = require('assert'); + +/* eslint-env jest */ + +function runAudit(rawHref) { + const {score} = AnchorHrefAudit.audit({ + AnchorElements: [{ + rawHref, + }], + }); + + return score; +} + +describe('SEO: Anchor Href audit', () => { + it('allows crawlable hrefs', () => { + assert.equal(runAudit('#top'), 1, 'hash fragment identifier'); + assert.equal(runAudit('mailto:name@example.com'), 1, 'email link with a mailto URI'); + assert.equal(runAudit('https://example.com'), 1, 'absolute HTTPs URL'); + assert.equal(runAudit('foo'), 1, 'relative URL'); + assert.equal(runAudit('/foo'), 1, 'relative URL'); + assert.equal(runAudit('#:~:text=string'), 1, 'hyperlink with a text fragment'); + assert.equal(runAudit('ftp://myname@host.dom'), 1, 'an FTP hyperlink'); + assert.equal(runAudit('http://172.217.20.78'), 1, 'IP address based link'); + assert.equal(runAudit('//example.com'), 1, 'protocol relative link'); + assert.equal(runAudit('?query=string'), 1, 'relative link which specifies a query string'); + assert.equal(runAudit('tel:5555555'), 1, 'email link with a tel URI'); + }); + + it('disallows uncrawlable hrefs', () => { + assert.equal(runAudit(''), 0, 'link empty quotes for the href attribute'); + assert.equal(runAudit('#'), 0, 'link with only a hash symbol'); + assert.equal(runAudit('javascript:void(0)'), 0, 'hyperlink with a `javascript:` URI'); + assert.equal(runAudit('file:///image.png'), 0, 'hyperlink with a `file:` URI'); + }); +}); diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 997ac987374c..3e8e75016d7e 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -101,7 +101,7 @@ describe('ReportGenerator', () => { fs.writeFileSync(path, csvOutput); const lines = csvOutput.split('\n'); - expect(lines).toHaveLength(145); + expect(lines).toHaveLength(146); expect(lines.slice(0, 2).join('\n')).toMatchInlineSnapshot(` "requestedUrl,finalUrl,category,name,title,type,score \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"first-contentful-paint\\",\\"First Contentful Paint\\",\\"numeric\\",\\"0.51\\" diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index 13d0134538de..fa6f50c02161 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -1749,49 +1749,124 @@ "AnchorElements": [ { "href": "https://www.google.com/", - "outerHTML": "Hello", + "rawHref": "https://www.google.com/", + "text": "external link", "rel": "", "target": "_blank", - "text": "external link" + "devtoolsNodePath": "3,HTML,1,BODY,36,A", + "selector": "body > a", + "nodeLabel": "external link", + "outerHTML": "" }, { "href": "", - "outerHTML": "Hello", + "rawHref": "", + "text": "external link", "rel": "", "target": "_blank", - "text": "Hello" + "devtoolsNodePath": "3,HTML,1,BODY,38,A", + "selector": "body > a", + "nodeLabel": "external link", + "outerHTML": "" }, { "href": "https://www.google.com/", - "outerHTML": "Hello", + "rawHref": "https://www.google.com/", + "text": "external link", + "rel": "nofollow", + "target": "_blank", + "devtoolsNodePath": "3,HTML,1,BODY,40,A", + "selector": "body > a", + "nodeLabel": "external link", + "outerHTML": "" + }, + { + "href": "https://www.google.com/", + "rawHref": "https://www.google.com/", + "text": "external link that uses rel noopener and another unrelated rel attribute", + "rel": "noopener nofollow", + "target": "_blank", + "devtoolsNodePath": "3,HTML,1,BODY,42,A", + "selector": "body > a", + "nodeLabel": "external link that uses rel noopener and another unrelated rel attribute", + "outerHTML": "" + }, + { + "href": "https://www.google.com/", + "rawHref": "https://www.google.com/", + "text": "external link that uses rel noreferrer and another unrelated rel attribute", + "rel": "noreferrer nofollow", + "target": "_blank", + "devtoolsNodePath": "3,HTML,1,BODY,44,A", + "selector": "body > a", + "nodeLabel": "external link that uses rel noreferrer and another unrelated rel attribute", + "outerHTML": "" + }, + { + "href": "https://www.google.com/", + "rawHref": "https://www.google.com/", + "text": "external link that uses rel noopener", "rel": "noopener", "target": "_blank", - "text": "external link that uses noopener" + "devtoolsNodePath": "3,HTML,1,BODY,46,A", + "selector": "body > a", + "nodeLabel": "external link that uses rel noopener", + "outerHTML": "" }, { "href": "https://www.google.com/", - "outerHTML": "Hello", - "rel": "nofollow", + "rawHref": "https://www.google.com/", + "text": "external link that uses rel noreferrer", + "rel": "noreferrer", + "target": "_blank", + "devtoolsNodePath": "3,HTML,1,BODY,48,A", + "selector": "body > a", + "nodeLabel": "external link that uses rel noreferrer", + "outerHTML": "" + }, + { + "href": "https://www.google.com/", + "rawHref": "https://www.google.com/", + "text": "external link that uses rel noopener and noreferrer", + "rel": "noopener noreferrer", "target": "_blank", - "text": "external link that uses nofollow" + "devtoolsNodePath": "3,HTML,1,BODY,50,A", + "selector": "body > a", + "nodeLabel": "external link that uses rel noopener and noreferrer", + "outerHTML": "" }, { - "href": "http://localhost:10200/dobetterweb/doesnotexist", + "href": "http://localhost:54106/dobetterweb/doesnotexist", + "rawHref": "./doesnotexist", + "text": "internal link is ok", "rel": "", "target": "_blank", - "text": "internal link is ok" + "devtoolsNodePath": "3,HTML,1,BODY,52,A", + "selector": "body > a", + "nodeLabel": "internal link is ok", + "outerHTML": "" }, { "href": "javascript:void(0)", + "rawHref": "javascript:void(0)", + "text": "", "rel": "", "target": "_blank", - "text": "" + "devtoolsNodePath": "3,HTML,1,BODY,54,A", + "selector": "body > a", + "nodeLabel": "a", + "outerHTML": "" }, { "href": "mailto:inbox@email.com", + "rawHref": "mailto:inbox@email.com", + "text": "", "rel": "", "target": "_blank", - "text": "" + "devtoolsNodePath": "3,HTML,1,BODY,56,A", + "selector": "body > a", + "nodeLabel": "a", + "outerHTML": "" } ], "AppCacheManifest": "clock.appcache", @@ -2105,4 +2180,4 @@ }, "MainDocumentContent": "\n\n\n\n\n\nDoBetterWeb Mega Tester... Of Death\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n
\n

Do better web tester page

\n Hi there!\n\n \n \n Facebook\n \n \n \n
\n\n
touchmove section
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "SourceMaps": [] -} \ No newline at end of file +} diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index a1c2b25cd9f4..fe4d0fcdf01c 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -2988,7 +2988,7 @@ "score": 0, "scoreDisplayMode": "binary", "warnings": [ - "Unable to determine the destination for anchor (
Hello). If not used as a hyperlink, consider removing target=_blank." + "Unable to determine the destination for anchor (). If not used as a hyperlink, consider removing target=_blank." ], "details": { "type": "table", @@ -3003,41 +3003,54 @@ { "node": { "type": "node", - "path": "", - "selector": "", - "nodeLabel": "", - "snippet": "Hello" + "path": "3,HTML,1,BODY,36,A", + "selector": "body > a", + "nodeLabel": "external link", + "snippet": "" }, "href": "https://www.google.com/", "target": "_blank", "rel": "", - "outerHTML": "Hello" + "outerHTML": "" }, { "node": { "type": "node", - "path": "", - "selector": "", - "nodeLabel": "", - "snippet": "Hello" + "path": "3,HTML,1,BODY,38,A", + "selector": "body > a", + "nodeLabel": "external link", + "snippet": "" }, "href": "Unknown", "target": "_blank", "rel": "", - "outerHTML": "Hello" + "outerHTML": "" }, { "node": { "type": "node", - "path": "", - "selector": "", - "nodeLabel": "", - "snippet": "Hello" + "path": "3,HTML,1,BODY,40,A", + "selector": "body > a", + "nodeLabel": "external link", + "snippet": "" }, "href": "https://www.google.com/", "target": "_blank", "rel": "nofollow", - "outerHTML": "Hello" + "outerHTML": "" + }, + { + "node": { + "type": "node", + "path": "3,HTML,1,BODY,52,A", + "selector": "body > a", + "nodeLabel": "internal link is ok", + "snippet": "" + }, + "href": "http://localhost:54106/dobetterweb/doesnotexist", + "target": "_blank", + "rel": "", + "outerHTML": "" } ] } @@ -3449,6 +3462,37 @@ "summary": {} } }, + "anchor-href": { + "id": "anchor-href", + "title": "Anchors do not have crawlable hyperlinks", + "description": "Search engines use hyperlinks to crawl websites", + "score": 0, + "scoreDisplayMode": "binary", + "details": { + "type": "table", + "headings": [ + { + "key": "node", + "itemType": "node", + "text": "Failing Anchor Elements" + } + ], + "items": [ + { + "node": { + "type": "node", + "snippet": "" + } + }, + { + "node": { + "type": "node", + "snippet": "" + } + } + ] + } + }, "is-crawlable": { "id": "is-crawlable", "title": "Page isn’t blocked from indexing", @@ -4272,6 +4316,11 @@ "weight": 1, "group": "seo-content" }, + { + "id": "anchor-href", + "weight": 1, + "group": "seo-crawl" + }, { "id": "is-crawlable", "weight": 1, @@ -4318,7 +4367,7 @@ } ], "id": "seo", - "score": 0.73 + "score": 0.67 }, "pwa": { "title": "Progressive Web App", @@ -5528,6 +5577,12 @@ "duration": 100, "entryType": "measure" }, + { + "startTime": 0, + "name": "lh:audit:anchor-href", + "duration": 100, + "entryType": "measure" + }, { "startTime": 0, "name": "lh:audit:is-crawlable", @@ -6650,7 +6705,7 @@ "lighthouse-core/audits/dobetterweb/external-anchors-use-rel-noopener.js | warning": [ { "values": { - "anchorHTML": "Hello" + "anchorHTML": "" }, "path": "audits[external-anchors-use-rel-noopener].warnings[0]" } @@ -6784,6 +6839,15 @@ "lighthouse-core/audits/seo/link-text.js | description": [ "audits[link-text].description" ], + "lighthouse-core/audits/seo/anchor-href.js | failureTitle": [ + "audits[anchor-href].title" + ], + "lighthouse-core/audits/seo/anchor-href.js | description": [ + "audits[anchor-href].description" + ], + "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": [ + "audits[anchor-href].details.headings[0].text" + ], "lighthouse-core/audits/seo/is-crawlable.js | title": [ "audits[is-crawlable].title" ], diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index 6a4b8394c5c4..0aeddc359db2 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -316,7 +316,10 @@ declare global { /** @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes */ export interface AnchorElement { rel: string + /** The computed href property: https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88517319, use `rawHref` for the exact attribute value */ href: string + /** The exact value of the href attribute value, as it is in the DOM */ + rawHref: string text: string target: string devtoolsNodePath: string From 6ff1ef14faa069b425658e5e1227596009a07e7a Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Mon, 4 May 2020 13:50:05 +0100 Subject: [PATCH 02/15] Rename audit to crawlable-anchors --- .../test/cli/__snapshots__/index-test.js.snap | 4 ++-- .../{anchor-href.js => crawlable-anchors.js} | 12 +++++----- lighthouse-core/config/default-config.js | 4 ++-- lighthouse-core/lib/i18n/locales/en-US.json | 24 +++++++++---------- lighthouse-core/lib/i18n/locales/en-XL.json | 24 +++++++++---------- ...href-test.js => crawlable-anchors-test.js} | 6 ++--- lighthouse-core/test/results/sample_v2.json | 22 ++++++++--------- 7 files changed, 48 insertions(+), 48 deletions(-) rename lighthouse-core/audits/seo/{anchor-href.js => crawlable-anchors.js} (91%) rename lighthouse-core/test/audits/seo/{anchor-href-test.js => crawlable-anchors-test.js} (91%) diff --git a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap index be5a56018aad..917e0b831ef6 100644 --- a/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap +++ b/lighthouse-cli/test/cli/__snapshots__/index-test.js.snap @@ -403,7 +403,7 @@ Object { "path": "seo/link-text", }, Object { - "path": "seo/anchor-href", + "path": "seo/crawlable-anchors", }, Object { "path": "seo/is-crawlable", @@ -1089,7 +1089,7 @@ Object { }, Object { "group": "seo-crawl", - "id": "anchor-href", + "id": "crawlable-anchors", "weight": 1, }, Object { diff --git a/lighthouse-core/audits/seo/anchor-href.js b/lighthouse-core/audits/seo/crawlable-anchors.js similarity index 91% rename from lighthouse-core/audits/seo/anchor-href.js rename to lighthouse-core/audits/seo/crawlable-anchors.js index 804e23b102d0..17ddea107e32 100644 --- a/lighthouse-core/audits/seo/anchor-href.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -21,13 +21,13 @@ const UIStrings = { const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); -class AnchorHref extends Audit { +class CrawlableAnchors extends Audit { /** * @return {LH.Audit.Meta} */ static get meta() { return { - id: 'anchor-href', + id: 'crawlable-anchors', title: str_(UIStrings.title), failureTitle: str_(UIStrings.failureTitle), description: str_(UIStrings.description), @@ -40,7 +40,7 @@ class AnchorHref extends Audit { * @return {LH.Audit.Product} */ static audit({AnchorElements: anchorElements}) { - const failingAnchorHrefs = anchorElements.filter(({rawHref}) => { + const failingAnchors = anchorElements.filter(({rawHref}) => { if (!rawHref) { return true; } @@ -66,7 +66,7 @@ class AnchorHref extends Audit { }]; /** @type {LH.Audit.Details.Table['items']} */ - const itemsToDisplay = failingAnchorHrefs.map(node => { + const itemsToDisplay = failingAnchors.map(node => { return { node: { type: 'node', @@ -76,11 +76,11 @@ class AnchorHref extends Audit { }); return { - score: Number(failingAnchorHrefs.length === 0), + score: Number(failingAnchors.length === 0), details: Audit.makeTableDetails(headings, itemsToDisplay), }; } } -module.exports = AnchorHref; +module.exports = CrawlableAnchors; module.exports.UIStrings = UIStrings; diff --git a/lighthouse-core/config/default-config.js b/lighthouse-core/config/default-config.js index eb987da7bdba..48391619b212 100644 --- a/lighthouse-core/config/default-config.js +++ b/lighthouse-core/config/default-config.js @@ -306,7 +306,7 @@ const defaultConfig = { 'seo/http-status-code', 'seo/font-size', 'seo/link-text', - 'seo/anchor-href', + 'seo/crawlable-anchors', 'seo/is-crawlable', 'seo/robots-txt', 'seo/tap-targets', @@ -535,7 +535,7 @@ const defaultConfig = { {id: 'meta-description', weight: 1, group: 'seo-content'}, {id: 'http-status-code', weight: 1, group: 'seo-crawl'}, {id: 'link-text', weight: 1, group: 'seo-content'}, - {id: 'anchor-href', weight: 1, group: 'seo-crawl'}, + {id: 'crawlable-anchors', weight: 1, group: 'seo-crawl'}, {id: 'is-crawlable', weight: 1, group: 'seo-crawl'}, {id: 'robots-txt', weight: 1, group: 'seo-crawl'}, {id: 'image-alt', weight: 1, group: 'seo-content'}, diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 46e5f27996c6..58a61c970a15 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -965,18 +965,6 @@ "lighthouse-core/audits/resource-summary.js | title": { "message": "Keep request counts low and transfer sizes small" }, - "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": { - "message": "Failing Anchor Elements" - }, - "lighthouse-core/audits/seo/anchor-href.js | description": { - "message": "Search engines use hyperlinks to crawl websites" - }, - "lighthouse-core/audits/seo/anchor-href.js | failureTitle": { - "message": "Anchors do not have crawlable hyperlinks" - }, - "lighthouse-core/audits/seo/anchor-href.js | title": { - "message": "Anchors have crawlable hyperlinks" - }, "lighthouse-core/audits/seo/canonical.js | description": { "message": "Canonical links suggest which URL to show in search results. [Learn more](https://web.dev/canonical)." }, @@ -1004,6 +992,18 @@ "lighthouse-core/audits/seo/canonical.js | title": { "message": "Document has a valid `rel=canonical`" }, + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { + "message": "Failing Anchor Elements" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | description": { + "message": "Search engines use hyperlinks to crawl websites" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { + "message": "Anchors do not have crawlable hyperlinks" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | title": { + "message": "Anchors have crawlable hyperlinks" + }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "Font sizes less than 12px are too small to be legible and require mobile visitors to “pinch to zoom” in order to read. Strive to have >60% of page text ≥12px. [Learn more](https://web.dev/font-size)." }, diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index ecf6aba42f3d..6045fcace992 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -965,18 +965,6 @@ "lighthouse-core/audits/resource-summary.js | title": { "message": "K̂éêṕ r̂éq̂úêśt̂ ćôún̂t́ŝ ĺôẃ âńd̂ t́r̂án̂śf̂ér̂ śîźêś ŝḿâĺl̂" }, - "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": { - "message": "F̂áîĺîńĝ Án̂ćĥór̂ Él̂ém̂én̂t́ŝ" - }, - "lighthouse-core/audits/seo/anchor-href.js | description": { - "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ" - }, - "lighthouse-core/audits/seo/anchor-href.js | failureTitle": { - "message": "Âńĉh́ôŕŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́ŷṕêŕl̂ín̂ḱŝ" - }, - "lighthouse-core/audits/seo/anchor-href.js | title": { - "message": "Âńĉh́ôŕŝ h́âv́ê ćr̂áŵĺâb́l̂é ĥýp̂ér̂ĺîńk̂ś" - }, "lighthouse-core/audits/seo/canonical.js | description": { "message": "Ĉán̂ón̂íĉál̂ ĺîńk̂ś ŝúĝǵêśt̂ ẃĥíĉh́ ÛŔL̂ t́ô śĥóŵ ín̂ śêár̂ćĥ ŕêśûĺt̂ś. [L̂éâŕn̂ ḿôŕê](https://web.dev/canonical)." }, @@ -1004,6 +992,18 @@ "lighthouse-core/audits/seo/canonical.js | title": { "message": "D̂óĉúm̂én̂t́ ĥáŝ á v̂ál̂íd̂ `rel=canonical`" }, + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { + "message": "F̂áîĺîńĝ Án̂ćĥór̂ Él̂ém̂én̂t́ŝ" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | description": { + "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { + "message": "Âńĉh́ôŕŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́ŷṕêŕl̂ín̂ḱŝ" + }, + "lighthouse-core/audits/seo/crawlable-anchors.js | title": { + "message": "Âńĉh́ôŕŝ h́âv́ê ćr̂áŵĺâb́l̂é ĥýp̂ér̂ĺîńk̂ś" + }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "F̂ón̂t́ ŝíẑéŝ ĺêśŝ t́ĥán̂ 12ṕx̂ ár̂é t̂óô śm̂ál̂ĺ t̂ó b̂é l̂éĝíb̂ĺê án̂d́ r̂éq̂úîŕê ḿôb́îĺê v́îśît́ôŕŝ t́ô “ṕîńĉh́ t̂ó ẑóôḿ” îń ôŕd̂ér̂ t́ô ŕêád̂. Śt̂ŕîv́ê t́ô h́âv́ê >60% óf̂ ṕâǵê t́êx́t̂ ≥12ṕx̂. [Ĺêár̂ń m̂ór̂é](https://web.dev/font-size)." }, diff --git a/lighthouse-core/test/audits/seo/anchor-href-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js similarity index 91% rename from lighthouse-core/test/audits/seo/anchor-href-test.js rename to lighthouse-core/test/audits/seo/crawlable-anchors-test.js index f03006c0520a..4aac1f97f12d 100644 --- a/lighthouse-core/test/audits/seo/anchor-href-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -5,13 +5,13 @@ */ 'use strict'; -const AnchorHrefAudit = require('../../../audits/seo/anchor-href.js'); +const CrawlableAnchorsAudit = require('../../../audits/seo/crawlable-anchors.js'); const assert = require('assert'); /* eslint-env jest */ function runAudit(rawHref) { - const {score} = AnchorHrefAudit.audit({ + const {score} = CrawlableAnchorsAudit.audit({ AnchorElements: [{ rawHref, }], @@ -20,7 +20,7 @@ function runAudit(rawHref) { return score; } -describe('SEO: Anchor Href audit', () => { +describe('SEO: Crawlable anchors audit', () => { it('allows crawlable hrefs', () => { assert.equal(runAudit('#top'), 1, 'hash fragment identifier'); assert.equal(runAudit('mailto:name@example.com'), 1, 'email link with a mailto URI'); diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index fe4d0fcdf01c..399218b066fb 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3462,8 +3462,8 @@ "summary": {} } }, - "anchor-href": { - "id": "anchor-href", + "crawlable-anchors": { + "id": "crawlable-anchors", "title": "Anchors do not have crawlable hyperlinks", "description": "Search engines use hyperlinks to crawl websites", "score": 0, @@ -4317,7 +4317,7 @@ "group": "seo-content" }, { - "id": "anchor-href", + "id": "crawlable-anchors", "weight": 1, "group": "seo-crawl" }, @@ -5579,7 +5579,7 @@ }, { "startTime": 0, - "name": "lh:audit:anchor-href", + "name": "lh:audit:crawlable-anchors", "duration": 100, "entryType": "measure" }, @@ -6839,14 +6839,14 @@ "lighthouse-core/audits/seo/link-text.js | description": [ "audits[link-text].description" ], - "lighthouse-core/audits/seo/anchor-href.js | failureTitle": [ - "audits[anchor-href].title" + "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": [ + "audits[crawlable-anchors].title" ], - "lighthouse-core/audits/seo/anchor-href.js | description": [ - "audits[anchor-href].description" + "lighthouse-core/audits/seo/crawlable-anchors.js | description": [ + "audits[crawlable-anchors].description" ], - "lighthouse-core/audits/seo/anchor-href.js | columnFailingAnchors": [ - "audits[anchor-href].details.headings[0].text" + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": [ + "audits[crawlable-anchors].details.headings[0].text" ], "lighthouse-core/audits/seo/is-crawlable.js | title": [ "audits[is-crawlable].title" @@ -7103,4 +7103,4 @@ } } ] -} \ No newline at end of file +} From 36015342b434b71dd125a13e8f90c2cf959ec3e6 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Mon, 4 May 2020 14:06:59 +0100 Subject: [PATCH 03/15] Address merge issue from master --- lighthouse-core/test/report/report-generator-test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 233d2e0e97e6..5f4e86dc2539 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -101,7 +101,6 @@ describe('ReportGenerator', () => { fs.writeFileSync(path, csvOutput); const lines = csvOutput.split('\n'); - expect(lines).toHaveLength(146); expect(lines.length).toBeGreaterThan(100); expect(lines.slice(0, 2).join('\n')).toMatchInlineSnapshot(` "requestedUrl,finalUrl,category,name,title,type,score From 6cea839b9ddefb443b4dc322db85c00d1c6f6345 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 8 May 2020 19:50:47 +0100 Subject: [PATCH 04/15] Relax audit, introduce event listeners to anchor elements gatherer --- .../audits/seo/crawlable-anchors.js | 16 ++--- .../gather/gatherers/anchor-elements.js | 41 +++++++++++- .../test/audits/seo/crawlable-anchors-test.js | 67 ++++++++++++++----- .../test/results/artifacts/artifacts.json | 13 +++- lighthouse-core/test/results/sample_v2.json | 4 +- types/artifacts.d.ts | 2 + 6 files changed, 112 insertions(+), 31 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 17ddea107e32..8ec8c8f6c40e 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -40,20 +40,20 @@ class CrawlableAnchors extends Audit { * @return {LH.Audit.Product} */ static audit({AnchorElements: anchorElements}) { - const failingAnchors = anchorElements.filter(({rawHref}) => { - if (!rawHref) { + const failingAnchors = anchorElements.filter(({rawHref, listeners = [], name = ''}) => { + if (rawHref.startsWith('file:')) { return true; } - if (rawHref === '#') { - return true; - } + const hasClickHandler = listeners.some(({type}) => type === 'click'); - if (rawHref.startsWith('javascript:')) { + if (hasClickHandler || name.trim().length > 0) return; + + if (rawHref === '') { return true; } - if (rawHref.startsWith('file:')) { + if (rawHref.startsWith('javascript:void(0)')) { return true; } }); @@ -70,7 +70,7 @@ class CrawlableAnchors extends Audit { return { node: { type: 'node', - snippet: node.outerHTML, + snippet: node.outerHTML + node.text, }, }; }); diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index 8df1c281707f..f17947bb26c8 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -48,6 +48,7 @@ function collectAnchorElements() { return { href: node.href, rawHref: node.getAttribute('href') || '', + name: node.name, text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, target: node.target, @@ -72,6 +73,28 @@ function collectAnchorElements() { }); } +/** + * @param {LH.Gatherer.PassContext['driver']} driver + * @param {string} devtoolsNodePath + */ +async function getEventListeners(driver, devtoolsNodePath) { + const {nodeId} = await driver.sendCommand('DOM.pushNodeByPathToFrontend', { + path: devtoolsNodePath + }); + + const {object: {objectId = ''}} = await driver.sendCommand('DOM.resolveNode', { + nodeId + }); + + const response = await driver.sendCommand('DOMDebugger.getEventListeners', { + objectId + }); + + if (response.listeners.length > 0) { + return response; + } +} + class AnchorElements extends Gatherer { /** * @param {LH.Gatherer.PassContext} passContext @@ -89,8 +112,22 @@ class AnchorElements extends Gatherer { return (${collectAnchorElements})(); })()`; - /** @type {Array} */ - return driver.evaluateAsync(expression, {useIsolation: true}); + /** @type {LH.Artifacts['AnchorElements']} */ + const anchors = await driver.evaluateAsync(expression, {useIsolation: true}); + await driver.sendCommand('DOM.enable'); + + // DOM.getDocument is necessary for pushNodesByBackendIdsToFrontend to properly retrieve nodeIds. + await driver.sendCommand('DOM.getDocument', {depth: -1, pierce: true}); + const anchorsWithEventListeners = anchors.map(async anchor => { + return { + ...anchor, + ...await getEventListeners(driver, anchor.devtoolsNodePath) + } + }); + + const result = await Promise.all(anchorsWithEventListeners); + await driver.sendCommand('DOM.disable'); + return result; } } diff --git a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js index 4aac1f97f12d..ba69f0f1ebe4 100644 --- a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -10,10 +10,12 @@ const assert = require('assert'); /* eslint-env jest */ -function runAudit(rawHref) { +function runAudit({rawHref, listeners, name = ''}) { const {score} = CrawlableAnchorsAudit.audit({ AnchorElements: [{ rawHref, + name, + ...(listeners && listeners.length && {listeners}), }], }); @@ -21,24 +23,53 @@ function runAudit(rawHref) { } describe('SEO: Crawlable anchors audit', () => { - it('allows crawlable hrefs', () => { - assert.equal(runAudit('#top'), 1, 'hash fragment identifier'); - assert.equal(runAudit('mailto:name@example.com'), 1, 'email link with a mailto URI'); - assert.equal(runAudit('https://example.com'), 1, 'absolute HTTPs URL'); - assert.equal(runAudit('foo'), 1, 'relative URL'); - assert.equal(runAudit('/foo'), 1, 'relative URL'); - assert.equal(runAudit('#:~:text=string'), 1, 'hyperlink with a text fragment'); - assert.equal(runAudit('ftp://myname@host.dom'), 1, 'an FTP hyperlink'); - assert.equal(runAudit('http://172.217.20.78'), 1, 'IP address based link'); - assert.equal(runAudit('//example.com'), 1, 'protocol relative link'); - assert.equal(runAudit('?query=string'), 1, 'relative link which specifies a query string'); - assert.equal(runAudit('tel:5555555'), 1, 'email link with a tel URI'); + it('allows crawlable anchors', () => { + assert.equal(runAudit({rawHref:'#top'}), 1, 'hash fragment identifier'); + assert.equal(runAudit({rawHref:'mailto:name@example.com'}), 1, 'email link with a mailto URI'); + assert.equal(runAudit({rawHref:'https://example.com'}), 1, 'absolute HTTPs URL'); + assert.equal(runAudit({rawHref:'foo'}), 1, 'relative URL'); + assert.equal(runAudit({rawHref:'/foo'}), 1, 'relative URL'); + assert.equal(runAudit({rawHref:'#:~:text=string'}), 1, 'hyperlink with a text fragment'); + assert.equal(runAudit({rawHref:'ftp://myname@host.dom'}), 1, 'an FTP hyperlink'); + assert.equal(runAudit({rawHref:'http://172.217.20.78'}), 1, 'IP address based link'); + assert.equal(runAudit({rawHref:'//example.com'}), 1, 'protocol relative link'); + assert.equal(runAudit({rawHref:'?query=string'}), 1, 'relative link which specifies a query string'); + assert.equal(runAudit({rawHref:'tel:5555555'}), 1, 'email link with a tel URI'); + assert.equal(runAudit({rawHref:'#'}), 1, 'link with only a hash symbol'); + assert.equal(runAudit({rawHref:'', name: 'name'}), 1, 'link with a name attribute'); }); - it('disallows uncrawlable hrefs', () => { - assert.equal(runAudit(''), 0, 'link empty quotes for the href attribute'); - assert.equal(runAudit('#'), 0, 'link with only a hash symbol'); - assert.equal(runAudit('javascript:void(0)'), 0, 'hyperlink with a `javascript:` URI'); - assert.equal(runAudit('file:///image.png'), 0, 'hyperlink with a `file:` URI'); + it('allows certain anchors which use event listeners on themselves', () => { + const auditResultJavaScriptURI = runAudit({ + rawHref:'javascript:void(0)', + listeners: [{type: 'click'}], + }); + assert.equal(auditResultJavaScriptURI, 1, 'hyperlink with a `javascript:` URI'); + + const auditResultEmptyQuotes = runAudit({ + rawHref:'', + listeners: [{type: 'click'}], + }); + assert.equal(auditResultEmptyQuotes, 1, 'link with empty quotes for the href attribute'); + }); + + it('checks the validity of the listeners', () => { + const auditResultBadEvent = runAudit({ + rawHref:'', + listeners: [{type: 'no'}], + }); + assert.equal(auditResultBadEvent, 0, 'link with unsupported event listener'); + + const auditResultGoodEvent = runAudit({ + rawHref:'', + listeners: [{type: 'no'}, {type: 'click'}], + }); + assert.equal(auditResultGoodEvent, 1, 'link with one supported and one unsupported event listener'); + }); + + it('disallows uncrawlable anchors', () => { + assert.equal(runAudit({rawHref:'javascript:void(0)'}), 0, 'hyperlink with a `javascript:` URI'); + assert.equal(runAudit({rawHref:''}), 0, 'link with empty quotes for the href attribute'); + assert.equal(runAudit({rawHref:'file:///image.png'}), 0, 'hyperlink with a `file:` URI'); }); }); diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index e9f73187c7a1..f227e9fbbb98 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -1750,6 +1750,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link", "rel": "", "target": "_blank", @@ -1761,6 +1762,7 @@ { "href": "", "rawHref": "", + "name": "", "text": "external link", "rel": "", "target": "_blank", @@ -1772,6 +1774,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link", "rel": "nofollow", "target": "_blank", @@ -1783,6 +1786,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link that uses rel noopener and another unrelated rel attribute", "rel": "noopener nofollow", "target": "_blank", @@ -1794,6 +1798,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link that uses rel noreferrer and another unrelated rel attribute", "rel": "noreferrer nofollow", "target": "_blank", @@ -1805,6 +1810,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link that uses rel noopener", "rel": "noopener", "target": "_blank", @@ -1816,6 +1822,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link that uses rel noreferrer", "rel": "noreferrer", "target": "_blank", @@ -1827,6 +1834,7 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "name": "", "text": "external link that uses rel noopener and noreferrer", "rel": "noopener noreferrer", "target": "_blank", @@ -1836,8 +1844,9 @@ "outerHTML": "" }, { - "href": "http://localhost:54106/dobetterweb/doesnotexist", + "href": "http://localhost:51938/dobetterweb/doesnotexist", "rawHref": "./doesnotexist", + "name": "", "text": "internal link is ok", "rel": "", "target": "_blank", @@ -1849,6 +1858,7 @@ { "href": "javascript:void(0)", "rawHref": "javascript:void(0)", + "name": "", "text": "", "rel": "", "target": "_blank", @@ -1860,6 +1870,7 @@ { "href": "mailto:inbox@email.com", "rawHref": "mailto:inbox@email.com", + "name": "", "text": "", "rel": "", "target": "_blank", diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index 975335d042e1..9a2916357a5a 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3510,7 +3510,7 @@ { "node": { "type": "node", - "snippet": "" + "snippet": "external link" } }, { @@ -7199,4 +7199,4 @@ } } ] -} +} \ No newline at end of file diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index 7e19d34ca7b3..0151654083c3 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -322,12 +322,14 @@ declare global { href: string /** The exact value of the href attribute value, as it is in the DOM */ rawHref: string + name?: string text: string target: string devtoolsNodePath: string selector: string nodeLabel: string outerHTML: string + listeners?: Array } export interface Font { From def16b64ae2fe1c2afbdf5516e70782163b0e37d Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Thu, 14 May 2020 00:29:36 +0100 Subject: [PATCH 05/15] Check for uncrawlable strings in the onclick attribute --- .../audits/seo/crawlable-anchors.js | 36 ++++-- .../gather/gatherers/anchor-elements.js | 23 ++-- lighthouse-core/lib/i18n/locales/en-US.json | 4 +- lighthouse-core/lib/i18n/locales/en-XL.json | 4 +- .../test/audits/seo/crawlable-anchors-test.js | 107 +++++++++++------- .../test/results/artifacts/artifacts.json | 30 ++++- lighthouse-core/test/results/sample_v2.json | 14 ++- types/artifacts.d.ts | 3 +- 8 files changed, 153 insertions(+), 68 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 8ec8c8f6c40e..1ed9334c1ea4 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -14,9 +14,9 @@ const UIStrings = { /** Descriptive title of a Lighthouse audit that provides detail on whether anchors have hyperlinks which can be crawled by search engines. This descriptive title is shown when there are hyperlinks which are not crawlable by search engines. */ failureTitle: 'Anchors do not have crawlable hyperlinks', /** Description of a Lighthouse audit that tells the user why hyperlinks should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ - description: 'Search engines use hyperlinks to crawl websites', + description: 'Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ - columnFailingAnchors: 'Failing Anchor Elements', + columnFailingAnchors: 'Uncrawlable Anchor Element', }; const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); @@ -40,12 +40,31 @@ class CrawlableAnchors extends Audit { * @return {LH.Audit.Product} */ static audit({AnchorElements: anchorElements}) { - const failingAnchors = anchorElements.filter(({rawHref, listeners = [], name = ''}) => { + const failingAnchors = anchorElements.filter(({ + rawHref, + hasClickHandler, + onclick = '', + name = '', + }) => { + onclick = onclick.replace( /\s/g, ''); + name = name.trim(); + rawHref = rawHref.replace( /\s/g, ''); + + const windowLocationRegExp = /window.location=/; + const windowOpenRegExp = /window.open\(/; + const javaScriptVoidRegExp = /javascript:void(\(|)0(\)|)/; + if (rawHref.startsWith('file:')) { return true; } - const hasClickHandler = listeners.some(({type}) => type === 'click'); + if (windowLocationRegExp.test(onclick)) { + return true; + } + + if (windowOpenRegExp.test(onclick)) { + return true; + } if (hasClickHandler || name.trim().length > 0) return; @@ -53,7 +72,7 @@ class CrawlableAnchors extends Audit { return true; } - if (rawHref.startsWith('javascript:void(0)')) { + if (javaScriptVoidRegExp.test(rawHref)) { return true; } }); @@ -66,11 +85,14 @@ class CrawlableAnchors extends Audit { }]; /** @type {LH.Audit.Details.Table['items']} */ - const itemsToDisplay = failingAnchors.map(node => { + const itemsToDisplay = failingAnchors.map(anchor => { return { node: { type: 'node', - snippet: node.outerHTML + node.text, + path: anchor.devtoolsNodePath || '', + selector: anchor.selector || '', + nodeLabel: anchor.nodeLabel || '', + snippet: anchor.outerHTML || '', }, }; }); diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index f17947bb26c8..de68690e7a88 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -48,6 +48,8 @@ function collectAnchorElements() { return { href: node.href, rawHref: node.getAttribute('href') || '', + onclick: node.getAttribute('onclick') || '', + hasClickHandler: false, name: node.name, text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, @@ -62,6 +64,8 @@ function collectAnchorElements() { return { href: resolveURLOrEmpty(node.href.baseVal), rawHref: node.getAttribute('href') || '', + onclick: node.getAttribute('onclick') || '', + hasClickHandler: false, text: node.textContent || '', rel: '', target: node.target.baseVal || '', @@ -79,20 +83,18 @@ function collectAnchorElements() { */ async function getEventListeners(driver, devtoolsNodePath) { const {nodeId} = await driver.sendCommand('DOM.pushNodeByPathToFrontend', { - path: devtoolsNodePath + path: devtoolsNodePath, }); const {object: {objectId = ''}} = await driver.sendCommand('DOM.resolveNode', { - nodeId + nodeId, }); const response = await driver.sendCommand('DOMDebugger.getEventListeners', { - objectId + objectId, }); - if (response.listeners.length > 0) { - return response; - } + return response.listeners; } class AnchorElements extends Gatherer { @@ -116,13 +118,16 @@ class AnchorElements extends Gatherer { const anchors = await driver.evaluateAsync(expression, {useIsolation: true}); await driver.sendCommand('DOM.enable'); - // DOM.getDocument is necessary for pushNodesByBackendIdsToFrontend to properly retrieve nodeIds. + // DOM.getDocument is necessary for pushNodesByBackendIdsToFrontend to properly retrieve nodeIds if the `DOM` domain was enabled before this gatherer, invoke it to be safe. await driver.sendCommand('DOM.getDocument', {depth: -1, pierce: true}); const anchorsWithEventListeners = anchors.map(async anchor => { + const eventListeners = await getEventListeners(driver, anchor.devtoolsNodePath); + const hasClickHandler = eventListeners.some(({type}) => type === 'click'); + return { ...anchor, - ...await getEventListeners(driver, anchor.devtoolsNodePath) - } + hasClickHandler, + }; }); const result = await Promise.all(anchorsWithEventListeners); diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 22e016a5fd06..8e5ba6b244c0 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -1008,10 +1008,10 @@ "message": "Document has a valid `rel=canonical`" }, "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { - "message": "Failing Anchor Elements" + "message": "Uncrawlable Anchor Element" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Search engines use hyperlinks to crawl websites" + "message": "Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { "message": "Anchors do not have crawlable hyperlinks" diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index 5b718841342a..af57a7966a34 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -1008,10 +1008,10 @@ "message": "D̂óĉúm̂én̂t́ ĥáŝ á v̂ál̂íd̂ `rel=canonical`" }, "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { - "message": "F̂áîĺîńĝ Án̂ćĥór̂ Él̂ém̂én̂t́ŝ" + "message": "Ûńĉŕâẃl̂áb̂ĺê Án̂ćĥór̂ Él̂ém̂én̂t́" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ" + "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { "message": "Âńĉh́ôŕŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́ŷṕêŕl̂ín̂ḱŝ" diff --git a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js index ba69f0f1ebe4..97794ed60c99 100644 --- a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -5,17 +5,23 @@ */ 'use strict'; -const CrawlableAnchorsAudit = require('../../../audits/seo/crawlable-anchors.js'); const assert = require('assert'); +const CrawlableAnchorsAudit = require('../../../audits/seo/crawlable-anchors.js'); /* eslint-env jest */ -function runAudit({rawHref, listeners, name = ''}) { +function runAudit({ + rawHref = '', + onclick = '', + name = '', + hasClickHandler = onclick.trim().length, +}) { const {score} = CrawlableAnchorsAudit.audit({ AnchorElements: [{ rawHref, name, - ...(listeners && listeners.length && {listeners}), + hasClickHandler, + onclick, }], }); @@ -24,52 +30,73 @@ function runAudit({rawHref, listeners, name = ''}) { describe('SEO: Crawlable anchors audit', () => { it('allows crawlable anchors', () => { - assert.equal(runAudit({rawHref:'#top'}), 1, 'hash fragment identifier'); - assert.equal(runAudit({rawHref:'mailto:name@example.com'}), 1, 'email link with a mailto URI'); - assert.equal(runAudit({rawHref:'https://example.com'}), 1, 'absolute HTTPs URL'); - assert.equal(runAudit({rawHref:'foo'}), 1, 'relative URL'); - assert.equal(runAudit({rawHref:'/foo'}), 1, 'relative URL'); - assert.equal(runAudit({rawHref:'#:~:text=string'}), 1, 'hyperlink with a text fragment'); - assert.equal(runAudit({rawHref:'ftp://myname@host.dom'}), 1, 'an FTP hyperlink'); - assert.equal(runAudit({rawHref:'http://172.217.20.78'}), 1, 'IP address based link'); - assert.equal(runAudit({rawHref:'//example.com'}), 1, 'protocol relative link'); - assert.equal(runAudit({rawHref:'?query=string'}), 1, 'relative link which specifies a query string'); - assert.equal(runAudit({rawHref:'tel:5555555'}), 1, 'email link with a tel URI'); - assert.equal(runAudit({rawHref:'#'}), 1, 'link with only a hash symbol'); - assert.equal(runAudit({rawHref:'', name: 'name'}), 1, 'link with a name attribute'); + assert.equal(runAudit({rawHref: '#top'}), 1, 'hash fragment identifier'); + assert.equal(runAudit({rawHref: 'mailto:name@example.com'}), 1, 'email link with a mailto URI'); + assert.equal(runAudit({rawHref: 'https://example.com'}), 1, 'absolute HTTPs URL'); + assert.equal(runAudit({rawHref: 'foo'}), 1, 'relative URL'); + assert.equal(runAudit({rawHref: '/foo'}), 1, 'relative URL'); + assert.equal(runAudit({rawHref: '#:~:text=string'}), 1, 'hyperlink with a text fragment'); + assert.equal(runAudit({rawHref: 'ftp://myname@host.dom'}), 1, 'an FTP hyperlink'); + assert.equal(runAudit({rawHref: 'http://172.217.20.78'}), 1, 'IP address based link'); + assert.equal(runAudit({rawHref: '//example.com'}), 1, 'protocol relative link'); + assert.equal(runAudit({rawHref: 'tel:5555555'}), 1, 'email link with a tel URI'); + assert.equal(runAudit({rawHref: '#'}), 1, 'link with only a hash symbol'); + assert.equal(runAudit({ + rawHref: '?query=string', + }), 1, 'relative link which specifies a query string'); + }); + + it('allows anchors which use a name attribute', () => { + assert.equal(runAudit({name: 'name'}), 1, 'link with a name attribute'); }); - it('allows certain anchors which use event listeners on themselves', () => { + it('allows anchors which use event listeners on themselves', () => { + assert.equal(runAudit({hasClickHandler: true}), 1, 'presence of a click handler is a pass'); + const auditResultJavaScriptURI = runAudit({ - rawHref:'javascript:void(0)', - listeners: [{type: 'click'}], + rawHref: 'javascript:void(0)', + hasClickHandler: true, }); - assert.equal(auditResultJavaScriptURI, 1, 'hyperlink with a `javascript:` URI'); + const assertionMessage = 'hyperlink with a `javascript:` URI and a click handler'; + assert.equal(auditResultJavaScriptURI, 1, assertionMessage); + }); - const auditResultEmptyQuotes = runAudit({ - rawHref:'', - listeners: [{type: 'click'}], - }); - assert.equal(auditResultEmptyQuotes, 1, 'link with empty quotes for the href attribute'); + it('disallows uncrawlable anchors', () => { + assert.equal(runAudit({}), 0, 'link with no meaningful attributes and no event handlers'); + assert.equal(runAudit({rawHref: 'file:///image.png'}), 0, 'hyperlink with a `file:` URI'); + assert.equal(runAudit({name: ' '}), 0, 'name attribute with only space characters'); + assert.equal(runAudit({rawHref: ' '}), 0, 'href attribute with only space characters'); + const assertionMessage = 'onclick attribute with only space characters'; + assert.equal(runAudit({rawHref: ' ', onclick: ' '}), 0, assertionMessage); }); - it('checks the validity of the listeners', () => { - const auditResultBadEvent = runAudit({ - rawHref:'', - listeners: [{type: 'no'}], - }); - assert.equal(auditResultBadEvent, 0, 'link with unsupported event listener'); + it('disallows javascript:void expressions in the onclick attribute', () => { + const javaScriptVoidVariations = [ + 'javascript:void(0)', + 'javascript: void(0)', + 'javascript : void(0)', + 'javascript : void ( 0 )', + 'javascript: void 0', + 'javascript:void 0', + ]; - const auditResultGoodEvent = runAudit({ - rawHref:'', - listeners: [{type: 'no'}, {type: 'click'}], - }); - assert.equal(auditResultGoodEvent, 1, 'link with one supported and one unsupported event listener'); + for (const javaScriptVoidVariation of javaScriptVoidVariations) { + assert.equal(runAudit({rawHref: javaScriptVoidVariation}), 0, 'javascript:void variations'); + } }); - it('disallows uncrawlable anchors', () => { - assert.equal(runAudit({rawHref:'javascript:void(0)'}), 0, 'hyperlink with a `javascript:` URI'); - assert.equal(runAudit({rawHref:''}), 0, 'link with empty quotes for the href attribute'); - assert.equal(runAudit({rawHref:'file:///image.png'}), 0, 'hyperlink with a `file:` URI'); + it('disallows window.location and window.open assignments in an onclick attribute', () => { + const onclickVariations = [ + 'window.location=', + 'window.location =', + 'window.open()', + `window.open('')`, + 'window.open(`http://example.com`)', + 'window.open ( )', + ]; + + for (const onclickVariation of onclickVariations) { + assert.equal(runAudit({onclick: onclickVariation}), 0, 'URL changing onclick strings'); + } }); }); diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index 5ee6049b6b54..dfed11f67ea0 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -463,7 +463,9 @@ "devicePixelRatio": 2.625 }, "WebAppManifest": null, - "InstallabilityErrors": {"errors": []}, + "InstallabilityErrors": { + "errors": [] + }, "MetaElements": [ { "name": "", @@ -1750,6 +1752,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link", "rel": "", @@ -1762,6 +1766,8 @@ { "href": "", "rawHref": "", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link", "rel": "", @@ -1774,6 +1780,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link", "rel": "nofollow", @@ -1786,6 +1794,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener and another unrelated rel attribute", "rel": "noopener nofollow", @@ -1798,6 +1808,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link that uses rel noreferrer and another unrelated rel attribute", "rel": "noreferrer nofollow", @@ -1810,6 +1822,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener", "rel": "noopener", @@ -1822,6 +1836,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link that uses rel noreferrer", "rel": "noreferrer", @@ -1834,6 +1850,8 @@ { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener and noreferrer", "rel": "noopener noreferrer", @@ -1844,8 +1862,10 @@ "outerHTML": "" }, { - "href": "http://localhost:51938/dobetterweb/doesnotexist", + "href": "http://localhost:63250/dobetterweb/doesnotexist", "rawHref": "./doesnotexist", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "internal link is ok", "rel": "", @@ -1858,6 +1878,8 @@ { "href": "javascript:void(0)", "rawHref": "javascript:void(0)", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "", "rel": "", @@ -1870,6 +1892,8 @@ { "href": "mailto:inbox@email.com", "rawHref": "mailto:inbox@email.com", + "onclick": "", + "hasClickHandler": false, "name": "", "text": "", "rel": "", @@ -2228,4 +2252,4 @@ }, "MainDocumentContent": "\n\n\n\n\n\nDoBetterWeb Mega Tester... Of Death\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n
\n

Do better web tester page

\n Hi there!\n\n \n \n Facebook\n \n \n \n
\n\n
touchmove section
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "SourceMaps": [] -} +} \ No newline at end of file diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index b6125f4e8cc0..d019b8732242 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3133,7 +3133,7 @@ "nodeLabel": "internal link is ok", "snippet": "
" }, - "href": "http://localhost:54106/dobetterweb/doesnotexist", + "href": "http://localhost:51938/dobetterweb/doesnotexist", "target": "_blank", "rel": "", "outerHTML": "" @@ -3551,7 +3551,7 @@ "crawlable-anchors": { "id": "crawlable-anchors", "title": "Anchors do not have crawlable hyperlinks", - "description": "Search engines use hyperlinks to crawl websites", + "description": "Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", "score": 0, "scoreDisplayMode": "binary", "details": { @@ -3560,19 +3560,25 @@ { "key": "node", "itemType": "node", - "text": "Failing Anchor Elements" + "text": "Uncrawlable Anchor Element" } ], "items": [ { "node": { "type": "node", - "snippet": "external link" + "path": "3,HTML,1,BODY,38,A", + "selector": "body > a", + "nodeLabel": "external link", + "snippet": "" } }, { "node": { "type": "node", + "path": "3,HTML,1,BODY,54,A", + "selector": "body > a", + "nodeLabel": "a", "snippet": "" } } diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index ab50d029a9ec..5b7b1ed12691 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -329,7 +329,8 @@ declare global { selector: string nodeLabel: string outerHTML: string - listeners?: Array + onclick: string + hasClickHandler: boolean } export interface Font { From 2e0a05c9b7b496235d138b15309a9fa3ef914a66 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Thu, 14 May 2020 00:45:33 +0100 Subject: [PATCH 06/15] Updates the same JSON --- lighthouse-core/test/results/sample_v2.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index d019b8732242..c3a4eaf0f83e 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3133,7 +3133,7 @@ "nodeLabel": "internal link is ok", "snippet": "" }, - "href": "http://localhost:51938/dobetterweb/doesnotexist", + "href": "http://localhost:63250/dobetterweb/doesnotexist", "target": "_blank", "rel": "", "outerHTML": "" From 5c0b9f4022a03e2f8f99bd12b4c5bb78d9992338 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 15 May 2020 00:03:01 +0100 Subject: [PATCH 07/15] Escape regexp wildcard, adds test cases, expose listeners array --- .../audits/seo/crawlable-anchors.js | 30 +++------ .../gather/gatherers/anchor-elements.js | 9 +-- .../test/audits/seo/crawlable-anchors-test.js | 67 +++++++++++++++---- .../test/results/artifacts/artifacts.json | 46 ++++++------- types/artifacts.d.ts | 4 +- 5 files changed, 92 insertions(+), 64 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 1ed9334c1ea4..37b922cec9c3 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -42,7 +42,7 @@ class CrawlableAnchors extends Audit { static audit({AnchorElements: anchorElements}) { const failingAnchors = anchorElements.filter(({ rawHref, - hasClickHandler, + listeners = [], onclick = '', name = '', }) => { @@ -50,31 +50,19 @@ class CrawlableAnchors extends Audit { name = name.trim(); rawHref = rawHref.replace( /\s/g, ''); - const windowLocationRegExp = /window.location=/; - const windowOpenRegExp = /window.open\(/; + const windowLocationRegExp = /window\.location=/; + const windowOpenRegExp = /window\.open\(/; const javaScriptVoidRegExp = /javascript:void(\(|)0(\)|)/; - if (rawHref.startsWith('file:')) { - return true; - } - - if (windowLocationRegExp.test(onclick)) { - return true; - } - - if (windowOpenRegExp.test(onclick)) { - return true; - } + if (rawHref.startsWith('file:')) return true; + if (windowLocationRegExp.test(onclick)) return true; + if (windowOpenRegExp.test(onclick)) return true; + const hasClickHandler = listeners.some(({type}) => type === 'click'); if (hasClickHandler || name.trim().length > 0) return; - if (rawHref === '') { - return true; - } - - if (javaScriptVoidRegExp.test(rawHref)) { - return true; - } + if (rawHref === '') return true; + if (javaScriptVoidRegExp.test(rawHref)) return true; }); /** @type {LH.Audit.Details.Table['headings']} */ diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index de68690e7a88..3bf9b8c3ff0c 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -49,7 +49,6 @@ function collectAnchorElements() { href: node.href, rawHref: node.getAttribute('href') || '', onclick: node.getAttribute('onclick') || '', - hasClickHandler: false, name: node.name, text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, @@ -65,7 +64,6 @@ function collectAnchorElements() { href: resolveURLOrEmpty(node.href.baseVal), rawHref: node.getAttribute('href') || '', onclick: node.getAttribute('onclick') || '', - hasClickHandler: false, text: node.textContent || '', rel: '', target: node.target.baseVal || '', @@ -94,7 +92,7 @@ async function getEventListeners(driver, devtoolsNodePath) { objectId, }); - return response.listeners; + return response.listeners.map(({type}) => ({type})); } class AnchorElements extends Gatherer { @@ -121,12 +119,11 @@ class AnchorElements extends Gatherer { // DOM.getDocument is necessary for pushNodesByBackendIdsToFrontend to properly retrieve nodeIds if the `DOM` domain was enabled before this gatherer, invoke it to be safe. await driver.sendCommand('DOM.getDocument', {depth: -1, pierce: true}); const anchorsWithEventListeners = anchors.map(async anchor => { - const eventListeners = await getEventListeners(driver, anchor.devtoolsNodePath); - const hasClickHandler = eventListeners.some(({type}) => type === 'click'); + const listeners = await getEventListeners(driver, anchor.devtoolsNodePath); return { ...anchor, - hasClickHandler, + listeners, }; }); diff --git a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js index 97794ed60c99..bdf55eabc844 100644 --- a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -14,13 +14,13 @@ function runAudit({ rawHref = '', onclick = '', name = '', - hasClickHandler = onclick.trim().length, + listeners = onclick.trim().length ? [{type: 'click'}] : [], }) { const {score} = CrawlableAnchorsAudit.audit({ AnchorElements: [{ rawHref, name, - hasClickHandler, + listeners, onclick, }], }); @@ -50,15 +50,28 @@ describe('SEO: Crawlable anchors audit', () => { assert.equal(runAudit({name: 'name'}), 1, 'link with a name attribute'); }); - it('allows anchors which use event listeners on themselves', () => { - assert.equal(runAudit({hasClickHandler: true}), 1, 'presence of a click handler is a pass'); + it('handles anchor elements which use event listeners', () => { + const auditResultClickPresent = runAudit({ + listeners: [{type: 'click'}], + }); + assert.equal(auditResultClickPresent, 1, 'presence of a click handler is a pass'); const auditResultJavaScriptURI = runAudit({ rawHref: 'javascript:void(0)', - hasClickHandler: true, + listeners: [{type: 'click'}], }); const assertionMessage = 'hyperlink with a `javascript:` URI and a click handler'; assert.equal(auditResultJavaScriptURI, 1, assertionMessage); + + const auditResultNonClickListener = runAudit({ + listeners: [{type: 'nope'}], + }); + assert.equal(auditResultNonClickListener, 0, 'no click event is a fail'); + + const auditResultMixtureOfListeners = runAudit({ + listeners: [{type: 'nope'}, {type: 'another'}, {type: 'click'}], + }); + assert.equal(auditResultMixtureOfListeners, 1, 'at least one click listener is a pass'); }); it('disallows uncrawlable anchors', () => { @@ -70,33 +83,61 @@ describe('SEO: Crawlable anchors audit', () => { assert.equal(runAudit({rawHref: ' ', onclick: ' '}), 0, assertionMessage); }); - it('disallows javascript:void expressions in the onclick attribute', () => { - const javaScriptVoidVariations = [ + it('handles javascript:void expressions in the onclick attribute', () => { + const expectedAuditFailures = [ 'javascript:void(0)', 'javascript: void(0)', 'javascript : void(0)', 'javascript : void ( 0 )', 'javascript: void 0', 'javascript:void 0', + // The audit logic removes all whitespace from the string and considers this a fail + 'javascript:void0', ]; - for (const javaScriptVoidVariation of javaScriptVoidVariations) { - assert.equal(runAudit({rawHref: javaScriptVoidVariation}), 0, 'javascript:void variations'); + for (const javaScriptVoidVariation of expectedAuditFailures) { + const auditResult = runAudit({rawHref: javaScriptVoidVariation}); + assert.equal(auditResult, 0, 'javascript:void failing variations'); + } + + const expectedAuditPasses = [ + 'javascript:void', + 'javascript:void()', + 'javascript:0', + ]; + + for (const javaScriptVoidVariation of expectedAuditPasses) { + const auditResult = runAudit({rawHref: javaScriptVoidVariation}); + assert.equal(auditResult, 1, 'javascript:void passing variations'); } }); - it('disallows window.location and window.open assignments in an onclick attribute', () => { - const onclickVariations = [ + it('handles window.location and window.open assignments in an onclick attribute', () => { + const expectedAuditFailures = [ 'window.location=', 'window.location =', 'window.open()', `window.open('')`, 'window.open(`http://example.com`)', 'window.open ( )', + `window.open('foo', 'name', 'resizable)`, + ]; + + for (const onclickVariation of expectedAuditFailures) { + const auditResult = runAudit({onclick: onclickVariation}); + assert.equal(auditResult, 0, 'URL changing onclick strings'); + } + + const expectedAuditPasses = [ + 'windowAlocation', + 'window.location.href', + 'window.Location =', + 'windowLopen()', ]; - for (const onclickVariation of onclickVariations) { - assert.equal(runAudit({onclick: onclickVariation}), 0, 'URL changing onclick strings'); + for (const onclickVariation of expectedAuditPasses) { + const auditResult = runAudit({onclick: onclickVariation}); + assert.equal(auditResult, 1, 'onclick strings which do not change the URL'); } }); }); diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index dfed11f67ea0..bbe7f58ea476 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -1753,7 +1753,6 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link", "rel": "", @@ -1761,13 +1760,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,36,A", "selector": "body > a", "nodeLabel": "external link", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "", "rawHref": "", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link", "rel": "", @@ -1775,13 +1774,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,38,A", "selector": "body > a", "nodeLabel": "external link", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link", "rel": "nofollow", @@ -1789,13 +1788,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,40,A", "selector": "body > a", "nodeLabel": "external link", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener and another unrelated rel attribute", "rel": "noopener nofollow", @@ -1803,13 +1802,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,42,A", "selector": "body > a", "nodeLabel": "external link that uses rel noopener and another unrelated rel attribute", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link that uses rel noreferrer and another unrelated rel attribute", "rel": "noreferrer nofollow", @@ -1817,13 +1816,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,44,A", "selector": "body > a", "nodeLabel": "external link that uses rel noreferrer and another unrelated rel attribute", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener", "rel": "noopener", @@ -1831,13 +1830,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,46,A", "selector": "body > a", "nodeLabel": "external link that uses rel noopener", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link that uses rel noreferrer", "rel": "noreferrer", @@ -1845,13 +1844,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,48,A", "selector": "body > a", "nodeLabel": "external link that uses rel noreferrer", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", - "hasClickHandler": false, "name": "", "text": "external link that uses rel noopener and noreferrer", "rel": "noopener noreferrer", @@ -1859,13 +1858,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,50,A", "selector": "body > a", "nodeLabel": "external link that uses rel noopener and noreferrer", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { - "href": "http://localhost:63250/dobetterweb/doesnotexist", + "href": "http://localhost:57433/dobetterweb/doesnotexist", "rawHref": "./doesnotexist", "onclick": "", - "hasClickHandler": false, "name": "", "text": "internal link is ok", "rel": "", @@ -1873,13 +1872,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,52,A", "selector": "body > a", "nodeLabel": "internal link is ok", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "javascript:void(0)", "rawHref": "javascript:void(0)", "onclick": "", - "hasClickHandler": false, "name": "", "text": "", "rel": "", @@ -1887,13 +1886,13 @@ "devtoolsNodePath": "3,HTML,1,BODY,54,A", "selector": "body > a", "nodeLabel": "a", - "outerHTML": "" + "outerHTML": "", + "listeners": [] }, { "href": "mailto:inbox@email.com", "rawHref": "mailto:inbox@email.com", "onclick": "", - "hasClickHandler": false, "name": "", "text": "", "rel": "", @@ -1901,7 +1900,8 @@ "devtoolsNodePath": "3,HTML,1,BODY,56,A", "selector": "body > a", "nodeLabel": "a", - "outerHTML": "" + "outerHTML": "", + "listeners": [] } ], "AppCacheManifest": "clock.appcache", diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index 5b7b1ed12691..372116602505 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -330,7 +330,9 @@ declare global { nodeLabel: string outerHTML: string onclick: string - hasClickHandler: boolean + listeners?: Array<{ + type: Crdp.DOMDebugger.EventListener['type'] + }> } export interface Font { From 7e4cbafcaf68b987819f1e3402a80d0ce5683db3 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 15 May 2020 00:05:47 +0100 Subject: [PATCH 08/15] Updates sample JSON --- lighthouse-core/test/results/sample_v2.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index c3a4eaf0f83e..3991ba97424f 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3133,7 +3133,7 @@ "nodeLabel": "internal link is ok", "snippet": "" }, - "href": "http://localhost:63250/dobetterweb/doesnotexist", + "href": "http://localhost:57433/dobetterweb/doesnotexist", "target": "_blank", "rel": "", "outerHTML": "" From d2095db1bd75423cc0eba3fb85472c5b27e5e14b Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 15 May 2020 16:48:09 +0100 Subject: [PATCH 09/15] Updates messages for the crawlable links audit --- lighthouse-core/audits/seo/crawlable-anchors.js | 12 ++++++------ lighthouse-core/lib/i18n/locales/en-US.json | 6 +++--- lighthouse-core/lib/i18n/locales/en-XL.json | 6 +++--- lighthouse-core/test/results/sample_v2.json | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 37b922cec9c3..1e253796d151 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -9,12 +9,12 @@ const Audit = require('../audit.js'); const i18n = require('../../lib/i18n/i18n.js'); const UIStrings = { - /** Title of a Lighthouse audit that provides detail on whether anchors have hyperlinks which can be crawled by search engines. This descriptive title is shown when all hyperlinks on the page are crawlable. */ - title: 'Anchors have crawlable hyperlinks', - /** Descriptive title of a Lighthouse audit that provides detail on whether anchors have hyperlinks which can be crawled by search engines. This descriptive title is shown when there are hyperlinks which are not crawlable by search engines. */ - failureTitle: 'Anchors do not have crawlable hyperlinks', - /** Description of a Lighthouse audit that tells the user why hyperlinks should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ - description: 'Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', + /** Title of a Lighthouse audit that provides detail on whether links have potentially-crawlable href attributes. This descriptive title is shown when all links on the page are potentially-crawlable. */ + title: 'Links have potentially-crawlable href attributes', + /** Descriptive title of a Lighthouse audit that provides detail on whether links have potentially-crawlable href attributes. This descriptive title is shown when there are href attributes which are not crawlable by search engines. */ + failureTitle: 'Links do not have crawlable href attributes', + /** Description of a Lighthouse audit that tells the user why href attributes on links should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ + description: 'Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ columnFailingAnchors: 'Uncrawlable Anchor Element', }; diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 8e5ba6b244c0..8427afe8ee44 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -1011,13 +1011,13 @@ "message": "Uncrawlable Anchor Element" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" + "message": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { - "message": "Anchors do not have crawlable hyperlinks" + "message": "Links do not have crawlable href attributes" }, "lighthouse-core/audits/seo/crawlable-anchors.js | title": { - "message": "Anchors have crawlable hyperlinks" + "message": "Links have potentially-crawlable href attributes" }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "Font sizes less than 12px are too small to be legible and require mobile visitors to “pinch to zoom” in order to read. Strive to have >60% of page text ≥12px. [Learn more](https://web.dev/font-size)." diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index af57a7966a34..3799bd620150 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -1011,13 +1011,13 @@ "message": "Ûńĉŕâẃl̂áb̂ĺê Án̂ćĥór̂ Él̂ém̂én̂t́" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Ŝéâŕĉh́ êńĝín̂éŝ úŝé ĥýp̂ér̂ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" + "message": "Ŝéâŕĉh́ êńĝín̂éŝ ḿâý ûśê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ ón̂ ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { - "message": "Âńĉh́ôŕŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́ŷṕêŕl̂ín̂ḱŝ" + "message": "L̂ín̂ḱŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ" }, "lighthouse-core/audits/seo/crawlable-anchors.js | title": { - "message": "Âńĉh́ôŕŝ h́âv́ê ćr̂áŵĺâb́l̂é ĥýp̂ér̂ĺîńk̂ś" + "message": "L̂ín̂ḱŝ h́âv́ê ṕôt́êńt̂íâĺl̂ý-ĉŕâẃl̂áb̂ĺê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ" }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "F̂ón̂t́ ŝíẑéŝ ĺêśŝ t́ĥán̂ 12ṕx̂ ár̂é t̂óô śm̂ál̂ĺ t̂ó b̂é l̂éĝíb̂ĺê án̂d́ r̂éq̂úîŕê ḿôb́îĺê v́îśît́ôŕŝ t́ô “ṕîńĉh́ t̂ó ẑóôḿ” îń ôŕd̂ér̂ t́ô ŕêád̂. Śt̂ŕîv́ê t́ô h́âv́ê >60% óf̂ ṕâǵê t́êx́t̂ ≥12ṕx̂. [Ĺêár̂ń m̂ór̂é](https://web.dev/font-size)." diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index 3991ba97424f..b80f37ff70cd 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3550,8 +3550,8 @@ }, "crawlable-anchors": { "id": "crawlable-anchors", - "title": "Anchors do not have crawlable hyperlinks", - "description": "Search engines use hyperlinks to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", + "title": "Links do not have crawlable href attributes", + "description": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", "score": 0, "scoreDisplayMode": "binary", "details": { From 8ea81877228ff1ac0ff3836eb5e30f0a567c31f4 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 15 May 2020 16:58:30 +0100 Subject: [PATCH 10/15] Updates messages for the crawlable links audit --- lighthouse-core/audits/seo/crawlable-anchors.js | 4 ++-- lighthouse-core/lib/i18n/locales/en-US.json | 4 ++-- lighthouse-core/lib/i18n/locales/en-XL.json | 4 ++-- lighthouse-core/test/results/sample_v2.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 1e253796d151..927e296efbbe 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -16,7 +16,7 @@ const UIStrings = { /** Description of a Lighthouse audit that tells the user why href attributes on links should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ description: 'Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ - columnFailingAnchors: 'Uncrawlable Anchor Element', + columnFailingLink: 'Uncrawlable Link', }; const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); @@ -69,7 +69,7 @@ class CrawlableAnchors extends Audit { const headings = [{ key: 'node', itemType: 'node', - text: str_(UIStrings.columnFailingAnchors), + text: str_(UIStrings.columnFailingLink), }]; /** @type {LH.Audit.Details.Table['items']} */ diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 8427afe8ee44..219bccbc5e61 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -1007,8 +1007,8 @@ "lighthouse-core/audits/seo/canonical.js | title": { "message": "Document has a valid `rel=canonical`" }, - "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { - "message": "Uncrawlable Anchor Element" + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingLink": { + "message": "Uncrawlable Link" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { "message": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index 3799bd620150..1df9b8b78368 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -1007,8 +1007,8 @@ "lighthouse-core/audits/seo/canonical.js | title": { "message": "D̂óĉúm̂én̂t́ ĥáŝ á v̂ál̂íd̂ `rel=canonical`" }, - "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": { - "message": "Ûńĉŕâẃl̂áb̂ĺê Án̂ćĥór̂ Él̂ém̂én̂t́" + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingLink": { + "message": "Ûńĉŕâẃl̂áb̂ĺê Ĺîńk̂" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { "message": "Ŝéâŕĉh́ êńĝín̂éŝ ḿâý ûśê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ ón̂ ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index b80f37ff70cd..ea462917f41c 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3560,7 +3560,7 @@ { "key": "node", "itemType": "node", - "text": "Uncrawlable Anchor Element" + "text": "Uncrawlable Link" } ], "items": [ @@ -7019,7 +7019,7 @@ "lighthouse-core/audits/seo/crawlable-anchors.js | description": [ "audits[crawlable-anchors].description" ], - "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingAnchors": [ + "lighthouse-core/audits/seo/crawlable-anchors.js | columnFailingLink": [ "audits[crawlable-anchors].details.headings[0].text" ], "lighthouse-core/audits/seo/is-crawlable.js | title": [ From e23266cf3ff054398a65dcaebed82b895ebd9ab9 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Fri, 15 May 2020 17:06:58 +0100 Subject: [PATCH 11/15] Updates messages for the crawlable links audit --- lighthouse-core/audits/seo/crawlable-anchors.js | 4 ++-- lighthouse-core/lib/i18n/locales/en-US.json | 4 ++-- lighthouse-core/lib/i18n/locales/en-XL.json | 4 ++-- lighthouse-core/test/results/sample_v2.json | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 927e296efbbe..de0a704a494b 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -10,9 +10,9 @@ const i18n = require('../../lib/i18n/i18n.js'); const UIStrings = { /** Title of a Lighthouse audit that provides detail on whether links have potentially-crawlable href attributes. This descriptive title is shown when all links on the page are potentially-crawlable. */ - title: 'Links have potentially-crawlable href attributes', + title: 'Links are crawlable', /** Descriptive title of a Lighthouse audit that provides detail on whether links have potentially-crawlable href attributes. This descriptive title is shown when there are href attributes which are not crawlable by search engines. */ - failureTitle: 'Links do not have crawlable href attributes', + failureTitle: 'Links are not crawlable', /** Description of a Lighthouse audit that tells the user why href attributes on links should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ description: 'Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index 219bccbc5e61..ce64f4e252ba 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -1014,10 +1014,10 @@ "message": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { - "message": "Links do not have crawlable href attributes" + "message": "Links are not crawlable" }, "lighthouse-core/audits/seo/crawlable-anchors.js | title": { - "message": "Links have potentially-crawlable href attributes" + "message": "Links are crawlable" }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "Font sizes less than 12px are too small to be legible and require mobile visitors to “pinch to zoom” in order to read. Strive to have >60% of page text ≥12px. [Learn more](https://web.dev/font-size)." diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index 1df9b8b78368..08172a0aaab3 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -1014,10 +1014,10 @@ "message": "Ŝéâŕĉh́ êńĝín̂éŝ ḿâý ûśê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ ón̂ ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { - "message": "L̂ín̂ḱŝ d́ô ńôt́ ĥáv̂é ĉŕâẃl̂áb̂ĺê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ" + "message": "L̂ín̂ḱŝ ár̂é n̂ót̂ ćr̂áŵĺâb́l̂é" }, "lighthouse-core/audits/seo/crawlable-anchors.js | title": { - "message": "L̂ín̂ḱŝ h́âv́ê ṕôt́êńt̂íâĺl̂ý-ĉŕâẃl̂áb̂ĺê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ" + "message": "L̂ín̂ḱŝ ár̂é ĉŕâẃl̂áb̂ĺê" }, "lighthouse-core/audits/seo/font-size.js | description": { "message": "F̂ón̂t́ ŝíẑéŝ ĺêśŝ t́ĥán̂ 12ṕx̂ ár̂é t̂óô śm̂ál̂ĺ t̂ó b̂é l̂éĝíb̂ĺê án̂d́ r̂éq̂úîŕê ḿôb́îĺê v́îśît́ôŕŝ t́ô “ṕîńĉh́ t̂ó ẑóôḿ” îń ôŕd̂ér̂ t́ô ŕêád̂. Śt̂ŕîv́ê t́ô h́âv́ê >60% óf̂ ṕâǵê t́êx́t̂ ≥12ṕx̂. [Ĺêár̂ń m̂ór̂é](https://web.dev/font-size)." diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index ea462917f41c..b0369bbba71e 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3550,7 +3550,7 @@ }, "crawlable-anchors": { "id": "crawlable-anchors", - "title": "Links do not have crawlable href attributes", + "title": "Links are not crawlable", "description": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", "score": 0, "scoreDisplayMode": "binary", From 1787102d1c7cfb184789490c5110f3b05347af9b Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Mon, 18 May 2020 23:25:54 +0100 Subject: [PATCH 12/15] Smoke tests for the crawlable anchors audit --- .../test/fixtures/seo/seo-failure-cases.html | 18 ++++++++++++++++++ .../test/fixtures/seo/seo-tester.html | 14 +++++++++++++- .../test-definitions/seo/expectations.js | 14 ++++++++++++++ .../audits/seo/crawlable-anchors.js | 2 +- .../gather/gatherers/anchor-elements.js | 10 ++++++++-- lighthouse-core/lib/i18n/locales/en-US.json | 2 +- lighthouse-core/lib/i18n/locales/en-XL.json | 2 +- .../test/results/artifacts/artifacts.json | 2 +- lighthouse-core/test/results/sample_v2.json | 2 +- 9 files changed, 58 insertions(+), 8 deletions(-) diff --git a/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html b/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html index f4640f0f1bd1..567bb20483ff 100644 --- a/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html +++ b/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html @@ -35,6 +35,24 @@

Anchor text

+ + Some link + + + Some link + + + Some link + + + + + + Your browser does not support the applet tag. diff --git a/lighthouse-cli/test/fixtures/seo/seo-tester.html b/lighthouse-cli/test/fixtures/seo/seo-tester.html index 1e381a96bc4e..5d4adf7a4a15 100644 --- a/lighthouse-cli/test/fixtures/seo/seo-tester.html +++ b/lighthouse-cli/test/fixtures/seo/seo-tester.html @@ -47,7 +47,7 @@

Anchor text

descriptive link click this click this - click this + click this

Small text

@@ -66,5 +66,17 @@
2
+ + + Some link + + + Some link + + + + Some link diff --git a/lighthouse-cli/test/smokehouse/test-definitions/seo/expectations.js b/lighthouse-cli/test/smokehouse/test-definitions/seo/expectations.js index 40921a259e00..404c2f58b071 100644 --- a/lighthouse-cli/test/smokehouse/test-definitions/seo/expectations.js +++ b/lighthouse-cli/test/smokehouse/test-definitions/seo/expectations.js @@ -199,6 +199,9 @@ const expectations = [ ], }, }, + 'crawlable-anchors': { + score: 1, + }, 'link-text': { score: 1, }, @@ -242,6 +245,14 @@ const expectations = [ explanation: 'Text is illegible because there\'s no viewport meta tag optimized for mobile screens.', }, + 'crawlable-anchors': { + score: 0, + details: { + items: { + length: 4, + }, + }, + }, 'link-text': { score: 0, displayValue: '4 links found', @@ -308,6 +319,9 @@ const expectations = [ 'font-size': { score: null, }, + 'crawlable-anchors': { + score: null, + }, 'link-text': { score: null, }, diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index de0a704a494b..14da997fb3fe 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -14,7 +14,7 @@ const UIStrings = { /** Descriptive title of a Lighthouse audit that provides detail on whether links have potentially-crawlable href attributes. This descriptive title is shown when there are href attributes which are not crawlable by search engines. */ failureTitle: 'Links are not crawlable', /** Description of a Lighthouse audit that tells the user why href attributes on links should be crawlable. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ - description: 'Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', + description: 'Search engines may use `href` attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)', /** Label for a column in a data table; entries will be the HTML anchor elements that failed the audit. Anchors are DOM elements that are links. */ columnFailingLink: 'Uncrawlable Link', }; diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index 3bf9b8c3ff0c..d1a3ed17fbd5 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -30,6 +30,12 @@ function collectAnchorElements() { } }; + /** @param {HTMLAnchorElement|SVGAElement} node */ + function getTruncatedOnclick(node) { + const onclick = node.getAttribute('onclick') || ''; + return onclick.slice(0, 1024); + } + /** @type {Array} */ // @ts-ignore - put into scope via stringification const anchorElements = getElementsInDocument('a'); // eslint-disable-line no-undef @@ -48,7 +54,7 @@ function collectAnchorElements() { return { href: node.href, rawHref: node.getAttribute('href') || '', - onclick: node.getAttribute('onclick') || '', + onclick: getTruncatedOnclick(node), name: node.name, text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, @@ -63,7 +69,7 @@ function collectAnchorElements() { return { href: resolveURLOrEmpty(node.href.baseVal), rawHref: node.getAttribute('href') || '', - onclick: node.getAttribute('onclick') || '', + onclick: getTruncatedOnclick(node), text: node.textContent || '', rel: '', target: node.target.baseVal || '', diff --git a/lighthouse-core/lib/i18n/locales/en-US.json b/lighthouse-core/lib/i18n/locales/en-US.json index ce64f4e252ba..73c75c7ce79a 100644 --- a/lighthouse-core/lib/i18n/locales/en-US.json +++ b/lighthouse-core/lib/i18n/locales/en-US.json @@ -1011,7 +1011,7 @@ "message": "Uncrawlable Link" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" + "message": "Search engines may use `href` attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { "message": "Links are not crawlable" diff --git a/lighthouse-core/lib/i18n/locales/en-XL.json b/lighthouse-core/lib/i18n/locales/en-XL.json index 08172a0aaab3..4c1745fa168f 100644 --- a/lighthouse-core/lib/i18n/locales/en-XL.json +++ b/lighthouse-core/lib/i18n/locales/en-XL.json @@ -1011,7 +1011,7 @@ "message": "Ûńĉŕâẃl̂áb̂ĺê Ĺîńk̂" }, "lighthouse-core/audits/seo/crawlable-anchors.js | description": { - "message": "Ŝéâŕĉh́ êńĝín̂éŝ ḿâý ûśê h́r̂éf̂ át̂t́r̂íb̂út̂éŝ ón̂ ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" + "message": "Ŝéâŕĉh́ êńĝín̂éŝ ḿâý ûśê `href` át̂t́r̂íb̂út̂éŝ ón̂ ĺîńk̂ś t̂ó ĉŕâẃl̂ ẃêb́ŝít̂éŝ. Én̂śûŕê t́ĥát̂ t́ĥé `href` ât́t̂ŕîb́ût́ê óf̂ án̂ćĥór̂ él̂ém̂én̂t́ŝ ĺîńk̂ś t̂ó âń âṕp̂ŕôṕr̂íât́ê d́êśt̂ín̂át̂íôń, ŝó m̂ór̂é p̂áĝéŝ óf̂ t́ĥé ŝít̂é ĉán̂ b́ê d́îśĉóv̂ér̂éd̂. [Ĺêár̂ń M̂ór̂é](https://support.google.com/webmasters/answer/9112205)" }, "lighthouse-core/audits/seo/crawlable-anchors.js | failureTitle": { "message": "L̂ín̂ḱŝ ár̂é n̂ót̂ ćr̂áŵĺâb́l̂é" diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index bbe7f58ea476..b7a89c32fb2c 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -1862,7 +1862,7 @@ "listeners": [] }, { - "href": "http://localhost:57433/dobetterweb/doesnotexist", + "href": "http://localhost:56001/dobetterweb/doesnotexist", "rawHref": "./doesnotexist", "onclick": "", "name": "", diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index b0369bbba71e..ff13f6e4a4c9 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3551,7 +3551,7 @@ "crawlable-anchors": { "id": "crawlable-anchors", "title": "Links are not crawlable", - "description": "Search engines may use href attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", + "description": "Search engines may use `href` attributes on links to crawl websites. Ensure that the `href` attribute of anchor elements links to an appropriate destination, so more pages of the site can be discovered. [Learn More](https://support.google.com/webmasters/answer/9112205)", "score": 0, "scoreDisplayMode": "binary", "details": { From b69d0fa3025ce88c7e1a84e5529ab8358700cc66 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Mon, 18 May 2020 23:28:18 +0100 Subject: [PATCH 13/15] Updates sample json --- lighthouse-core/test/results/sample_v2.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index ff13f6e4a4c9..ee5bba28bc98 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3133,7 +3133,7 @@ "nodeLabel": "internal link is ok", "snippet": "" }, - "href": "http://localhost:57433/dobetterweb/doesnotexist", + "href": "http://localhost:56001/dobetterweb/doesnotexist", "target": "_blank", "rel": "", "outerHTML": "" From 70a9a9c8a46d5af71bf73ac0db307a2e36f81e27 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Mon, 18 May 2020 23:39:00 +0100 Subject: [PATCH 14/15] More helpful messages in unit test --- lighthouse-core/test/audits/seo/crawlable-anchors-test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js index bdf55eabc844..6727880264d1 100644 --- a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -97,7 +97,7 @@ describe('SEO: Crawlable anchors audit', () => { for (const javaScriptVoidVariation of expectedAuditFailures) { const auditResult = runAudit({rawHref: javaScriptVoidVariation}); - assert.equal(auditResult, 0, 'javascript:void failing variations'); + assert.equal(auditResult, 0, `'${javaScriptVoidVariation}' should fail the audit`); } const expectedAuditPasses = [ @@ -108,7 +108,7 @@ describe('SEO: Crawlable anchors audit', () => { for (const javaScriptVoidVariation of expectedAuditPasses) { const auditResult = runAudit({rawHref: javaScriptVoidVariation}); - assert.equal(auditResult, 1, 'javascript:void passing variations'); + assert.equal(auditResult, 1, `'${javaScriptVoidVariation}' should pass the audit`); } }); @@ -125,7 +125,7 @@ describe('SEO: Crawlable anchors audit', () => { for (const onclickVariation of expectedAuditFailures) { const auditResult = runAudit({onclick: onclickVariation}); - assert.equal(auditResult, 0, 'URL changing onclick strings'); + assert.equal(auditResult, 0, `'${onclickVariation}' should fail the audit`); } const expectedAuditPasses = [ @@ -137,7 +137,7 @@ describe('SEO: Crawlable anchors audit', () => { for (const onclickVariation of expectedAuditPasses) { const auditResult = runAudit({onclick: onclickVariation}); - assert.equal(auditResult, 1, 'onclick strings which do not change the URL'); + assert.equal(auditResult, 1, `'${onclickVariation}' should pass the audit`); } }); }); From db26d769085a1058ac294d00c0208118882dea04 Mon Sep 17 00:00:00 2001 From: Umar Hansa Date: Tue, 19 May 2020 23:49:17 +0100 Subject: [PATCH 15/15] Pass the audit with a valid role attribute value --- lighthouse-core/audits/seo/crawlable-anchors.js | 8 ++++++-- lighthouse-core/gather/gatherers/anchor-elements.js | 2 ++ .../test/audits/seo/crawlable-anchors-test.js | 12 ++++++++++++ .../test/results/artifacts/artifacts.json | 13 ++++++++++++- lighthouse-core/test/results/sample_v2.json | 2 +- types/artifacts.d.ts | 1 + 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lighthouse-core/audits/seo/crawlable-anchors.js b/lighthouse-core/audits/seo/crawlable-anchors.js index 14da997fb3fe..b97af5b8369e 100644 --- a/lighthouse-core/audits/seo/crawlable-anchors.js +++ b/lighthouse-core/audits/seo/crawlable-anchors.js @@ -45,10 +45,14 @@ class CrawlableAnchors extends Audit { listeners = [], onclick = '', name = '', + role = '', }) => { onclick = onclick.replace( /\s/g, ''); - name = name.trim(); rawHref = rawHref.replace( /\s/g, ''); + name = name.trim(); + role = role.trim(); + + if (role.length > 0) return; const windowLocationRegExp = /window\.location=/; const windowOpenRegExp = /window\.open\(/; @@ -59,7 +63,7 @@ class CrawlableAnchors extends Audit { if (windowOpenRegExp.test(onclick)) return true; const hasClickHandler = listeners.some(({type}) => type === 'click'); - if (hasClickHandler || name.trim().length > 0) return; + if (hasClickHandler || name.length > 0) return; if (rawHref === '') return true; if (javaScriptVoidRegExp.test(rawHref)) return true; diff --git a/lighthouse-core/gather/gatherers/anchor-elements.js b/lighthouse-core/gather/gatherers/anchor-elements.js index d1a3ed17fbd5..b6f8e7874263 100644 --- a/lighthouse-core/gather/gatherers/anchor-elements.js +++ b/lighthouse-core/gather/gatherers/anchor-elements.js @@ -55,6 +55,7 @@ function collectAnchorElements() { href: node.href, rawHref: node.getAttribute('href') || '', onclick: getTruncatedOnclick(node), + role: node.getAttribute('role') || '', name: node.name, text: node.innerText, // we don't want to return hidden text, so use innerText rel: node.rel, @@ -70,6 +71,7 @@ function collectAnchorElements() { href: resolveURLOrEmpty(node.href.baseVal), rawHref: node.getAttribute('href') || '', onclick: getTruncatedOnclick(node), + role: node.getAttribute('role') || '', text: node.textContent || '', rel: '', target: node.target.baseVal || '', diff --git a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js index 6727880264d1..e55972d975b0 100644 --- a/lighthouse-core/test/audits/seo/crawlable-anchors-test.js +++ b/lighthouse-core/test/audits/seo/crawlable-anchors-test.js @@ -12,6 +12,7 @@ const CrawlableAnchorsAudit = require('../../../audits/seo/crawlable-anchors.js' function runAudit({ rawHref = '', + role = '', onclick = '', name = '', listeners = onclick.trim().length ? [{type: 'click'}] : [], @@ -22,6 +23,7 @@ function runAudit({ name, listeners, onclick, + role, }], }); @@ -50,6 +52,16 @@ describe('SEO: Crawlable anchors audit', () => { assert.equal(runAudit({name: 'name'}), 1, 'link with a name attribute'); }); + it('handles anchors with a role attribute', () => { + const auditResult = runAudit({ + role: 'some-role', + rawHref: 'javascript:void(0)', + }); + assert.equal(auditResult, 1, 'Href value has no effect when a role is present'); + assert.equal(runAudit({role: 'a'}), 1, 'Using a role attribute value is an immediate pass'); + assert.equal(runAudit({role: ' '}), 0, 'A role value of a space character fails the audit'); + }); + it('handles anchor elements which use event listeners', () => { const auditResultClickPresent = runAudit({ listeners: [{type: 'click'}], diff --git a/lighthouse-core/test/results/artifacts/artifacts.json b/lighthouse-core/test/results/artifacts/artifacts.json index b7a89c32fb2c..b744322088e0 100644 --- a/lighthouse-core/test/results/artifacts/artifacts.json +++ b/lighthouse-core/test/results/artifacts/artifacts.json @@ -1753,6 +1753,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link", "rel": "", @@ -1767,6 +1768,7 @@ "href": "", "rawHref": "", "onclick": "", + "role": "", "name": "", "text": "external link", "rel": "", @@ -1781,6 +1783,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link", "rel": "nofollow", @@ -1795,6 +1798,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link that uses rel noopener and another unrelated rel attribute", "rel": "noopener nofollow", @@ -1809,6 +1813,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link that uses rel noreferrer and another unrelated rel attribute", "rel": "noreferrer nofollow", @@ -1823,6 +1828,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link that uses rel noopener", "rel": "noopener", @@ -1837,6 +1843,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link that uses rel noreferrer", "rel": "noreferrer", @@ -1851,6 +1858,7 @@ "href": "https://www.google.com/", "rawHref": "https://www.google.com/", "onclick": "", + "role": "", "name": "", "text": "external link that uses rel noopener and noreferrer", "rel": "noopener noreferrer", @@ -1862,9 +1870,10 @@ "listeners": [] }, { - "href": "http://localhost:56001/dobetterweb/doesnotexist", + "href": "http://localhost:53133/dobetterweb/doesnotexist", "rawHref": "./doesnotexist", "onclick": "", + "role": "", "name": "", "text": "internal link is ok", "rel": "", @@ -1879,6 +1888,7 @@ "href": "javascript:void(0)", "rawHref": "javascript:void(0)", "onclick": "", + "role": "", "name": "", "text": "", "rel": "", @@ -1893,6 +1903,7 @@ "href": "mailto:inbox@email.com", "rawHref": "mailto:inbox@email.com", "onclick": "", + "role": "", "name": "", "text": "", "rel": "", diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json index ee5bba28bc98..1d8294543335 100644 --- a/lighthouse-core/test/results/sample_v2.json +++ b/lighthouse-core/test/results/sample_v2.json @@ -3133,7 +3133,7 @@ "nodeLabel": "internal link is ok", "snippet": "" }, - "href": "http://localhost:56001/dobetterweb/doesnotexist", + "href": "http://localhost:53133/dobetterweb/doesnotexist", "target": "_blank", "rel": "", "outerHTML": "" diff --git a/types/artifacts.d.ts b/types/artifacts.d.ts index 372116602505..ea217e52cba7 100644 --- a/types/artifacts.d.ts +++ b/types/artifacts.d.ts @@ -324,6 +324,7 @@ declare global { rawHref: string name?: string text: string + role: string target: string devtoolsNodePath: string selector: string