hhvm / xhp-lib

Class libraries for XHP. XHP is a Hack feature that augments the syntax of the language such that XML document fragments become valid Hack expressions.
http://facebook.github.io/xhp-lib/
MIT License
1.38k stars 160 forks source link

input->:step doesn't allow 'any' #162

Closed HRMsimon closed 4 years ago

HRMsimon commented 8 years ago

The value for the step attribute on an input can be:

[...] the string any or a positive floating point number (source)

XHP only allows a float.

<input type="number" step="any" />
HRMsimon commented 8 years ago

Our workaround:

<?hh // strict

/**
 * A wrapper for :input
 *
 * :input doesn't support step="any", so we force it.
 * @see https://github.com/facebook/xhp-lib/issues/162
 */
class :ee:input extends :x:element {
  use XHPHelpers;

  attribute mixed step, :input;
  category %flow, %phrase, %interactive;

  public function render(): :input {
    $input = <input />;

    if ($this->isAttributeSet('step')) {
      $value = $this->:step;
      if ($value !== 'any' && !is_float($value)) {
        try {
          $value = XHPAttributeCoercion::CoerceToFloat($this, 'step', $value);
        } catch (XHPInvalidAttributeException $e) {
          throw new XHPInvalidAttributeException(
            $this,
            'float or "any"',
            'step',
            $value,
          );
        }
      }
      $input->forceAttribute('step', $value);
      $this->removeAttribute('step');
    }

    return $input;
  }
}
fredemmott commented 8 years ago

For my future reference: 'any' !== unspecified; unspecified uses defaults rather than allowing anything.