Closest parent conditional (selector question)

Closest parent conditional (selector question)

I have the following page structure:
<form action="/foo" method="POST">
  <div class="form">
 
    <button onclick="__nearest_parent();">Test</button>
  </div>
</form>


OR

<div class="form">
  <div class="form">
 
    <button onclick="__nearest_parent();">Test</button>
  </div>
</div>


The problem is that I want to be able to tell what the nearest parent (either class="form" or tag <form> is to the clicked element. The button may be buried down a couple levels deeper from the parent I wish to access also, but this parent will ALWAYS either have the class="form" OR be a form tag. Is there a selector combo I could use to determine the closest parent of this type? I was thinking $(this).parents()...but then I'm lost on how to restrict the search to either <form> or class="form" and then after that determine which is closer to the clicked element.

Thanks, sorry if I gave too much info, just want to make sure it's clear!