-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
anchor-elements.js
127 lines (110 loc) · 4.32 KB
/
anchor-elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @license Copyright 2019 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';
/* global getNodeDetails */
const Gatherer = require('./gatherer.js');
const pageFunctions = require('../../lib/page-functions.js');
/* eslint-env browser, node */
/**
* Function that is stringified and run in the page to collect anchor elements.
* Additional complexity is introduced because anchors can be HTML or SVG elements.
*
* We use this evaluateAsync method because the `node.getAttribute` method doesn't actually normalize
* the values like access from JavaScript in-page does.
*
* @return {LH.Artifacts['AnchorElements']}
*/
/* c8 ignore start */
function collectAnchorElements() {
/** @param {string} url */
const resolveURLOrEmpty = url => {
try {
return new URL(url, window.location.href).href;
} catch (_) {
return '';
}
};
/** @param {HTMLAnchorElement|SVGAElement} node */
function getTruncatedOnclick(node) {
const onclick = node.getAttribute('onclick') || '';
return onclick.slice(0, 1024);
}
/** @type {Array<HTMLAnchorElement|SVGAElement>} */
// @ts-expect-error - put into scope via stringification
const anchorElements = getElementsInDocument('a'); // eslint-disable-line no-undef
return anchorElements.map(node => {
if (node instanceof HTMLAnchorElement) {
return {
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,
target: node.target,
// @ts-expect-error - getNodeDetails put into scope via stringification
node: getNodeDetails(node),
};
}
return {
href: resolveURLOrEmpty(node.href.baseVal),
rawHref: node.getAttribute('href') || '',
onclick: getTruncatedOnclick(node),
role: node.getAttribute('role') || '',
text: node.textContent || '',
rel: '',
target: node.target.baseVal || '',
// @ts-expect-error - getNodeDetails put into scope via stringification
node: getNodeDetails(node),
};
});
}
/* c8 ignore stop */
/**
* @param {LH.Gatherer.PassContext['driver']} driver
* @param {string} devtoolsNodePath
* @return {Promise<Array<{type: string}>>}
*/
async function getEventListeners(driver, devtoolsNodePath) {
const objectId = await driver.resolveDevtoolsNodePathToObjectId(devtoolsNodePath);
if (!objectId) return [];
const response = await driver.sendCommand('DOMDebugger.getEventListeners', {
objectId,
});
return response.listeners.map(({type}) => ({type}));
}
class AnchorElements extends Gatherer {
/**
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['AnchorElements']>}
*/
async afterPass(passContext) {
const driver = passContext.driver;
const anchors = await driver.evaluate(collectAnchorElements, {
args: [],
useIsolation: true,
deps: [
pageFunctions.getElementsInDocumentString,
pageFunctions.getNodeDetailsString,
],
});
await driver.sendCommand('DOM.enable');
// 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 listeners = await getEventListeners(driver, anchor.node.devtoolsNodePath);
return {
...anchor,
listeners,
};
});
const result = await Promise.all(anchorsWithEventListeners);
await driver.sendCommand('DOM.disable');
return result;
}
}
module.exports = AnchorElements;