Detecting "runs" of DOM elements in jQuery
<p>What is the best way to detect runs of dom elements in jQuery?</p>

<p>For instance, if I have the following list of items</p>

<pre><code>&lt;ol&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
&lt;/ol&gt;
</code></pre>

<p>Say I want to grab all the <code>li.foo</code> elements and wrap them inside their own <code>&lt;ol&gt;</code> block (or any wrapper for that matter) to end up with something like.</p>

<pre><code>&lt;ol&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li&gt;&lt;ol&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;/ol&gt;&lt;/li&gt;
  &lt;li class="a"&gt;&lt;/li&gt;
  &lt;li&gt;&lt;ol&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
    &lt;li class="foo"&gt;&lt;/li&gt;
  &lt;/ol&gt;&lt;/li&gt;
&lt;/ol&gt;
</code></pre>

<p>As you can see from the example, I only want to wrap "runs" of <code>li.foo</code> dom elements (where there are 2 or more <code>li.foo</code> elements in succession.</p>

<p>I'm not sure of the best/most efficient way to accomplish this via jQuery (or just plain javascript for that matter).</p>
