Note that there are some explanatory texts on larger screens.

plurals
  1. PO:has CSS pseudo class in Nokogiri
    primarykey
    data
    text
    <p>I'm looking for the pseudoclass <code>:has</code> in <a href="http://nokogiri.org/Nokogiri/XML/Node.html" rel="nofollow">Nokogiri</a>. It should work just like jQuery's <a href="http://api.jquery.com/has-selector/" rel="nofollow"><code>has</code> selector</a>.</p> <p>For example:</p> <pre><code>&lt;li&gt;&lt;h1&gt;&lt;a href="dfd"&gt;ex1&lt;/a&gt;&lt;/h1&gt;&lt;span class="string"&gt;sdfsdf&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;h1&gt;&lt;a href="dsfsdf"&gt;ex2&lt;/a&gt;&lt;/h1&gt;&lt;span class="string"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;h1&gt;&lt;a href="sdfd"&gt;ex3&lt;/a&gt;&lt;/h1&gt;&lt;/li&gt; </code></pre> <p>The CSS selector should return only the first link, the one with the not-empty <code>span.string</code> sibling.</p> <p>In jQuery this selector works well:</p> <pre><code>$('li:has(span.string:not(:empty))&gt;h1&gt;a') </code></pre> <p>but not in Nokogiri:</p> <pre><code>Nokogiri::HTML(html_source).css('li:has(span.string:not(:empty))&gt;h1&gt;a') </code></pre> <p><code>:not</code> and <code>:empty</code> works well, but not <code>:has</code>.</p> <hr> <ol> <li>Is there any documentation for CSS selectors in Nokogiri?</li> <li>Maybe someone can write a custom <code>:has</code> pseudo class? Here is an <a href="http://nokogiri.org/Nokogiri/XML/Node.html#method-i-css" rel="nofollow">example</a> how to write a <code>:regexp</code> selector.</li> <li>Optionally I can use XPath. How do I write XPath for <code>li:has(span.string:not(:empty))&gt;h1&gt;a</code>?</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload