English

Lighthouseで、ボタンの表示の文字と名前が違うときの直し方

​​Lighthouse​axe-core,HTML,アクセシビリティ,トラブルシュート

Lighthouse の JSON レポートで label-content-name-mismatch(英語の表示は「Elements with visible text labels do not have matching accessible names.」)が不合格になっていたら、ボタンやリンクに付けた aria-label や aria-labelledby の名前が、画面に見えている文字を含んでいません。aria-label を外して表示の文字をそのまま名前にするか、名前の中に表示の文字をそのまま入れると、指摘は消えます。「送信」ボタンに aria-label="フォームを送る" を付けたり、「購入手続きへ」のリンクに別の説明を aria-label で付けたりしたときに出る指摘です。

私は検証環境で、表示と名前が違うボタンとリンクのページに Lighthouse を実行し、指摘が出ることと、直すと消えることを確かめました。あわせて、この監査は Lighthouse 13.6.0 では HTML レポートに表示されず、点数にも入らないことも分かりました。

補足

検証環境は Ubuntu 24.04、Node.js 22.23.5、Lighthouse 13.6.0(npm で入れた CLI。中で使われている axe-core は 4.14.0)、Playwright 1.63.0、ブラウザは Google Chrome for Testing 153 のヘッドレスです。アクセシビリティの項目だけ(--only-categories=accessibility)を、初期設定のモバイル向けの条件で実行しました。ページは手元で作り、127.0.0.1 から配信しました。Chrome の DevTools の Lighthouse パネル、PageSpeed Insights、実際のスクリーンリーダーや音声入力での操作は試していません。

この監査は HTML レポートには出ない

先に、見つけ方の注意です。Lighthouse 13.6.0 の設定ファイルでは、この監査は重み0で、hidden というグループに入っています。行の横のコメントには「experimental」(試験的)とあります。

text
585:        {id: 'label-content-name-mismatch', weight: 0, group: 'hidden'}, // Serious, experimental

HTML レポートを作る処理は、hidden のグループの監査を飛ばして表示します。検証でも、指摘が出るページの HTML レポートを Playwright で開いて調べたところ、表示された73個の監査の中に label-content-name-mismatch はなく、アクセシビリティの点数は100のままでした。

一方、判定そのものは毎回行われていて、結果は JSON のレポートの audits['label-content-name-mismatch'] に入ります。この記事の結果は、JSON から次のスクリプトで取り出したものです(Lighthouse の CLI は --output=json で JSON を書き出せます)。

js
// summary.mjs(点数や重みを出す行は省いています)
import fs from 'node:fs';
const r = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
const a = r.audits['label-content-name-mismatch'];
console.log('title:', a.title);
console.log('score:', a.score, 'scoreDisplayMode:', a.scoreDisplayMode);
for (const it of a.details?.items ?? []) {
  console.log('-', it.node.selector, '|', it.node.snippet);
  console.log('  ', it.node.explanation.replace(/\n/g, '\n   '));
}

日本語のレポート(--locale=ja)での名前は「表示されるテキストラベルの要素に、一致するユーザー補助機能用の名前がありません。」です。

指摘が出るページ

検証に使ったのは、次の3つの要素を並べたページです。どれも、画面に見える文字と、aria-label などで付けた名前が違います。

html
<!-- 1. 表示は「送信」、aria-label は別の言葉 -->
<button type="button" class="send" aria-label="フォームを送る">送信</button>
<!-- 2. リンクの表示と aria-label が違う -->
<a href="/checkout" class="checkout" aria-label="カートを見る">購入手続きへ</a>
<!-- 3. aria-labelledby で別の要素の文字を名前にしている -->
<span id="save-label" hidden>メモを保管</span>
<button type="button" class="save" aria-labelledby="save-label">保存</button>

このページを python3 -m http.server で配信し、次のように Lighthouse を実行しました。

bash
npx lighthouse http://127.0.0.1:8000/bad.html \
  --only-categories=accessibility \
  --output=json --output=html --output-path=./out/bad \
  --chrome-flags="--headless=new --no-sandbox" --quiet

JSON から取り出した結果は次のとおりです。3つとも指摘されました。それでも、アクセシビリティの点数は100です。

text
accessibility score: 100
label-content-name-mismatch weight: 0 group: hidden
title: Elements with visible text labels do not have matching accessible names.
score: 0 scoreDisplayMode: binary
- body > main > button.send | <button type="button" class="send" aria-label="フォームを送る">
   Fix any of the following:
     Text inside the element is not included in the accessible name
- body > main > a.checkout | <a href="/checkout" class="checkout" aria-label="カートを見る">
   Fix any of the following:
     Text inside the element is not included in the accessible name
- body > main > button.save | <button type="button" class="save" aria-labelledby="save-label">
   Fix any of the following:
     Text inside the element is not included in the accessible name
failed accessibility audits: label-content-name-mismatch(weight 0)

理由の「Text inside the element is not included in the accessible name」は、要素の中の文字が名前に含まれていない、という意味です。Playwright の ariaSnapshot() でページの構造を書き出すと、ボタンの名前は表示の「送信」ではなく「フォームを送る」になっていました。

