Skip to content

Add a lang accessor to JSDOMParser's Element - #1028

Open
afonsojanu wants to merge 1 commit into
mozilla:mainfrom
afonsojanu:fix/jsdomparser-lang-accessor
Open

afonsojanu wants to merge 1 commit into
mozilla:mainfrom
afonsojanu:fix/jsdomparser-lang-accessor

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #1026.

JSDOMParser's Element reflects several HTML attributes as properties such as className, id, href, and srcset, but not lang. Code that reads doc.documentElement.lang gets undefined on a JSDOMParser document, even though the same document read through a real DOM would return the language tag.

Readability itself isn't affected by this, since it reads the value with getAttribute("lang") directly and exposes it as lang on the parse result. The problem hits consumers that take a document back from JSDOMParser and read the DOM property instead, which is exactly what happened with Firefox for Android's Reader View: it caches a parsed document, reloads it through JSDOMParser, and reads doc.documentElement.lang, which came back empty on the cached path.

Added the accessor next to the others, following the same getAttribute/setAttribute pattern the existing ones use, plus a test covering both the getter and setter. Ran the full test suite (1985 tests) and lint, both clean.

Element already reflects several HTML attributes as properties
(className, id, href, src, srcset) but not lang, so code that reads
doc.documentElement.lang gets undefined from a JSDOMParser document
even though the same document read through a real DOM would return
the language tag.

Readability itself isn't affected, since it reads the value with
getAttribute("lang") directly, but consumers that hand a parsed
document back to other code and expect normal DOM property access
run into this. Firefox for Android's Reader View hit exactly this
caching a parsed document and reloading it through JSDOMParser.

Fixes mozillaGH-1026.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSDOMParser: Element is missing a lang accessor

1 participant