Skip to content

Commit

Permalink
Merge pull request #699 from equalizedigital/william/697/fix-empty-li…
Browse files Browse the repository at this point in the history
…nk-detection

Fixed: Allow empty_link rule to detect actually empty links
  • Loading branch information
pattonwebz committed Jul 1, 2024
2 parents 8b602e7 + 902472e commit 348842b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
21 changes: 14 additions & 7 deletions includes/rules/empty_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,32 @@ function edac_rule_empty_link( $content, $post ) { // phpcs:ignore -- $post is r
// does not have a name.

$image = $link->find( 'img' );
if ( ! $error && isset( $input[0] ) && empty( trim( $image[0]->getAttribute( 'alt' ) ) ) ) {
$input = $link->find( 'input' );
$i = $link->find( 'i' );

// If there's no image, input or i tag it's just an empty link and should be flagged.
if ( empty( $image ) && empty( $input ) && empty( $i ) ) {
$error = $a_tag_code;
}

if ( ! $error && isset( $image[0] ) && empty( trim( $image[0]->getAttribute( 'alt' ) ) ) ) {

// The first image inside the link does not have an alt.
// Throw error.
$error = $a_tag_code;
}

$input = $link->find( 'input' );
if ( ! $error && isset( $input[0] ) && empty( trim( $image[0]->getAttribute( 'value' ) ) ) ) {
if ( ! $error && isset( $input[0] ) && empty( trim( $input[0]->getAttribute( 'value' ) ) ) ) {

// The first input inside the link does not have a value.
// Throw error.
$error = $a_tag_code;
}

$i = $link->find( 'i' );
if ( ! $error && isset( $input[0] ) &&
empty( trim( $i[0]->getAttribute( 'title' ) ) ) &&
empty( trim( $i[0]->getAttribute( 'aria-label' ) ) )
if ( ! $error &&
isset( $i[0] ) &&
empty( trim( $i[0]->getAttribute( 'title' ) ) ) &&
empty( trim( $i[0]->getAttribute( 'aria-label' ) ) )
) {

// The first i inside the link does not have a title &
Expand Down
111 changes: 111 additions & 0 deletions tests/phpunit/includes/rules/EmptyLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Tests the empty_link rule.
*
* @package Accessibility_Checker
*/

/**
* Some test cases for the empty_link rule.
*
* @group rules
*/
class EmptyLinkTest extends WP_UnitTestCase {

/**
* Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error.
*/
public function test_empty_link() {

$expected_error = '<a href="http://example.com"></a>';

$not_expected_error = '<a href="http://example.com">Some content</a>';


$dom = new EDAC_Dom();
$dom->load( $expected_error . PHP_EOL . $not_expected_error );

$errors = edac_rule_empty_link( [ 'html' => $dom ], null );

$this->assertContains( $expected_error, $errors );
$this->assertNotContains( $not_expected_error, $errors );
}

/**
* Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error.
*/
public function test_empty_link_with_img() {
$expected_errors = [
'<a href="http://example.com"><img src="http://example.com/image.jpg" alt=""></a>',
'<a href="http://example.com"><img src="http://example.com/image.jpg" alt=" "></a>',
];

$not_expected_error = '<a href="http://example.com"><img src="http://example.com/image.jpg" alt="A filled alt"></a>';

$dom = new EDAC_Dom();
$dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . $not_expected_error );

$errors = edac_rule_empty_link( [ 'html' => $dom ], null );

foreach ( $expected_errors as $expected_error ) {
$this->assertContains( $expected_error, $errors );
}

$this->assertNotContains( $not_expected_error, $errors );
}

/**
* Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error.
*/
public function test_empty_link_with_input() {

$expected_errors = [
'<a href="http://example.com"><input type="text"></a>',
'<a href="http://example.com"><input type="text" value=""></a>',
'<a href="http://example.com"><input type="text" value=" "></a>', // whitespace should be stripped, this is still empty.
];

$not_expected_error = '<a href="http://example.com"><input type="text" value="Some value"></a>';

$dom = new EDAC_Dom();
$dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . $not_expected_error );

$errors = edac_rule_empty_link( [ 'html' => $dom ], null );

foreach ( $expected_errors as $expected_error ) {
$this->assertContains( $expected_error, $errors );
}
$this->assertNotContains( $not_expected_error, $errors );
}

/**
* Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error.
*/
public function test_empty_link_with_i() {
$expected_errors = [
'<a href="http://example.com"><i></i></a>',
'<a href="http://example.com"><i title=""></i></a>',
'<a href="http://example.com"><i aria-label=""></i></a>',
'<a href="http://example.com"><i title=" "></i></a>', // whitespace should be stripped, this is still empty.
'<a href="http://example.com"><i aria-label=" "></i></a>', // whitespace should be stripped, this is still empty.
];

$not_expected_errors = [
'<a href="http://example.com"><i title="Some title"></i></a>',
'<a href="http://example.com"><i aria-label="A label"></i></a>',
'<a href="http://example.com"><i title="Some title" aria-label="A label"></i></a>',
];

$dom = new EDAC_Dom();
$dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . implode( PHP_EOL, $not_expected_errors ) );

$errors = edac_rule_empty_link( [ 'html' => $dom ], null );

foreach ( $expected_errors as $expected_error ) {
$this->assertContains( $expected_error, $errors );
}
foreach ( $not_expected_errors as $not_expected_error ) {
$this->assertNotContains( $not_expected_error, $errors );
}
}
}

0 comments on commit 348842b

Please sign in to comment.