css-select icon indicating copy to clipboard operation
css-select copied to clipboard

Adding support for the lang pseudo selector

Open chiragpat opened this issue 3 years ago • 1 comments

  • Currently css-select module throws since there is no definition for the lang pseudo selector present
  • Adding implementation for lang pseudo selector which simply delegates to the attribute equality selector

chiragpat avatar May 18 '22 23:05 chiragpat

@chiragpat Thanks for this PR! I am interested in getting this landed, but would want to be closer to the CSS spec.

The actual semantics of the :lang selector are quite a bit more complicated than this: https://drafts.csswg.org/selectors/#lang-pseudo

The main reason I never looked into implementing :lang() is that determining the content language of an element correctly is complicated: https://html.spec.whatwg.org/multipage/dom.html#attr-lang

One way around this would be an optional adapter method (the same way :hover, :visited, and :active are implemented). getContentLanguage(element) would defer to the document for getting the element's language. We would otherwise defer to the default implementation below. (The default implementation for the already-optional pseudo-classes is to always return false, which is in line with the spec. :lang() unfortunately does need an implementation to match the spec.)

Another way around the content language issue would be to have a documentLanguage option, which will be taken as the default value if no lang attribute is found.

As the default implementation, we should check all parent nodes for a lang attribute.

As :lang() values can be quoted, we would also have to strip quotes in css-what: https://github.com/fb55/css-what/blob/1416da67dbebd9d5f413cf86a084593639bccd99/src/parse.ts#L91

fb55 avatar May 19 '22 12:05 fb55

@fb55 I agree with your suggestion here. I wasn't aware of the intricacies involved in the :lang selector. I currently don't have time to support this, so going to go ahead and close the PR for now and will reopen/create a new one if I get time to land this.

chiragpat avatar Jan 03 '23 17:01 chiragpat