mirror of
https://github.com/moodle/moodle.git
synced 2025-05-08 01:05:48 +02:00
MDL-75404 tool_brickfield: Allow link open in new window with warning
This commit is contained in:
parent
5d320dd7d1
commit
44cb3349b2
admin/tool/brickfield
classes/local/htmlchecker/common
lang/en
tests/local/htmlchecker/common/checks
@ -379,19 +379,61 @@ class brickfield_accessibility_test {
|
||||
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) != '';
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,7 @@ $string['invalidcourseid'] = 'Invalid course, please check your input';
|
||||
$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.';
|
||||
|
16
admin/tool/brickfield/tests/local/htmlchecker/common/checks/a_links_dont_open_new_window_test.php
16
admin/tool/brickfield/tests/local/htmlchecker/common/checks/a_links_dont_open_new_window_test.php
@ -87,6 +87,19 @@ EOD;
|
||||
</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
|
||||
*/
|
||||
@ -102,5 +115,8 @@ EOD;
|
||||
|
||||
$results = $this->get_checker_results($this->htmlpass3);
|
||||
$this->assertEmpty($results);
|
||||
|
||||
$results = $this->get_checker_results($this->htmlpass4);
|
||||
$this->assertEmpty($results);
|
||||
}
|
||||
}
|
||||
|
@ -87,6 +87,19 @@ EOD;
|
||||
</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
|
||||
*/
|
||||
@ -102,5 +115,8 @@ EOD;
|
||||
|
||||
$results = $this->get_checker_results($this->htmlpass3);
|
||||
$this->assertEmpty($results);
|
||||
|
||||
$results = $this->get_checker_results($this->htmlpass4);
|
||||
$this->assertEmpty($results);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user