function mdSelectOnFocusDirective($document, $timeout) {

  return {
    restrict: 'A',
    link: postLink
  };

  function postLink(scope, element, attr) {
    if (element[0].nodeName !== 'INPUT' && element[0].nodeName !== "TEXTAREA") return;

    var preventMouseUp = false;

    element
      .on('focus', onFocus)
      .on('mouseup', onMouseUp);

    scope.$on('$destroy', function() {
      element
        .off('focus', onFocus)
        .off('mouseup', onMouseUp);
    });

    function onFocus() {
      preventMouseUp = true;

      $timeout(function() {

        // Use HTMLInputElement#select to fix firefox select issues.
        // The debounce is here for Edge's sake, otherwise the selection doesn't work.
        // Since focus may already have been lost on the input (and because `select()`
        // will re-focus), make sure the element is still active before applying.
        if ($document[0].activeElement === element[0]) {
          element[0].select();
        }

        // This should be reset from inside the `focus`, because the event might
        // have originated from something different than a click, e.g. a keyboard event.
        preventMouseUp = false;
      }, 1, false);
    }

    // Prevents the default action of the first `mouseup` after a focus.
    // This is necessary, because browsers fire a `mouseup` right after the element
    // has been focused. In some browsers (Firefox in particular) this can clear the
    // selection. There are examples of the problem in issue #7487.
    function onMouseUp(event) {
      if (preventMouseUp) {
        event.preventDefault();
      }
    }
  }
}
@ngdoc directive
@name mdSelectOnFocus
@module material.components.input

@restrict A

@description
The `md-select-on-focus` directive allows you to automatically select the element's input text on focus.

<h3>Notes</h3>
- The use of `md-select-on-focus` is restricted to `<input>` and `<textarea>` elements.

@usage
<h3>Using with an Input</h3>
<hljs lang="html">

<md-input-container>
  <label>Auto Select</label>
  <input type="text" md-select-on-focus>
</md-input-container>
</hljs>

<h3>Using with a Textarea</h3>
<hljs lang="html">

<md-input-container>
  <label>Auto Select</label>
  <textarea md-select-on-focus>This text will be selected on focus.</textarea>
</md-input-container>

</hljs>