Skip to content

JSDOMParser: Element is missing a lang accessor #1026

Description

@moyheen

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);                                                                                                                                                   
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions