cheerio icon indicating copy to clipboard operation
cheerio copied to clipboard

:scope selector not working on root node

Open ShadyShenAli opened this issue 9 months ago • 1 comments

To select the direct child of the root node, I'm using :scope pseudo class in the selector. For example,

const html=`
<collection>
  <id value="a"/>
  <relatedCollections>
    <collection>
      <id value="a1"/>
    </collection>
  </relatedCollections>
</collection>
<collection>
  <id value="b"/>
  <relatedCollections>
    <collection>
      <id value="b1"/>
    </collection>
  </relatedCollections>
</collection>
`
let $ = cheerio.load(html,{xmlMode:true},false);

to get the root collection id, I'm using

  $(":scope> collection > id").map(function(){ return $(this).attr("value");}).get()

, which should return ["a","b"] however, it is currently returning empty result []

Is it a bug? Any solution? or any workaround?

Thanks and Best Regards, Shady

ShadyShenAli avatar Apr 03 '25 06:04 ShadyShenAli

a workaround has been found,

$(":root:is(collection) > id").map(function(){ return $(this).attr("value");}).get()

Although the original issue still persists.

ShadyShenAli avatar Apr 03 '25 07:04 ShadyShenAli