heppokofrontend/jquery-accessible-name

View on GitHub
src/utils/aria-labelledby.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
/**
 * aria-labelledby属性経由で取得できるアクセシブルネームを返す
 * @param self - アクセシブルネームを取得したい要素
 * @return - アクセシブルネーム
 */
export const getLabelFromAriaLabelledBy = (self: HTMLElement | FormAssociated) => {
  const ariaLabelledBy = self.getAttribute('aria-labelledby');

  if (!ariaLabelledBy) {
    return null;
  }

  const idList = ariaLabelledBy.split(/\s/);
  const result = [];

  for (const id of idList) {
    const label = document.getElementById(id);

    if (label) {
      result.push((label.textContent || '').trim());
    }
  }

  return result.join(' ');
};