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

Add a rule and check for empty paragraphs #591

Merged
merged 21 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6a52823
Add a rule and check for empty paragraphs
pattonwebz Apr 25, 2024
c17e9c0
Run the check for empty paragraphs in JS page scanner
pattonwebz Apr 25, 2024
fc819ad
Use the id direct from the import to avoid needing to write a string …
pattonwebz Apr 25, 2024
d533bab
Add the empty paragraph rule to rules array
pattonwebz Apr 26, 2024
7b75bea
Add a filter to make empty paragraph html unique
pattonwebz Apr 26, 2024
ca54ed4
Make empty paragraph a warning
pattonwebz May 14, 2024
e1a5d0b
Fix filter to use method from inside the class
pattonwebz May 14, 2024
e0ac1c5
Use singular form for title and update the summary text
pattonwebz May 14, 2024
88e3a61
Use 'empty_paragraph' as the slug for new rule
pattonwebz May 15, 2024
102cbe4
Use 'empty_paragraph_tag' as the slug for new rule
pattonwebz May 15, 2024
93856d9
Add best practices tag to new empty paragraphs rule
pattonwebz May 15, 2024
c0e9576
Update paragraph_not_empty check to pass elements with no text conten…
pattonwebz May 20, 2024
1bcda56
Simplify the final return in paragraph_not_empty
pattonwebz May 20, 2024
2aee264
Simplify the final return in paragraph_not_empty
pattonwebz May 20, 2024
c6cea02
Merge branch 'develop' into william/127/error-for-empty-paragraphs
pattonwebz May 22, 2024
5e43763
Add a since tag to new method
pattonwebz May 22, 2024
d895dfd
Don't pass on child nodes if the first node is a text node.
pattonwebz May 23, 2024
38ddafb
Add a temporary link that points to docs for empty paragraphs
pattonwebz May 23, 2024
a200fa2
Merge remote-tracking branch 'origin/william/127/error-for-empty-para…
pattonwebz May 23, 2024
fc2f325
Update info/docs link for empty paragraph rule
pattonwebz May 24, 2024
4e3bc10
Merge branch 'develop' into william/127/error-for-empty-paragraphs
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
26 changes: 25 additions & 1 deletion includes/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct() {
*/
public function init_hooks() {
add_action( 'init', [ $this, 'init_rest_routes' ] );
add_filter( 'edac_filter_js_violation_html', [ $this, 'filter_js_validation_html' ], 10, 3 );
}


Expand Down Expand Up @@ -161,6 +162,29 @@ function () use ( $ns, $version ) {
);
}

/**
* Filter the html of the js validation violation.
*
* This can be used to store additional data in the html of the violation.
*
pattonwebz marked this conversation as resolved.
Show resolved Hide resolved
* @since 1.13.0
* @param string $html The html of the violation.
* @param string $rule_id The id of the rule.
* @param array $violation The violation data.
*
* @return string
*/
public function filter_js_validation_html( string $html, string $rule_id, array $violation ): string {
// Add the selector to the violation message as empty paragraphs are almost always
// duplicate html fragments. Adding the selector makes it unique, so it can be saved.
if ( 'empty_paragraph_tag' === $rule_id ) {
$html .= $violation['selector'][0]
? '// {{ ' . $violation['selector'][0] . ' }}'
: '';
}
return $html;
}

/**
* REST handler that saves to the DB a list of js rule violations for a post.
*
Expand Down Expand Up @@ -239,7 +263,7 @@ public function set_post_scan_results( $request ) {

// This rule is one that we've included in our js ruleset.

$html = $violation['html'];
$html = apply_filters( 'edac_filter_js_violation_html', $violation['html'], $rule_id, $violation );
$impact = $violation['impact']; // by default, use the impact setting from the js rule.

//phpcs:ignore Generic.Commenting.Todo.TaskFound
Expand Down
8 changes: 8 additions & 0 deletions includes/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@
'<code>&lt;h2&gt;</code>'
),
],
[
'title' => esc_html__( 'Empty Paragraph Tag', 'accessibility-checker' ),
'info_url' => 'https://a11ychecker.com/help7870',
'slug' => 'empty_paragraph_tag',
'rule_type' => 'warning',
'summary' => esc_html__( 'An Empty Paragraph Tag warning means there is a paragraph tag present that does not contain content. These may be announced by screen readers or create confusion for users. To fix this warning, remove the empty paragraphs from the page. If you need to add spacing between sections, this should be done with padding, margins, or a spacer block.', 'accessibility-checker' ),
'ruleset' => 'js',
],
[
'title' => esc_html__( 'iFrame Missing Title', 'accessibility-checker' ),
'info_url' => 'https://a11ychecker.com/help1953',
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions src/pageScanner/checks/paragraph-not-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Check that a paragraph is NOT empty.
*
* @param {Node} node The paragraph element to evaluate.
* @return {boolean} Returns true if the paragraph has content, false if empty.
*/

export default {
id: 'paragraph_not_empty',
evaluate: ( node ) => {
if ( 'p' !== node.tagName.toLowerCase() ) {
return true;
}

// Pass if there are child nodes and the first child is not a text node (notType of 3).
if ( node.childNodes.length && node.childNodes[ 0 ].nodeType !== 3 ) {
return true;
}

// If there is text content then it passes.
return node.textContent.trim() !== '';
},
};
7 changes: 7 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 emptyParagraph from './rules/empty-paragraph';
import paragraphNotEmpty from './checks/paragraph-not-empty';
import textSmall from './rules/text-small';
import textSizeTooSmall from './checks/text-size-too-small';
import textJustified from './rules/text-justified';
Expand Down Expand Up @@ -44,13 +46,15 @@ const scan = async (
//customRule1,
colorContrastFailure,
underlinedText,
emptyParagraph,
textSmall,
textJustified,
],
checks: [
//alwaysFail,
elementIsAUTag,
elementWithUnderline,
paragraphNotEmpty,
textSizeTooSmall,
textIsJustified,
],
Expand All @@ -69,6 +73,9 @@ const scan = async (
'meta-viewport',
textJustified.id,
textSmall.id,
colorContrastFailure.id,
underlinedText.id,
emptyParagraph.id,
],
},

Expand Down
17 changes: 17 additions & 0 deletions src/pageScanner/rules/empty-paragraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
id: 'empty_paragraph_tag',
selector: 'p',
excludeHidden: false,
tags: [
'cat.text',
'best-practices',
],
impact: 'moderate',
metadata: {
description: 'Detects empty paragraph tags',
help: 'Paragraphs should not be used for layout purposes and should never be empty',
},
all: [],
any: [ 'paragraph_not_empty' ],
none: [],
};
Loading