Skip to content

forEach should accept callback's this value and pass this to the third argument of callback #1858

@ikeyan

Description

@ikeyan

Describe the bug
These are forEach from DOMTokenList and NodeList:

  • thisArg should be of type any.
  • the third argument of callback should be this, not an internal array.

https://github.com/capricorn86/happy-dom/blob/v18.0.1/packages/happy-dom/src/dom/DOMTokenList.ts#L226-L231
https://github.com/capricorn86/happy-dom/blob/v18.0.1/packages/happy-dom/src/nodes/node/NodeList.ts#L200C9-L205

These are forEach from Headers and FormData:

  • should accept thisArg?: any

https://github.com/capricorn86/happy-dom/blob/v18.0.1/packages/happy-dom/src/fetch/Headers.ts#L119-L123
https://github.com/capricorn86/happy-dom/blob/v18.0.1/packages/happy-dom/src/form-data/FormData.ts#L93-L97

To Reproduce

// node
const {FormData} = require('happy-dom')
var d = new FormData()
d.append('a', 'b')
const {strict: assert} = require('assert')
d.forEach(function(v, k, t) { console.log(this, v, k, t) }, {foo: 'foo'})
// FormData {} b a FormData {}
d.forEach(function(v, k, t) { assert.equal(this.foo, 'foo'); assert.equal(v, 'b'); assert.equal(k, 'a'); assert.equal(t, d); }, {foo: 'foo'})
// Uncaught AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
//   undefined !== 'foo'

Expected behavior
forEach should work in the same way as Array.prototype.forEach or Map.prototype.forEach.

Screenshots
Not applicable.

Device:
Device independent

Additional context
References to Web IDL spec. There are 3 definitions of forEach depending on the kind of interface but they are essentially the same

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions