tags that have none, keyed by file stem. * * @param string $html HTML to edit. * @param array $alts Map of alexibility_fixes_wp_stem() => alt text ('' marks the image decorative). * @param array $replace Stems whose empty alt="" is replaced too (an image that is a link's only content). * @return array [ edited html, number of images changed ] */ function alexibility_fixes_add_img_alt( $html, $alts, $replace = array() ) { if ( ! $alts || false === stripos( $html, 'next_tag( 'img' ) ) { $src = $p->get_attribute( 'src' ); if ( ! is_string( $src ) ) { continue; } $stem = alexibility_fixes_wp_stem( alexibility_fixes_file_name_of( $src ) ); if ( ! alexibility_fixes_alt_missing( $p->get_attribute( 'alt' ), in_array( $stem, $replace, true ) ) ) { continue; } if ( array_key_exists( $stem, $alts ) ) { $p->set_attribute( 'alt', $alts[ $stem ] ); ++$count; } } return array( $p->get_updated_html(), $count ); } /** Whether an image's alt needs setting: none at all, or an empty one when the fix replaces empty alts. */ function alexibility_fixes_alt_missing( $alt, $replace_empty ) { return null === $alt || ( $replace_empty && is_string( $alt ) && '' === trim( $alt ) ); } /** * Hides graphics that only repeat their control's name: aria-hidden="true" focusable="false" on * elements matching $criteria that aren't hidden yet and have no name of their own. * * @return array [ edited html, number of elements changed ] */ function alexibility_fixes_hide_graphic( $html, $tag, $criteria ) { if ( ! $criteria || false === stripos( $html, '<' . $tag ) ) { return array( $html, 0 ); } if ( ! preg_match_all( '/<' . preg_quote( $tag, '/' ) . '\b[^>]*>/i', $html, $found, PREG_OFFSET_CAPTURE ) ) { return array( $html, 0 ); } $out = ''; $last = 0; $count = 0; foreach ( $found[0] as $match ) { list( $open, $offset ) = $match; $ok = ! alexibility_fixes_has_attr( $open, 'aria-hidden' ) && ! alexibility_fixes_has_attr( $open, 'aria-label' ) && ! alexibility_fixes_has_attr( $open, 'aria-labelledby' ); foreach ( $criteria as $key => $value ) { $ok = $ok && alexibility_fixes_get_attr( $open, $key ) === $value; } if ( $ok ) { $end = $offset + strlen( $open ); $close = stripos( $html, ']/i', substr( $html, $end, $close - $end ) ); } if ( ! $ok ) { continue; } $hidden = preg_replace( '/^<' . preg_quote( $tag, '/' ) . '\b/i', '$0 aria-hidden="true" focusable="false"', $open, 1 ); $out .= substr( $html, $last, $offset - $last ) . $hidden; $last = $offset + strlen( $open ); ++$count; } return array( $out . substr( $html, $last ), $count ); } /** Attributes that identify one element, best first; falls back to every attribute except style. */ function alexibility_fixes_match_criteria( $attrs ) { $picked = array(); foreach ( array( 'id', 'name', 'href', 'src', 'for', 'data-testid', 'action', 'aria-controls' ) as $key ) { if ( isset( $attrs[ $key ] ) && '' !== $attrs[ $key ] ) { $picked[ $key ] = $attrs[ $key ]; } } if ( $picked ) { return $picked; } unset( $attrs['style'] ); return $attrs; } /** Value of a quoted attribute in an opening tag, or null. */ function alexibility_fixes_get_attr( $tag, $name ) { if ( ! preg_match( '/\s' . preg_quote( $name, '/' ) . '\s*=\s*(?:"([^"]*)"|\'([^\']*)\')/i', $tag, $m ) ) { return null; } return isset( $m[2] ) ? $m[2] : $m[1]; } function alexibility_fixes_has_attr( $tag, $name ) { return 1 === preg_match( '/\s' . preg_quote( $name, '/' ) . '(?:\s*=|[\s\/>])/i', $tag ); } /** * Adds aria-label to elements matching $criteria that have no accessible name yet. Links and buttons * that already contain text are left alone. * * @return array [ edited html, number of elements changed ] */ function alexibility_fixes_add_aria_label( $html, $tag, $criteria, $label ) { if ( ! $criteria || '' === $label || false === stripos( $html, '<' . $tag ) ) { return array( $html, 0 ); } if ( ! preg_match_all( '/<' . preg_quote( $tag, '/' ) . '\b[^>]*>/i', $html, $found, PREG_OFFSET_CAPTURE ) ) { return array( $html, 0 ); } $out = ''; $last = 0; $count = 0; foreach ( $found[0] as $match ) { list( $open, $offset ) = $match; $ok = ! alexibility_fixes_has_attr( $open, 'aria-label' ) && ! alexibility_fixes_has_attr( $open, 'aria-labelledby' ) && ! alexibility_fixes_has_attr( $open, 'title' ); foreach ( $criteria as $key => $value ) { $ok = $ok && alexibility_fixes_get_attr( $open, $key ) === $value; } if ( $ok && in_array( $tag, array( 'a', 'button' ), true ) ) { $end = $offset + strlen( $open ); $close = stripos( $html, 'get_attribute( 'class' ); $classes = is_string( $class ) ? preg_split( '/\s+/', trim( $class ) ) : array(); foreach ( $match['classes'] as $wanted ) { if ( ! in_array( $wanted, $classes, true ) ) { return false; } } foreach ( $match['attrs'] as $key => $value ) { if ( $p->get_attribute( $key ) !== $value ) { return false; } } foreach ( $match['has'] as $key ) { if ( null === $p->get_attribute( $key ) ) { return false; } } return true; } /** * Makes an attr-repair's change on every element it matches: removes and sets attributes and adds style * properties, as changeAttrs() does in src/fixes/attrs.ts. Values were validated on upload. * * @return array [ edited html, number of elements changed ] */ function alexibility_fixes_change_attrs( $html, $fix ) { $match = $fix['match']; if ( false === stripos( $html, '<' . $match['tag'] ) ) { return array( $html, 0 ); } $p = new WP_HTML_Tag_Processor( $html ); $count = 0; while ( $p->next_tag( $match['tag'] ) ) { if ( ! alexibility_fixes_tag_matches( $p, $match ) ) { continue; } foreach ( $fix['remove'] as $name ) { $p->remove_attribute( $name ); } foreach ( $fix['set'] as $name => $value ) { $p->set_attribute( $name, $value ); } if ( $fix['style'] ) { $add = array(); foreach ( $fix['style'] as $property => $value ) { $add[] = $property . ':' . $value; } $old = $p->get_attribute( 'style' ); $old = is_string( $old ) ? rtrim( trim( $old ), '; ' ) : ''; $p->set_attribute( 'style', ( '' === $old ? '' : $old . ';' ) . implode( ';', $add ) ); } ++$count; } return array( $p->get_updated_html(), $count ); } /** An attr-repair's match as a CSS selector, for people: a.fl-tabs-label[role="button"][aria-selected]. */ function alexibility_fixes_match_selector( $match ) { $selector = $match['tag']; foreach ( $match['classes'] as $class ) { $selector .= '.' . $class; } foreach ( $match['attrs'] as $key => $value ) { $selector .= '[' . $key . '="' . $value . '"]'; } foreach ( $match['has'] as $key ) { $selector .= '[' . $key . ']'; } return $selector; } /** Hidden until focused, then pinned top-left above everything. */ function alexibility_fixes_skip_link_css() { return '.alexibility-fixes-skip-link{position:absolute;left:-10000px;top:auto;width:1px;height:1px;overflow:hidden}' . '.alexibility-fixes-skip-link:focus{position:fixed;left:8px;top:8px;width:auto;height:auto;overflow:visible;z-index:100000;padding:8px 16px;background:#ffffff;color:#000000;outline:3px solid #000000}'; } /** CSS for the approved contrast, focus, skip-link and repair fixes. Values were validated on upload. */ function alexibility_fixes_fix_css( $fixes ) { $rules = array(); foreach ( $fixes as $fix ) { if ( 'contrast-color' === $fix['kind'] && $fix['selectors'] ) { $rules[] = implode( ',', $fix['selectors'] ) . '{color:' . $fix['replacement'] . ' !important}'; } elseif ( 'focus-visible' === $fix['kind'] ) { $rules[] = $fix['selector'] . ':focus-visible{outline:3px solid currentColor !important;outline-offset:2px !important}'; } elseif ( 'skip-link' === $fix['kind'] ) { $rules[] = alexibility_fixes_skip_link_css(); } elseif ( 'css-repair' === $fix['kind'] ) { $declarations = array(); foreach ( $fix['css'] as $property => $value ) { $declarations[] = $property . ':' . $value . ' !important'; } $rules[] = $fix['selector'] . '{' . implode( ';', $declarations ) . '}'; } } // Belt and braces: nothing in a style element may close it. return str_replace( '