|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Lint rule: text contrast below WCAG AA. |
| 4 | + * |
| 5 | + * @package Saddle |
| 6 | + */ |
| 7 | + |
| 8 | +defined( 'ABSPATH' ) || exit; |
| 9 | + |
| 10 | +/** |
| 11 | + * Any text whose color fails WCAG AA against its effective background — the |
| 12 | + * node's own background, or the nearest ancestor's when the node paints none. |
| 13 | + * Generalizes button-contrast to all text; buttons stay that rule's job so a |
| 14 | + * bad button is flagged once, not twice. |
| 15 | + * |
| 16 | + * Thresholds follow WCAG AA: 4.5:1 for normal text, 3:1 for large text. |
| 17 | + * Headings and text at ≥24px count as large — rendered weight is unknowable |
| 18 | + * from the tree, so the rule under-flags rather than over-flags. Needs the |
| 19 | + * companion style accessor for the large-text call; without it the rule |
| 20 | + * stays silent (skip, never guess). |
| 21 | + */ |
| 22 | +class Saddle_Lint_Rule_Text_Contrast extends Saddle_Lint_Rule { |
| 23 | + |
| 24 | + /** |
| 25 | + * WCAG AA minimum for normal text. |
| 26 | + */ |
| 27 | + const MINIMUM = 4.5; |
| 28 | + |
| 29 | + /** |
| 30 | + * WCAG AA minimum for large text (≥24px, or any heading). |
| 31 | + */ |
| 32 | + const MINIMUM_LARGE = 3.0; |
| 33 | + |
| 34 | + /** |
| 35 | + * Font size, in px, from which text counts as large. |
| 36 | + */ |
| 37 | + const LARGE_PX = 24; |
| 38 | + |
| 39 | + /** |
| 40 | + * Rule id. |
| 41 | + * |
| 42 | + * @return string |
| 43 | + */ |
| 44 | + public function id() { |
| 45 | + return 'text-contrast'; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Flag text whose contrast against its effective background is below AA. |
| 50 | + * |
| 51 | + * @param array[] $nodes Flat node list. |
| 52 | + * @param Saddle_Lint_Accessor $accessor Builder accessor. |
| 53 | + * @return array[] |
| 54 | + */ |
| 55 | + public function check( array $nodes, Saddle_Lint_Accessor $accessor ) { |
| 56 | + if ( ! $accessor instanceof Saddle_Lint_Style_Accessor ) { |
| 57 | + return array(); |
| 58 | + } |
| 59 | + |
| 60 | + $by_address = array(); |
| 61 | + foreach ( $nodes as $node ) { |
| 62 | + $by_address[ $node['address'] ] = $node; |
| 63 | + } |
| 64 | + |
| 65 | + $violations = array(); |
| 66 | + foreach ( $nodes as $node ) { |
| 67 | + // Buttons are button-contrast's job — one finding per problem. |
| 68 | + if ( $accessor->is_button( $node['block'] ) ) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $text = $accessor->text_color( $node['block'] ); |
| 73 | + if ( null === $text ) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $background = $this->effective_background( $node, $by_address, $accessor ); |
| 78 | + $ratio = Saddle_Lint_Color::contrast( (string) $text, (string) $background ); |
| 79 | + if ( null === $ratio ) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $minimum = $this->is_large( $node, $accessor ) ? self::MINIMUM_LARGE : self::MINIMUM; |
| 84 | + if ( $ratio >= $minimum ) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $violations[] = $this->violation( |
| 89 | + $node['address'], |
| 90 | + self::SEVERITY_ERROR, |
| 91 | + sprintf( |
| 92 | + /* translators: 1: text color, 2: background color, 3: contrast ratio, 4: required minimum. */ |
| 93 | + __( 'Text %1$s on background %2$s has a contrast of %3$s:1 — below the WCAG AA minimum of %4$s:1.', 'saddle' ), |
| 94 | + $text, |
| 95 | + $background, |
| 96 | + number_format_i18n( $ratio, 2 ), |
| 97 | + number_format_i18n( $minimum, 1 ) |
| 98 | + ), |
| 99 | + __( 'Darken the text or lighten the background (or vice versa) so the pair reaches WCAG AA.', 'saddle' ) |
| 100 | + ); |
| 101 | + } |
| 102 | + return $violations; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * The background this node's text actually sits on: its own, or the |
| 107 | + * nearest ancestor's. Null when nothing up the chain resolves — an |
| 108 | + * unknown page background is never guessed at. |
| 109 | + * |
| 110 | + * @param array $node Node entry. |
| 111 | + * @param array[] $by_address Address → node map. |
| 112 | + * @param Saddle_Lint_Accessor $accessor Builder accessor. |
| 113 | + * @return string|null |
| 114 | + */ |
| 115 | + private function effective_background( array $node, array $by_address, Saddle_Lint_Accessor $accessor ) { |
| 116 | + $current = $node; |
| 117 | + while ( $current ) { |
| 118 | + $background = $accessor->background_color( $current['block'] ); |
| 119 | + if ( null !== $background ) { |
| 120 | + return $background; |
| 121 | + } |
| 122 | + $parent = $current['parent']; |
| 123 | + $current = null !== $parent && isset( $by_address[ $parent ] ) ? $by_address[ $parent ] : null; |
| 124 | + } |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Whether the node's text counts as WCAG "large": any heading, or a font |
| 130 | + * size of at least 24px. Only px sizes are compared — rem/em/clamp depend |
| 131 | + * on context the tree can't know, so they fall back to the normal-text |
| 132 | + * threshold (the stricter, safer direction... for the ratio; for the |
| 133 | + * threshold choice the heading call is the under-flagging one). |
| 134 | + * |
| 135 | + * @param array $node Node entry. |
| 136 | + * @param Saddle_Lint_Style_Accessor $accessor Style accessor. |
| 137 | + * @return bool |
| 138 | + */ |
| 139 | + private function is_large( array $node, Saddle_Lint_Style_Accessor $accessor ) { |
| 140 | + if ( null !== $accessor->heading_level( $node['block'] ) ) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + $size = $accessor->font_size( $node['block'] ); |
| 144 | + if ( is_string( $size ) && preg_match( '/^(\d+(?:\.\d+)?)px$/', trim( $size ), $m ) ) { |
| 145 | + return (float) $m[1] >= self::LARGE_PX; |
| 146 | + } |
| 147 | + return false; |
| 148 | + } |
| 149 | +} |
0 commit comments