Skip to content

Commit

Permalink
Merge branch 'MDL-75404-master_toolbrickfieldlinkinnewwindow' of http…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 16, 2023
2 parents 9b64511 + 44cb334 commit a2579a5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,19 +379,61 @@ public function element_contains_readable_text($element): bool {
return false;
}

/**
* Returns an array of the newwindowphrases for all enabled language packs.
* @return array of the newwindowphrases for all enabled language packs.
*/
public static function get_all_newwindowphrases(): array {
// Need to process all enabled lang versions of newwindowphrases.
return static::get_all_phrases('newwindowphrases');
}

/**
* Returns an array of the invalidlinkphrases for all enabled language packs.
* @return array of the invalidlinkphrases for all enabled language packs.
*/
public static function get_all_invalidlinkphrases(): array {
// Need to process all enabled lang versions of invalidlinkphrases.
return static::get_all_phrases('invalidlinkphrases');
}

/**
* Returns an array of the relevant phrases for all enabled language packs.
* @param string $stringname the language string identifier you want get the phrases for.
* @return array of the invalidlinkphrases for all enabled language packs.
*/
protected static function get_all_phrases(string $stringname): array {
$stringmgr = get_string_manager();
$allstrings = [];
$enabledlangs = get_string_manager()->get_list_of_translations();

// Somehow, an invalid string was requested. Add exception handling for this in the future.
if (!$stringmgr->string_exists($stringname, manager::PLUGINNAME)) {
return $allstrings;
}

// Need to process all enabled lang versions of invalidlinkphrases.
$enabledlangs = $stringmgr->get_list_of_translations();
foreach ($enabledlangs as $lang => $value) {
$tmpstring = (string)new \lang_string('invalidlinkphrases', manager::PLUGINNAME, null, $lang);
$tmpstring = (string)new \lang_string($stringname, manager::PLUGINNAME, null, $lang);
$tmplangarray = explode('|', $tmpstring);
$allstrings = array_merge($allstrings, $tmplangarray);
}
// Removing duplicates if a lang is enabled, yet using default 'en' due to no relevant lang file.
$allstrings = array_unique($allstrings);
return $allstrings;
}

/**
* Assesses whether a string contains any readable text, which is text that
* contains any characters other than whitespace characters.
*
* @param string $text
* @return bool
*/
public static function is_text_readable(string $text): bool {
// These characters in order are a space, tab, line feed, carriage return,
// NUL-byte, vertical tab and non-breaking space unicode character \xc2\xa0.
$emptycharacters = " \t\n\r\0\x0B\xc2\xa0";
return trim($text, $emptycharacters) != '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,27 @@ class a_links_dont_open_new_window extends brickfield_accessibility_test {
* The main check function. This is called by the parent class to actually check content.
*/
public function check(): void {

// Need to process all enabled lang versions of newwindowphrases.
$text = brickfield_accessibility_test::get_all_newwindowphrases();

foreach ($this->get_all_elements('a') as $a) {
if ($a->hasAttribute('target') && !in_array($a->getAttribute('target'), $this->allowedtargets)) {
$this->add_report($a);
$phrasefound = false;
foreach ($text as $phrase) {
// Sanity check for readable text.
if (!brickfield_accessibility_test::is_text_readable($phrase)) {
break;
}
$pos = stripos($a->nodeValue, $phrase);
if ($pos !== false) {
$phrasefound = true;
break;
}
}
if (!$phrasefound) {
$this->add_report($a);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,26 @@ class area_dont_open_new_window extends brickfield_accessibility_test {
* The main check function. This is called by the parent class to actually check content
*/
public function check(): void {
// Need to process all enabled lang versions of newwindowphrases.
$text = brickfield_accessibility_test::get_all_newwindowphrases();

foreach ($this->get_all_elements('area') as $area) {
if ($area->hasAttribute('target') && !in_array($area->getAttribute('target'), $this->allowedtargets)) {
$this->add_report($area);
$phrasefound = false;
foreach ($text as $phrase) {
// Sanity check for readable text.
if (!brickfield_accessibility_test::is_text_readable($phrase)) {
break;
}
$pos = stripos($area->getAttribute('alt'), $phrase);
if ($pos !== false) {
$phrasefound = true;
break;
}
}
if (!$phrasefound) {
$this->add_report($area);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions admin/tool/brickfield/lang/en/tool_brickfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
$string['invalidlinkphrases'] = 'click|click here|here|more|more here|info|info here|information|information here|read more|read more here|further information|further information here|further details|further details here';
$string['module'] = 'Module';
$string['modulename'] = 'Name';
$string['newwindowphrases'] = 'new window|new-window|new_window';
$string['noerrorsfound'] = 'No common accessibility errors were found for your search parameters. Congratulations!';
$string['norecords'] = 'No relevant records were found for your search parameters.';
$string['notregistered'] = 'Your accessibility toolkit needs to be registered.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ class a_links_dont_open_new_window_test extends all_checks {
</html>
EOD;

/** @var string Html pass 4 */
private $htmlpass4 = <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<title>A link can indicate that it will open in a new window</title>
</head>
<body>
<a href="www.youtube.com" target="_blank">This is youtube (opens in a new window)</a>
</body>
</html>
EOD;

/**
* Test for links opening a new tab or window
*/
Expand All @@ -102,5 +115,8 @@ public function test_check() {

$results = $this->get_checker_results($this->htmlpass3);
$this->assertEmpty($results);

$results = $this->get_checker_results($this->htmlpass4);
$this->assertEmpty($results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ class area_dont_open_new_window_test extends all_checks {
</html>
EOD;

/** @var string Html pass 4 */
private $htmlpass4 = <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<title>A link can indicate that it will open in a new window</title>
</head>
<body>
<area target="_blank" alt="(opens in a new window)"></area>
</body>
</html>
EOD;

/**
* Test Area tags opening new window without warning
*/
Expand All @@ -102,5 +115,8 @@ public function test_check() {

$results = $this->get_checker_results($this->htmlpass3);
$this->assertEmpty($results);

$results = $this->get_checker_results($this->htmlpass4);
$this->assertEmpty($results);
}
}

0 comments on commit a2579a5

Please sign in to comment.