Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It seems to be unclear how to get an element's lang through inheritance #37680

Open
juanlanus opened this issue Jan 16, 2025 · 2 comments
Open
Labels
Content:WebAPI Web API docs

Comments

@juanlanus
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang

What specific section or headline is this issue about?

Inheritance

What information was incorrect, unhelpful, or incomplete?

... or I didn't understand.

It reads: "If an element has no lang attribute, it will inherit the lang value set on its parent node, which in turn may inherit it from its parent, and so on."
It's not happening, at least the way I expect it to.
Descendants of an element with lang return a null lang property.
I understand that #text nodes have no lang, and this seems to cut the inheritance chain.
So I tried Element.closest('[lang]') and it also failed.

My nodes descend from a fragment, created from a page with lang at its . The fragment didn't come out with the page's lang, so I added it.
The descendants of the fragment don't seem to inherit its lang even after I brute-forced it.

StackOverflow has a number of developers wandering like zombies searching for the light.
I (one of those zombies) finally wrote a function to walk the hierarchy to do what I believed that closest() should do.

This is Chrome version 132.0.6834.83 (Official Build) (64-bit) in Ubuntu 22.04.5 LTS

What did you expect to see?

I expected the descendants elements of one having lang set, to proudly return their ancestor's lang.
Or, at least, closest to get it.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

Select text and press the button

Cannot upload html & js or 7z files :-(

const getNodeLang = ( node ) => {
  let lang = node.lang;
  while( ! lang && node.parentNode ){
    if( node.parentNode ){
      node = node.parentNode;
    } else {
      return null;
    }
    lang = node.lang;
  }
  return lang;
}

MDN metadata

Page report details
@juanlanus juanlanus added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 16, 2025
@github-actions github-actions bot added Content:Glossary Glossary entries Content:HTML Hypertext Markup Language docs labels Jan 16, 2025
@Josh-Cena
Copy link
Member

Descendants inherit the behavior of the lang, such as with assistive technologies or translation softwares. Descendants don't inherit the property. You can't programmatically retrieve this property via HTMLElement#lang, because that property simply reflects the HTML content attribute, as specified. We should mention this on https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/lang, as we already do for https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dir.

@Josh-Cena Josh-Cena added Content:WebAPI Web API docs and removed Content:HTML Hypertext Markup Language docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. Content:Glossary Glossary entries labels Jan 16, 2025
@juanlanus
Copy link
Author

juanlanus commented Jan 16, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs
Projects
None yet
Development

No branches or pull requests

2 participants