text
- main:
  - heading "メモの編集" [level=1]
  - button "フォームを送る": 送信
  - link "カートを見る":
    - /url: /checkout
    - text: 購入手続きへ
  - button "メモを保管": 保存

この判定は、Lighthouse が中で使っている axe-core の label-content-name-mismatch のルールです。タグに wcag253 とあるとおり、WCAG 2.1 の達成基準 2.5.3(Label in Name)に対応しています。Lighthouse の説明文には、表示の文字と名前が合わないと、スクリーンリーダーを使う人が混乱することがある、と書かれています。

表示の文字を名前に入れる直し方

表示の文字が名前の中に入っていれば、指摘は消えます。

表示の文字と名前を比べた図。上は「送信」と表示されたボタンに aria-label="フォームを送る" が付いていて、送信が名前に入っていないので指摘される例。下は「購入手続きへ」と表示されたリンクに aria-label="購入手続きへ進む" が付いていて、表示の文字が名前の先頭に入っているので合格する例
表示の文字が名前の中にそのまま入っていれば合格する

いちばん簡単なのは、aria-label や aria-labelledby を外して、表示の文字をそのまま名前にする方法です。aria-label を残したいときは、名前に表示の文字をそのまま含めます。名前に補足を足したいときは、aria-label を使わず、画面に出さない文字をボタンの中の後ろに足す方法もあります。

html
<!-- 1. aria-label を外し、表示の文字をそのまま名前にする -->
<button type="button" class="send">送信</button>
<!-- 2. aria-label を、表示の文字から始まる言葉にする -->
<a href="/checkout" class="checkout" aria-label="購入手続きへ進む">購入手続きへ</a>
<!-- 3. 補足は画面に出さない文字で、表示の文字の後ろに足す -->
<button type="button" class="save">保存<span class="visually-hidden">(メモ1)</span></button>

visually-hidden は、文字を1ピクセルの枠に押し込めて、画面では見えなくする CSS です。

css
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

同じ条件で Lighthouse を実行し直すと、監査は合格になりました。

text
accessibility score: 100
label-content-name-mismatch weight: 0 group: hidden
title: Elements with visible text labels have matching accessible names.
score: 1 scoreDisplayMode: binary
failed accessibility audits: (none)

ariaSnapshot() で見た名前は、それぞれ「送信」「購入手続きへ進む」「保存 (メモ1)」です。

text
- main:
  - heading "メモの編集" [level=1]
  - button "送信"
  - link "購入手続きへ進む":
    - /url: /checkout
    - text: 購入手続きへ
  - button "保存 (メモ1)"

axe-core をページに直接入れて、このルールだけを実行すると、合格として数えられたのはリンクの1つだけでした。「送信」と「保存」のボタンは aria-label も aria-labelledby もないので、ルールの対象から外れます。対象になるのは、ボタンやリンクのように中の文字が名前になる部品で、画面に見える文字があり、aria-label か aria-labelledby が付いている要素です。

名前の途中に入っていても合格する

表示の文字が名前の先頭にない場合や、記号だけの表示の場合も試しました。

html
<button type="button" class="send" aria-label="フォームを送信">送信</button>
<button type="button" class="save" aria-label="上書き保存済みにする">保存</button>
<button type="button" class="close" aria-label="閉じる">×</button>
<button type="button" class="next" aria-label="Next page">Next</button>
<button type="button" class="prev" aria-label="Go back">Previous</button>

Lighthouse の JSON で指摘されたのは、「Previous」と表示して aria-label="Go back" を付けたボタンだけでした。

text
- body > main > button.prev | <button type="button" class="prev" aria-label="Go back">
   Fix any of the following:
     Text inside the element is not included in the accessible name

axe-core を直接実行した結果では、残りの4つは次のように分かれました。

text
incomplete .close | <button type="button" class="close" aria-label="閉じる">×</button>
passes     .send | <button type="button" class="send" aria-label="フォームを送信">送信</button>
passes     .save | <button type="button" class="save" aria-label="上書き保存済みにする">保存</button>
passes     .next | <button type="button" class="next" aria-label="Next page">Next</button>

「フォームを送信」のように、表示の文字が名前の途中や最後にあっても合格しました。axe-core の判定の処理は、表示の文字と名前を小文字にそろえ、ブラウザの Intl.Segmenter で単語に区切ってから、表示の単語の並びが名前の中にそのまま現れるかを比べています。同じ Chrome で Intl.Segmenter を実行すると、「上書き保存済みにする」は「上書き」「保存」「済み」「に」「する」に区切られました。「保存」が1つの単語として取り出されるので、合格になります。また、比べる前に、表示の文字と名前の両方から、かっこ書き(全角の()も含む)が取り除かれます。

「×」のような記号1文字の表示は、合っているかを判定できない「incomplete」(判定保留)になり、Lighthouse の指摘には入りませんでした。

2.5.3 に対応する ACT ルールは、かっこ書きを取り除く理由として、音声入力を使う人がかっこの中をふだん口にしないことを挙げています。名前を付けるときは、画面に見えている言葉を言い換えずに、そのまま名前に入れておくのが確実です。

​Posted by 86