Skip to content

Commit b69574b

Browse files
committed
core(font-size): exclude invisible text
1 parent 36cac18 commit b69574b

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

cli/test/fixtures/font-size.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ <h6>2</h6>
3434
<script class='small'>
3535
// text from SCRIPT tags should be ignored by the font-size audit
3636
</script>
37+
38+
<!-- PASS(font-size): text is not visible -->
39+
<div style="visibility: hidden;">
40+
<div class="small">Invisible</div>
41+
</div>
3742
</body>
3843
</html>

core/gather/gatherers/seo/font-size.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ class FontSize extends BaseGatherer {
221221

222222
const nodeIndex = doc.layout.nodeIndex[layoutIndex];
223223
const styles = doc.layout.styles[layoutIndex];
224-
const [fontSizeStringId] = styles;
224+
const [fontSizeStringId, visibilityStringId] = styles;
225225
const fontSize = getFloat(fontSizeStringId);
226+
const visibility = getString(visibilityStringId);
226227

227228
const parentIndex = nodes.parentIndex[nodeIndex];
228229
const grandParentIndex = nodes.parentIndex[parentIndex];
@@ -234,6 +235,7 @@ class FontSize extends BaseGatherer {
234235
nodeIndex,
235236
backendNodeId: nodes.backendNodeId[nodeIndex],
236237
fontSize,
238+
visibility,
237239
textLength: getTextLength(text),
238240
parentNode: {
239241
...parentNode,
@@ -257,17 +259,24 @@ class FontSize extends BaseGatherer {
257259
let failingTextLength = 0;
258260

259261
for (const textNodeData of this.getTextNodesInLayoutFromSnapshot(snapshot)) {
262+
if (textNodeData.visibility === 'hidden') {
263+
continue;
264+
}
265+
260266
totalTextLength += textNodeData.textLength;
261-
if (textNodeData.fontSize < MINIMAL_LEGIBLE_FONT_SIZE_PX) {
262-
// Once a bad TextNode is identified, its parent Node is needed.
263-
failingTextLength += textNodeData.textLength;
264-
failingNodes.push({
265-
nodeId: 0, // Set later in fetchFailingNodeSourceRules.
266-
parentNode: textNodeData.parentNode,
267-
textLength: textNodeData.textLength,
268-
fontSize: textNodeData.fontSize,
269-
});
267+
268+
if (textNodeData.fontSize >= MINIMAL_LEGIBLE_FONT_SIZE_PX) {
269+
continue;
270270
}
271+
272+
// Once a bad TextNode is identified, its parent Node is needed.
273+
failingTextLength += textNodeData.textLength;
274+
failingNodes.push({
275+
nodeId: 0, // Set later in fetchFailingNodeSourceRules.
276+
parentNode: textNodeData.parentNode,
277+
textLength: textNodeData.textLength,
278+
fontSize: textNodeData.fontSize,
279+
});
271280
}
272281

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

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

300309
const {

0 commit comments

Comments
 (0)