b-fuze / deno-dom

Browser DOM & HTML parser in Deno
https://jsr.io/@b-fuze/deno-dom
MIT License
423 stars 47 forks source link

Support :has pseudo class #139

Open cderv opened 1 year ago

cderv commented 1 year ago

This is about the pseudo class :has (https://developer.mozilla.org/en-US/docs/Web/CSS/:has#specifications) which currently return a DOMException

import { DOMParser, Element } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const doc = new DOMParser().parseFromString(
  `
  <h1>Hello World!</h1>
  <p>Hello from <a href="https://deno.land/">Deno!</a></p>
`,
  "text/html",
);
const p = doc.querySelector("p");
const p2 = doc.querySelector("p:has(a)");
// returns Uncaught DOMException

The above works in JS in browser console.

Possibly still early maybe as it requires some flags in some versions of browsers (https://caniuse.com/css-has) but still interesting feature.

b-fuze commented 1 year ago

I'll have to check if our upstream dependency NWSAPI has implemented it already; I think that they have and I need to vendor their newer release anyways since it fixes some long standing bugs (e.g. :scope)

oles commented 5 months ago

:has usage is becoming more and more common now - I'd love to see support for :has for deno-dom :heart:

b-fuze commented 5 months ago

Yeah, me too tbh. The problem currently is that nwsapi has bugs in its :has selector implementation... And while I'm interested in other engines like css-select from NPM I do not like the non trivial drop in performance that would result from that... So currently I'm thinking of forking nwsapi for deno-dom and fixing it myself and maybe the nwsapi authors can upstream my changes or fix it in the way they prefer when they get the time and then I can decommission the fork