Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

False positive with inputs inside labels #106

Open
jeremyZX opened this issue Jan 22, 2020 · 1 comment
Open

False positive with inputs inside labels #106

jeremyZX opened this issue Jan 22, 2020 · 1 comment

Comments

@jeremyZX
Copy link

In the algorithm for Rule 204 - All input elements, type of "radio" have a label containing text, steps 7 and 8 state:

  1. The input element is contained by a label element
  2. Check if the label contains text

However, your detection for text inside the label is limited to text preceding the input. So the following element will validate as valid:

<label>
    My billing and shipping addresses are the same <input name="billing" type="radio" />
</label>

But this element will not:

<label>
    <input name="billing" type="radio" /> My billing and shipping addresses are the same
</label>

Since the WAI recommends that all left-to-right languages place text to the right of radio buttons and checkboxes, in LTR languages, this check results in a false positive.

The problem appears to be located in the associated_label_has_text function, which constructs a regular expression for the innerHTML of the label element, but omits a second capture group for text / HTML nodes to the right of the input element. So a rudimentary fix would look something like the following:

  public static function associated_label_has_text($e, $content_dom)
  {
      ...

      // 2. The element $e is contained by a "label" element
      if ($e->parent()->tag == "label")
      {
        $pattern = "/(.*)". preg_quote($e->outertext, '/') ."(.*)/";
        preg_match($pattern, $e->parent->innertext, $matches);
        if (strlen(trim($matches[1])) > 0) return true;
        if (strlen(trim($matches[2])) > 0) return true;
      }

      ...

      return false;
}
@cindyli
Copy link
Contributor

cindyli commented Jan 22, 2020

Thanks for reporting the issue in such a detail, @jeremyZX. We'll have a look at the next development cycle.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants