diff --git a/includes/rules.php b/includes/rules.php index 3499df05..baf9332e 100644 --- a/includes/rules.php +++ b/includes/rules.php @@ -104,13 +104,8 @@ 'info_url' => 'https://a11ychecker.com/help1980', 'slug' => 'text_justified', 'rule_type' => 'warning', - 'summary' => sprintf( - // translators: %s is <p>, %s is <div>, %s is <td>. - esc_html__( 'A Text Justified warning shows up in Accessibility Checker when the text on your post or page, either a %1$s, %2$s, or %3$s element, with more than 500 characters has been styled with a justified alignment (text-align:justify). To fix this warning, you will need to remove the justified styling from the specified text elements.', 'accessibility-checker' ), - '<p>', - '<div>', - '<td>' - ), + 'summary' => esc_html__( 'A Text Justified warning appears in Accessibility Checker when text with more than 200 characters on your post or page is styled with justified alignment (text-align:justify). To fix this warning, remove the justified styling from the specified text elements.', 'accessibility-checker' ), + 'ruleset' => 'js', ], [ 'title' => esc_html__( 'Link Opens New Window or Tab', 'accessibility-checker' ), diff --git a/includes/rules/text_justified.php b/includes/rules/text_justified.php deleted file mode 100644 index c43ebfc4..00000000 --- a/includes/rules/text_justified.php +++ /dev/null @@ -1,95 +0,0 @@ -test - */ - $dom = $content['html']; - $elements = $dom->find( '*' ); - if ( $elements ) { - foreach ( $elements as $element ) { - - if ( isset( $element ) && stristr( $element->getAttribute( 'style' ), 'text-align:' ) && '' !== $element->innertext ) { - - foreach ( $fontsearchpatterns as $pattern ) { - if ( preg_match_all( $pattern, $element, $matches, PREG_PATTERN_ORDER ) ) { - $matchsize = count( $matches ); - for ( $i = 0; $i < $matchsize; $i++ ) { - if ( isset( $matches[0][ $i ] ) && '' !== $matches[0][ $i ] ) { - $errors[] = $element; - } - } - } - } - } - } - } - - // check styles. - if ( $content['css_parsed'] ) { - $errors = array_merge( edac_css_justified_text_check( $content ), $errors ); - } - - return $errors; -} - -/** - * Check for text-align: justify in css files - * - * @param obj $content The object. - * @return array - */ -function edac_css_justified_text_check( $content ) { - - $dom = $content['html']; - $errors = []; - $error_code = ''; - $css_array = $content['css_parsed']; - - if ( $css_array ) { - foreach ( $css_array as $element => $rules ) { - if ( array_key_exists( 'text-align', $rules ) ) { - - // replace CSS variables. - $rules['text-align'] = edac_replace_css_variables( $rules['text-align'], $css_array ); - - $value = preg_replace( '/\d/', '', $rules['text-align'] ); - - if ( 'justify' === $value ) { - - $error_code = $element . '{ '; - foreach ( $rules as $key => $value ) { - $error_code .= $key . ': ' . $value . '; '; - } - $error_code .= '}'; - - $elements = $dom->find( $element ); - if ( $elements ) { - foreach ( $elements as $element ) { - $errors[] = $element->outertext . ' ' . $error_code; - } - } - } - } - } - } - return $errors; -} diff --git a/src/pageScanner/checks/text-is-justified.js b/src/pageScanner/checks/text-is-justified.js new file mode 100644 index 00000000..07c5b464 --- /dev/null +++ b/src/pageScanner/checks/text-is-justified.js @@ -0,0 +1,14 @@ +/** + * Axe core check for elements that are aligned to justify. + * + * @param {Node} node The node to evaluate. + * @return {boolean} True if the node is justified, false otherwise. + */ + +export default { + id: 'text_is_justified', + evaluate: ( node ) => { + const style = window.getComputedStyle( node ); + return style.textAlign.toLowerCase() === 'justify'; + }, +}; diff --git a/src/pageScanner/index.js b/src/pageScanner/index.js index fb8e9d22..526e12eb 100644 --- a/src/pageScanner/index.js +++ b/src/pageScanner/index.js @@ -6,6 +6,8 @@ import colorContrastFailure from './rules/color-contrast-failure'; import underlinedText from './rules/underlined-text'; import elementWithUnderline from './checks/element-with-underline'; import elementIsAUTag from './checks/element-is-u-tag'; +import textJustified from './rules/text-justified'; +import textIsJustified from './checks/text-is-justified'; //TODO: examples: //import customRule1 from './rules/custom-rule-1'; @@ -40,11 +42,13 @@ const scan = async ( //customRule1, colorContrastFailure, underlinedText, + textJustified, ], checks: [ //alwaysFail, elementIsAUTag, elementWithUnderline, + textIsJustified, ], iframes: false, @@ -57,6 +61,7 @@ const scan = async ( 'color_contrast_failure', 'underlined_text', 'meta-viewport', + textJustified.id, ], }, diff --git a/src/pageScanner/rules/text-justified.js b/src/pageScanner/rules/text-justified.js new file mode 100644 index 00000000..904a6785 --- /dev/null +++ b/src/pageScanner/rules/text-justified.js @@ -0,0 +1,21 @@ +/** + * Rule: Text Justified + * + * Text elements should not be justified because it interferes with readability. + */ + +const TEXT_JUSTIFIED_CHECK_THRESHOLD = 200; +export default { + id: 'text_justified', + selector: 'p, span, small, strong, b, i, em, h1, h2, h3, h4, h5, h6, a, label, button, th, td, li, div, blockquote, address, cite, q, s, sub, sup, u, del, caption, dt, dd, figcaption, summary, data, time', + matches: ( element ) => { + return element.textContent.trim().length >= TEXT_JUSTIFIED_CHECK_THRESHOLD; + }, + tags: [ 'wcag2aaa', 'wcag148', 'cat.text', 'custom' ], + metadata: { + description: 'Text elements inside containers should not be justified.', + }, + all: [], + any: [], + none: [ 'text_is_justified' ], +};