Skip to content

Commit

Permalink
Refactor the text_size_too_small check to try more accurately determi…
Browse files Browse the repository at this point in the history
…ne if the node is the one to analyse

It looks now at child nodes if they exist for TEXT_NODE types
  • Loading branch information
pattonwebz committed Jan 21, 2025
1 parent e620f12 commit 32a1ad6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/pageScanner/checks/text-size-too-small.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ const SMALL_FONT_SIZE_THRESHOLD = 10;
export default {
id: 'text_size_too_small',
evaluate: ( node ) => {
// fails if the font size is less than or equal to 10px.
return fontSizeInPx( node ) <= SMALL_FONT_SIZE_THRESHOLD;
// If the node has no text content then it can't have text that's too small.
if ( ! node.textContent.trim().length ) {
return false;
}

// If the node has no child nodes, then consider it as a text node itself and check it.
if ( ! node.childNodes.length ) {
return fontSizeInPx( node ) <= SMALL_FONT_SIZE_THRESHOLD;
}

// Check onlychild nodes of the element that are text nodes.
let isTextTooSmall = false;
node.childNodes.forEach( ( child ) => {
if ( child.nodeType === Node.TEXT_NODE ) {
if ( fontSizeInPx( node ) <= SMALL_FONT_SIZE_THRESHOLD ) {
isTextTooSmall = true;
}
}
} );
return isTextTooSmall;
},
};

0 comments on commit 32a1ad6

Please sign in to comment.