-
Notifications
You must be signed in to change notification settings - Fork 9
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
Check possible_heading rule with computedStyles #622
Merged
pattonwebz
merged 17 commits into
develop
from
william/612/move-possible_heading-rule-to-JS-check
May 24, 2024
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
437f45d
Add a rule and check file for detection paragraphs that are possible …
pattonwebz 8187628
Include the possible_heading rule in the page scanner
pattonwebz 0d5ade2
Make the possible_heading rule use the js ruleset
pattonwebz e9bb544
Remove the possible_heading.php file as it is replaced by JS
pattonwebz ddb578f
Improve comment in paragraph-styled-as-header.js check
pattonwebz 45f435c
Add a helpers file with a fontSizeInPxxx function
pattonwebz 3580bf7
Use new fontSizeInPx helper
pattonwebz b02dc50
Use < 16px in first return so that everything after it is 16px or more.
pattonwebz 5a235cd
Merge branch 'develop' into william/612/move-possible_heading-rule-to…
pattonwebz 9c1ac6f
Don't check possible headings if they are in blockquote, figcaption o…
pattonwebz 404bc15
Add text for possible_heading rule
pattonwebz cddf1ae
Update some code comments
pattonwebz 26c5f54
Simplify the node value check early bail
pattonwebz 6274a5b
Use a simplified catch for null and undefined as early bail
pattonwebz 913aeca
Handle potential `NaN` values by checking for instance of Number
pattonwebz 01bfc89
Merge remote-tracking branch 'origin/william/612/move-possible_headin…
pattonwebz 97ea323
Merge branch 'develop' into william/612/move-possible_heading-rule-to…
pattonwebz 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Check for elements with text content that is styled like a header. | ||
* | ||
* Axe-core has a built-in `p-as-heading` rule that checks for paragraphs | ||
* that are styled like headings. That rule is less robust than this check, | ||
* as it only checks for paragraphs with bold or italic text and gives back | ||
* 'incomplete' or uncertain results for some of the test data that we want | ||
* to flag for further checks. | ||
* | ||
* This check in this file takes into account the font size, length of the | ||
* text, and considers paragraphs with large font size as headers as well. | ||
* | ||
* @param {Node} node The node to evaluate. | ||
* @return {boolean} True if the node is styled like a header, false otherwise. Paragraphs with only bold or italic, | ||
* are shorter than 50 characters, or are short with large font size are considered headers. | ||
*/ | ||
|
||
import { fontSizeInPx } from '../helpers/helpers.js'; | ||
|
||
export default { | ||
id: 'paragraph_styled_as_header', | ||
evaluate: ( node ) => { | ||
const pixelSize = fontSizeInPx( node ); | ||
|
||
// long paragraphs or with size under 16px are unlikely to be headers. | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( node.textContent.trim().length > 50 || pixelSize < 16 ) { | ||
return false; | ||
} | ||
|
||
// paragraphs that are 20px or more are probably headers. | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( pixelSize >= 20 ) { | ||
return true; | ||
} | ||
|
||
const style = window.getComputedStyle( node ); | ||
|
||
const fontWeight = style.getPropertyValue( 'font-weight' ); | ||
const isBold = [ 'bold', 'bolder', '700', '800', '900' ].includes( fontWeight ); | ||
|
||
const fontStyle = style.getPropertyValue( 'font-style' ); | ||
const isItalic = [ 'italic', 'oblique' ].includes( fontStyle ); | ||
|
||
const hasBoldOrItalicTag = node.querySelector( 'b, strong, i, em' ) !== null; | ||
|
||
if ( isBold || isItalic || hasBoldOrItalicTag ) { | ||
return true; | ||
} | ||
|
||
// didn't find anything indicating this is a possible header. | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const fontSizeInPx = ( node ) => { | ||
if ( node === null || node === 'undefined' || node.nodeType !== Node.ELEMENT_NODE ) { | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
} | ||
|
||
return parseFloat( window.getComputedStyle( node ).fontSize ); | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default { | ||
id: 'possible_heading', | ||
selector: 'p', | ||
excludeHidden: false, | ||
tags: [ | ||
'cat.text', | ||
pattonwebz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
metadata: { | ||
description: 'Headings should be used to convey the structure of the page, not styled paragraphs', | ||
help: 'Paragraphs should not be styled to look like headings. Use the appropriate heading tag instead.', | ||
}, | ||
all: [], | ||
any: [], | ||
none: [ 'paragraph_styled_as_header' ], | ||
}; |
Oops, something went wrong.
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.
I'm not sure what happens before this but is there any value in returning early here if node is empty?