Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(font-size): exclude invisible text #16281

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/test/fixtures/font-size.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ <h6>2</h6>
<script class='small'>
// text from SCRIPT tags should be ignored by the font-size audit
</script>

<!-- PASS(font-size): text is not visible -->
<div style="visibility: hidden;">
<div class="small">Invisible</div>
</div>
</body>
</html>
31 changes: 20 additions & 11 deletions core/gather/gatherers/seo/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ class FontSize extends BaseGatherer {

const nodeIndex = doc.layout.nodeIndex[layoutIndex];
const styles = doc.layout.styles[layoutIndex];
const [fontSizeStringId] = styles;
const [fontSizeStringId, visibilityStringId] = styles;
const fontSize = getFloat(fontSizeStringId);
const visibility = getString(visibilityStringId);

const parentIndex = nodes.parentIndex[nodeIndex];
const grandParentIndex = nodes.parentIndex[parentIndex];
Expand All @@ -234,6 +235,7 @@ class FontSize extends BaseGatherer {
nodeIndex,
backendNodeId: nodes.backendNodeId[nodeIndex],
fontSize,
visibility,
textLength: getTextLength(text),
parentNode: {
...parentNode,
Expand All @@ -257,17 +259,24 @@ class FontSize extends BaseGatherer {
let failingTextLength = 0;

for (const textNodeData of this.getTextNodesInLayoutFromSnapshot(snapshot)) {
if (textNodeData.visibility === 'hidden') {
continue;
}

totalTextLength += textNodeData.textLength;
if (textNodeData.fontSize < MINIMAL_LEGIBLE_FONT_SIZE_PX) {
// Once a bad TextNode is identified, its parent Node is needed.
failingTextLength += textNodeData.textLength;
failingNodes.push({
nodeId: 0, // Set later in fetchFailingNodeSourceRules.
parentNode: textNodeData.parentNode,
textLength: textNodeData.textLength,
fontSize: textNodeData.fontSize,
});

if (textNodeData.fontSize >= MINIMAL_LEGIBLE_FONT_SIZE_PX) {
continue;
}

// Once a bad TextNode is identified, its parent Node is needed.
failingTextLength += textNodeData.textLength;
failingNodes.push({
nodeId: 0, // Set later in fetchFailingNodeSourceRules.
parentNode: textNodeData.parentNode,
textLength: textNodeData.textLength,
fontSize: textNodeData.fontSize,
});
}

return {totalTextLength, failingTextLength, failingNodes};
Expand All @@ -294,7 +303,7 @@ class FontSize extends BaseGatherer {

// Get the computed font-size style of every node.
const snapshot = await session.sendCommand('DOMSnapshot.captureSnapshot', {
computedStyles: ['font-size'],
computedStyles: ['font-size', 'visibility'],
});

const {
Expand Down
Loading