-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: page function updates relating to upcoming tap targets audit #6702
Merged
patrickhulce
merged 4 commits into
GoogleChrome:master
from
mattzeunert:page-functions-tap-targets
Dec 5, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
// @ts-nocheck | ||
'use strict'; | ||
|
||
/* global window document */ | ||
/* global window document Node */ | ||
|
||
/** | ||
* Helper functions that are passed by `toString()` by Driver to be evaluated in target page. | ||
|
@@ -119,7 +119,7 @@ function getOuterHTMLSnippet(element, ignoreAttrs = []) { | |
clone.removeAttribute(attribute); | ||
}); | ||
|
||
const reOpeningTag = /^.*?>/; | ||
const reOpeningTag = /^[\s\S]*?>/; | ||
const match = clone.outerHTML.match(reOpeningTag); | ||
|
||
return (match && match[0]) || ''; | ||
|
@@ -153,6 +153,71 @@ function ultradumbBenchmark() { | |
return Math.round(iterations / durationInSeconds); | ||
} | ||
|
||
/** | ||
* Adapted from DevTools' SDK.DOMNode.prototype.path | ||
* https://github.com/ChromeDevTools/devtools-frontend/blob/7a2e162ddefd/front_end/sdk/DOMModel.js#L530-L552 | ||
* TODO: Doesn't handle frames or shadow roots... | ||
* @param {Node} node | ||
*/ | ||
/* istanbul ignore next */ | ||
function getNodePath(node) { | ||
/** @param {Node} node */ | ||
function getNodeIndex(node) { | ||
let index = 0; | ||
let prevNode; | ||
while (prevNode = node.previousSibling) { | ||
node = prevNode; | ||
// skip empty text nodes | ||
if (node.nodeType === Node.TEXT_NODE && node.textContent && | ||
node.textContent.trim().length === 0) continue; | ||
index++; | ||
} | ||
return index; | ||
} | ||
|
||
const path = []; | ||
while (node && node.parentNode) { | ||
const index = getNodeIndex(node); | ||
path.push([index, node.nodeName]); | ||
node = node.parentNode; | ||
} | ||
path.reverse(); | ||
return path.join(','); | ||
} | ||
|
||
/** | ||
* @param {Element} node | ||
* @returns {string} | ||
*/ | ||
/* istanbul ignore next */ | ||
function getNodeSelector(node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think we could get a quick test for this function? don't need crazy coverage just something to make sure we don't break it |
||
/** | ||
* @param {Element} node | ||
*/ | ||
function getSelectorPart(node) { | ||
let part = node.tagName.toLowerCase(); | ||
if (node.id) { | ||
part += '#' + node.id; | ||
} else if (node.classList.length > 0) { | ||
part += '.' + node.classList[0]; | ||
} | ||
return part; | ||
} | ||
|
||
const parts = []; | ||
while (parts.length < 4) { | ||
parts.unshift(getSelectorPart(node)); | ||
if (!node.parentElement) { | ||
break; | ||
} | ||
node = node.parentElement; | ||
if (node.tagName === 'HTML') { | ||
break; | ||
} | ||
} | ||
return parts.join(' > '); | ||
} | ||
|
||
module.exports = { | ||
wrapRuntimeEvalErrorInBrowserString: wrapRuntimeEvalErrorInBrowser.toString(), | ||
registerPerformanceObserverInPageString: registerPerformanceObserverInPage.toString(), | ||
|
@@ -162,4 +227,7 @@ module.exports = { | |
getOuterHTMLSnippet: getOuterHTMLSnippet, | ||
ultradumbBenchmark: ultradumbBenchmark, | ||
ultradumbBenchmarkString: ultradumbBenchmark.toString(), | ||
getNodePathString: getNodePath.toString(), | ||
getNodeSelectorString: getNodeSelector.toString(), | ||
getNodeSelector: getNodeSelector, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ const pageFunctions = require('../../lib/page-functions'); | |
|
||
/* eslint-env jest */ | ||
|
||
describe('DetailsRenderer', () => { | ||
describe('Page Functions', () => { | ||
let dom; | ||
|
||
beforeAll(() => { | ||
|
@@ -44,5 +44,19 @@ describe('DetailsRenderer', () => { | |
['style-missing', 'aria-label-missing'] | ||
), '<div id="1" style="style" aria-label="label">'); | ||
}); | ||
|
||
it('works if attribute values contain line breaks', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
assert.equal(pageFunctions.getOuterHTMLSnippet( | ||
dom.createElement('div', '', {style: 'style1\nstyle2'})), '<div style="style1\nstyle2">'); | ||
}); | ||
}); | ||
|
||
describe('getNodeSelector', () => { | ||
it('Uses IDs where available and otherwise falls back to classes', () => { | ||
const parentEl = dom.createElement('div', '', {id: 'wrapper', class: 'dont-use-this'}); | ||
const childEl = dom.createElement('div', '', {class: 'child'}); | ||
parentEl.appendChild(childEl); | ||
assert.equal(pageFunctions.getNodeSelector(childEl), 'div#wrapper > div.child'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could keep this the same, if you also export
getNodePath
inpage-functions.js
.Will need that module to export
getNodePath
for linking nodes in the report for the font-size audit (#6436) (and for other non-accessibility audits).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean @hoten, it's run in the context of the page and he's exported the stringified version which is what he'll need, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize all of that was running in context of the page. Nevermind!