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

Check possible_heading rule with computedStyles #622

Merged
merged 17 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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 May 14, 2024
8187628
Include the possible_heading rule in the page scanner
pattonwebz May 14, 2024
0d5ade2
Make the possible_heading rule use the js ruleset
pattonwebz May 14, 2024
e9bb544
Remove the possible_heading.php file as it is replaced by JS
pattonwebz May 14, 2024
ddb578f
Improve comment in paragraph-styled-as-header.js check
pattonwebz May 20, 2024
45f435c
Add a helpers file with a fontSizeInPxxx function
pattonwebz May 21, 2024
3580bf7
Use new fontSizeInPx helper
pattonwebz May 21, 2024
b02dc50
Use < 16px in first return so that everything after it is 16px or more.
pattonwebz May 21, 2024
5a235cd
Merge branch 'develop' into william/612/move-possible_heading-rule-to…
pattonwebz May 22, 2024
9c1ac6f
Don't check possible headings if they are in blockquote, figcaption o…
pattonwebz May 23, 2024
404bc15
Add text for possible_heading rule
pattonwebz May 24, 2024
cddf1ae
Update some code comments
pattonwebz May 24, 2024
26c5f54
Simplify the node value check early bail
pattonwebz May 24, 2024
6274a5b
Use a simplified catch for null and undefined as early bail
pattonwebz May 24, 2024
913aeca
Handle potential `NaN` values by checking for instance of Number
pattonwebz May 24, 2024
01bfc89
Merge remote-tracking branch 'origin/william/612/move-possible_headin…
pattonwebz May 24, 2024
97ea323
Merge branch 'develop' into william/612/move-possible_heading-rule-to…
pattonwebz May 24, 2024
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
1 change: 1 addition & 0 deletions includes/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
'slug' => 'possible_heading',
'rule_type' => 'warning',
'summary' => esc_html__( 'A Possible Heading warning occurs when there is text on a page that appears to be a heading, but has not been coded with proper heading tags. This warning is appears if there are short phrases or strings of text less than 50 characters in length that are formatted in a way which suggests they might be being used as headers (they are 20 pixels or bigger, or are 16 pixels or bigger and bold and/or italicized). To fix a Possible Heading warning, you will need to determine if the flagged text is indeed intended to be a heading. If so, you need to change it from a paragraph to a heading at the proper level. If it is not supposed to be a heading then you can safely “Ignore” the warning.', 'accessibility-checker' ),
'ruleset' => 'js',
],
[
'title' => esc_html__( 'Blinking or Scrolling Content', 'accessibility-checker' ),
Expand Down
152 changes: 0 additions & 152 deletions includes/rules/possible_heading.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/pageScanner/checks/paragraph-styled-as-header.js
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 );
Copy link
Member

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?

if ( ! node ) {
     return false;
}


// long paragraphs or with size under 16px are unlikely to be headers.
if ( node.textContent.trim().length > 50 || pixelSize < 16 ) {
return false;
}

// paragraphs that are 20px or more are probably headers.
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.
return false;
},
};
7 changes: 7 additions & 0 deletions src/pageScanner/helpers/helpers.js
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 ) {
return 0;
}

return parseFloat( window.getComputedStyle( node ).fontSize );
};
5 changes: 5 additions & 0 deletions src/pageScanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import colorContrastFailure from './rules/color-contrast-failure';
import underlinedText from './rules/underlined-text';
import elementWithUnderline from './checks/element-with-underline';
import elementIsAUTag from './checks/element-is-u-tag';
import possibleHeading from './rules/possible-heading';
import paragraphStyledAsHeader from './checks/paragraph-styled-as-header';

//TODO: examples:
//import customRule1 from './rules/custom-rule-1';
Expand Down Expand Up @@ -40,11 +42,13 @@ const scan = async (
// customRule1,
colorContrastFailure,
underlinedText,
possibleHeading,
],
checks: [
//alwaysFail,
elementIsAUTag,
elementWithUnderline,
paragraphStyledAsHeader,
],
iframes: false,

Expand All @@ -56,6 +60,7 @@ const scan = async (
values: [
'color_contrast_failure',
'underlined_text',
possibleHeading.id,
],
},

Expand Down
15 changes: 15 additions & 0 deletions src/pageScanner/rules/possible-heading.js
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',
],
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' ],
};
Loading