Skip to content

Object.keys() does not enumerate properties of window.location #528

@atnak

Description

@atnak

Problem in brief

Properties of window.location are not enumerated by methods such as Object.keys(), Object.entries(), Oject.getOwnPropertyNames() but are when location.__proto__ is queried instead.

Reproducing

Below code shows expected behaviour (tested in Chrome, Firefox, Edge) and that of HtmlUnit 2.66.

<!DOCTYPE html>
<html>
<head>
<script>

// Expected: [href,origin,protocol,host,hostname,port,pathname,search,hash,assign,replace,reload,toString]
// HtmlUnit 2.66: []
console.log(Object.keys(location));

// Expected: []
// HtmlUnit 2.66: [search,hostname,protocol,port,origin,host,href,hash,pathname,reload,replace,toString,assign]
console.log(Object.keys(location.__proto__))

</script>
</head>
<body>
</body>
</html>
  • FWIW, in IE it was enumerated in both cases.

More details

This practical consequence of this issue and the site it was discovered was using code like this:

let abc = {}
console.log(Object.assign(abc, location).href)

The issue is complicated by the fact this expected behaviour seems more or less specific to window.location. For other properties such as those under window.screen the way HtmlUnit behaves is the expected:

<!DOCTYPE html>
<html>
<head>
<script>

// Expected: []
// HtmlUnit 2.66: []
console.log(Object.keys(screen));

// Expected: [availWidth, availHeight, width, height, colorDepth, pixelDepth, availLeft, availTop, orientation, onchange, isExtended]
// HtmlUnit 2.66: [orientation, onchange, availLeft, availWidth, pixelDepth, top, left, availTop, width, colorDepth, mozOrientation, height, availHeight]
console.log(Object.keys(screen.__proto__))

</script>
</head>
<body>
</body>
</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions