Re: [csswg-drafts] [css-selectors-4] Selector for element with another element as ancestor (#9130)

Yup, `a p` is what you want, and you can use `:is()` as @Loirooriol said if you need to impose multiple ancestor conditions that aren't related to each other.

Note tho that your first example:

```
<div>
  <a href="http://example.org/">
    <p>I match
  </a>
</div>
```

won't match the selector because, due to the way HTML parsing is defined, inline elements are auto-closed by a `p` and then reopened inside of them. That is, the DOM structure produced by that markup is:

```
DIV
├ A href="http://example.org/"
└ P
  └ A href="http://example.org/"
    └#text: I match
```

(Your second example works as intended, because `div` does *not* have that special behavior. So it is able to nest inside the `a` without a problem, and then the `p` sees that it has a `div` parent and acts normally as well. HTML parsing has a lot of funky quirks that it's developed over the decades that HTML has been in use.)

This might be the source of your confusion!

-- 
GitHub Notification of comment by tabatkins
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/9130#issuecomment-1661143278 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 1 August 2023 21:46:06 UTC