function findPreviousMessage(parent, comment) {
          var prevNode = comment;
          var parentLookup = [];

          while (prevNode && prevNode !== parent) {
            var prevKey = prevNode.$$ngMessageNode;
            if (prevKey && prevKey.length) {
              return messages[prevKey];
            }

            // dive deeper into the DOM and examine its children for any ngMessage
            // comments that may be in an element that appears deeper in the list
            if (prevNode.childNodes.length && parentLookup.indexOf(prevNode) === -1) {
              parentLookup.push(prevNode);
              prevNode = prevNode.childNodes[prevNode.childNodes.length - 1];
            } else if (prevNode.previousSibling) {
              prevNode = prevNode.previousSibling;
            } else {
              prevNode = prevNode.parentNode;
              parentLookup.push(prevNode);
            }
          }
        }
@ngdoc directive
@module ngMessages
@name ngMessages
@restrict AE

@description
`ngMessages` is a directive that is designed to show and hide messages based on the state
of a key/value object that it listens on. The directive itself complements error message
reporting with the `ngModel` $error object (which stores a key/value state of validation errors).

`ngMessages` manages the state of internal messages within its container element. The internal
messages use the `ngMessage` directive and will be inserted/removed from the page depending
on if they're present within the key/value object. By default, only one message will be displayed
at a time and this depends on the prioritization of the messages within the template. (This can
be changed by using the `ng-messages-multiple` or `multiple` attribute on the directive container.)

A remote template can also be used (With {@link ngMessagesInclude}) to promote message
reusability and messages can also be overridden.

A default message can also be displayed when no `ngMessage` directive is inserted, using the
{@link ngMessageDefault} directive.

{@link module:ngMessages Click here} to learn more about `ngMessages` and `ngMessage`.

@usage
```html
<!-- using attribute directives -->
<ANY ng-messages="expression" role="alert">
  <ANY ng-message="stringValue">...</ANY>
  <ANY ng-message="stringValue1, stringValue2, ...">...</ANY>
  <ANY ng-message-exp="expressionValue">...</ANY>
  <ANY ng-message-default>...</ANY>
</ANY>

<!-- or by using element directives -->
<ng-messages for="expression" role="alert">
  <ng-message when="stringValue">...</ng-message>
  <ng-message when="stringValue1, stringValue2, ...">...</ng-message>
  <ng-message when-exp="expressionValue">...</ng-message>
  <ng-message-default>...</ng-message-default>
</ng-messages>
```

@param {string} ngMessages an AngularJS expression evaluating to a key/value object
                (this is typically the $error object on an ngModel instance).
@param {string=} ngMessagesMultiple|multiple when set, all messages will be displayed with true

@example
<example name="ngMessages-directive" module="ngMessagesExample"
         deps="angular-messages.js"
         animations="true" fixBase="true">
  <file name="index.html">
    <form name="myForm">
      <label>
        Enter your name:
        <input type="text"
               name="myName"
               ng-model="name"
               ng-minlength="5"
               ng-maxlength="20"
               required />
      </label>
      <pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>

      <div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
        <div ng-message="required">You did not enter a field</div>
        <div ng-message="minlength">Your field is too short</div>
        <div ng-message="maxlength">Your field is too long</div>
        <div ng-message-default>This field has an input error</div>
      </div>
    </form>
  </file>
  <file name="script.js">
    angular.module('ngMessagesExample', ['ngMessages']);
  </file>
</example>