find('input');
foreach ($inputs as $input) {
$parent = $input->parent();
if ($parent && $parent->tag !== 'label') {
if (!$input->hasAttribute('id') && !$input->hasAttribute('placeholder') && !$input->hasAttribute('aria-label') && !$input->hasAttribute('aria-labelledby')) {
$inputType = $input->getAttribute('type');
if ($inputType && !in_array($inputType, ['hidden', 'button', 'submit'])) {
$input->setAttribute('aria-label', $inputType);
}
}
}
}
return $dom->html();
}
// Links do not have a discernible name
function easywpstuff_assesplus_add_aria_labels_to_links($html) {
// Remove closing tags for img and source elements to avoid parsing issues
$html = str_replace('', '', $html);
$html = str_replace('', '', $html);
$dom = new \DiDom\Document($html);
if (empty($dom)) {
return $dom;
}
$links = $dom->find('a');
$filteredHeaderLinks = array_filter($dom->find('header a'), function ($link) {
return $link->has('i') || ($link->has('span') && trim($link->text()));
});
$links = array_merge($links, $filteredHeaderLinks);
foreach ($links as $link) {
$span = $link->first('span');
$div = $link->first('div');
$spnimg = $link->first('img');
if ($span && !$div && !$spnimg && !$link->hasAttribute('aria-label')) {
$span_text = trim($span->text());
if ($span_text) {
$link->setAttribute('aria-label', $span_text);
}
}
$spanimg = $link->first('span img');
if ($spanimg && !$link->hasAttribute('aria-label') && (!$spanimg->hasAttribute('alt') || !$spanimg->hasAttribute('title'))) {
$link->setAttribute('aria-label', 'image');
}
if (!trim($link->text()) && !$link->hasAttribute('aria-label')) {
$child_elements = $link->find('*');
if (empty($child_elements)) {
$link->setAttribute('aria-label', 'link');
} else {
$icon = $link->first('i');
if ($icon) {
$link->setAttribute('aria-label', 'icon');
} else {
$img = $link->first('img');
if ($img && !$img->hasAttribute('alt')) {
$link->setAttribute('aria-label', 'image');
} else if ($img && (!$img->hasAttribute('alt') || !$img->hasAttribute('title'))) {
$link->setAttribute('aria-label', 'image');
}
$svg = $link->first('svg');
if ($svg) {
$link->setAttribute('aria-label', 'svg');
}
if (!$svg && !$img) {
$link->setAttribute('aria-label', 'link');
}
}
}
}
}
return $dom->html();
}
//Buttons do not have an accessible name
function easywpstuff_assesplus_add_aria_labels_to_buttons($html) {
$dom = new \DiDom\Document($html);
$buttons = $dom->find('button');
foreach ($buttons as $button) {
if (!trim($button->text()) && !$button->hasAttribute('aria-label')) {
$child_elements = $button->find('*');
if (empty($child_elements)) {
$button->setAttribute('aria-label', 'button');
} else {
$button->setAttribute('aria-label', 'button');
}
}
if (!$button->hasAttribute('aria-label')) {
$svg = $button->first('svg');
$path = $button->first('path');
$span = $button->first('span');
if ($svg && $path) {
$button->setAttribute('aria-label', 'button');
} elseif ($span) {
$span_text = trim($span->text());
if ($span_text) {
$button->setAttribute('aria-label', $span_text);
}
}
}
}
return $dom->html();
}
//[user-scalable="no"] is used in the element or the [maximum-scale] attribute is less than 5.
function easywpstuff_assesplus_modify_viewport_meta_tag($html) {
$dom = new \DiDom\Document($html);
$viewport_meta_tag = $dom->first('meta[name=viewport]');
if ($viewport_meta_tag) {
$content = $viewport_meta_tag->getAttribute('content');
$content = preg_replace('/,\s*user-scalable=(?:no|0)/', '', $content);
if (preg_match('/maximum-scale=([^,\s]*)/', $content, $matches)) {
$maximum_scale = (float) $matches[1];
if ($maximum_scale < 2) {
$content = preg_replace('/maximum-scale=[^,\s]*/', 'maximum-scale=2', $content);
}
} else {
$content .= ', maximum-scale=2';
}
$viewport_meta_tag->setAttribute('content', $content);
}
return $dom->html();
}
function easywpstuff_assesplus_add_aria_labels_to_headers($html) {
$dom = new \DiDom\Document($html);
foreach ($dom->find('header div') as $div) {
$role = $div->getAttribute('role');
if ($role === 'link' && !$div->hasAttribute('aria-label')) {
$div->setAttribute('aria-label', 'link');
}
if ($role === 'button' && !$div->hasAttribute('aria-label')) {
$div->setAttribute('aria-label', 'button');
}
if ($role === 'menuitem' && !$div->hasAttribute('aria-label')) {
$div->setAttribute('aria-label', 'menuitem');
}
}
return $dom->html();
}
// or