Skip to content

Instantly share code, notes, and snippets.

@AliceWonderMiscreations
Last active November 12, 2017 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliceWonderMiscreations/7533f448749cd4c13666739ab8643e4e to your computer and use it in GitHub Desktop.
Save AliceWonderMiscreations/7533f448749cd4c13666739ab8643e4e to your computer and use it in GitHub Desktop.
private function fixArticleLandmarks() {
$articlelist = $this->xmlBody->getElementsByTagName('article');
$nn = $articlelist->length;
if($nn == 0) {
return;
}
$x = new DOMXPath($this->dom);
for($ii = 0; $ii < $nn; $ii++) {
$arnode = $articlelist->item($ii);
$nodelist = $arnode->getElementsByTagName('aside');
$n = $nodelist->length;
for($i = 0; $i < $n; $i++) {
$aside = $nodelist->item($i);
if(! $aside->hasAttribute('role')) {
$aside->setAttribute('role', 'region');
if(! $aside->hasAttribute('aria-labelledby')) {
if(! $aside->hasAttribute('aria-label')) {
$count = 0;
foreach($aside->childNodes as $child) {
if(!($child instanceof \DomText)) {
$count++;
}
}
if($count != 0) {
$first = $x->query('*', $aside)->item(0);
$tagname = $first->tagName;
if(in_array($tagname, array ('h2', 'h3', 'h4', 'h5', 'h6'))) {
$labelid = "aside: " . trim($first->textContent);
$aside->setAttribute('aria-label', $labelid);
}
}
}
}
}
} // end for $i loop
$nodelist = $arnode->getElementsByTagName('section');
$n = $nodelist->length;
for($i = 0; $i < $n; $i++) {
$section = $nodelist->item($i);
if(! $section->hasAttribute('aria-labelledby')) {
if(! $section->hasAttribute('aria-label')) {
$count = 0;
foreach($section->childNodes as $child) {
if(!($child instanceof \DomText)) {
$count++;
}
}
if($count != 0) {
$first = $x->query('*', $section)->item(0);
$tagname = $first->tagName;
if(in_array($tagname, array ('h2', 'h3', 'h4', 'h5', 'h6'))) {
$labelid = "section: " . trim($first->textContent);
$section->setAttribute('aria-label', $labelid);
}
}
}
}
} //end of for $i loop
$nodelist = $arnode->getElementsByTagName('details');
$n = $nodelist->length;
for($i = 0; $i < $n; $i++) {
$details = $nodelist->item($i);
if($details->hasAttribute('class')) {
$class = $details->getAttribute('class');
if(strcmp($class, 'toc') == 0) {
$details->setAttribute('role', 'navigation');
if(! $details->hasAttribute('aria-labelledby')) {
if(! $details->hasAttribute('aria-label')) {
$details->setAttribute('aria-label', 'Table of Contents');
}
}
}
}
} // end of for $i loop
} // end for for $ii loop
} // end of function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment