mozilla / mozilla/readability

JSDOMParser: Element is missing a lang accessor

Open Beginner friendly
#1,026 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.5k
Forks
732
PR merge metrics
No merged PRs in 30d

Description

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

Readability itself is not broken by this. It reads the value with getAttribute("lang") (Readability.js:1071) 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.

Firefox for Android's Reader View hit exactly this. It caches a parsed document, reloads it through JSDOMParser, and read doc.documentElement.lang, which gave no language on the cached path. We worked around it with getAttribute, but the accessor is worth having so the parser matches the DOM.

See also: Bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=2062976

Suggested fix

Add the accessor next to the others:

  get lang() {                                                                                                                                                                        
    return this.getAttribute("lang") || "";                                                                                                                                           
  },                                                                                                                                                                                  
                                                                                                                                                                                      
  set lang(str) {                                                                                                                                                                     
    this.setAttribute("lang", str);                                                                                                                                                   
  },

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Find JSDOMParser's Element attribute accessors and inspect the surrounding implementations. Add the lang property behavior shown in the issue, then verify that documentElement.lang reflects the lang attribute and that assigning it updates the attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.