Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,9 @@ webidl.interfaceConverter = function (i) {

webidl.dictionaryConverter = function (converters) {
return (dictionary, prefix, argument) => {
const type = webidl.util.Type(dictionary)
const dict = {}

if (type === 'Null' || type === 'Undefined') {
return dict
} else if (type !== 'Object') {
if (dictionary != null && webidl.util.Type(dictionary) !== 'Object') {
throw webidl.errors.exception({
header: prefix,
message: `Expected ${dictionary} to be one of: Null, Undefined, Object.`
Expand All @@ -370,21 +367,21 @@ webidl.dictionaryConverter = function (converters) {
const { key, defaultValue, required, converter } = options

if (required === true) {
if (!Object.hasOwn(dictionary, key)) {
if (dictionary == null || !Object.hasOwn(dictionary, key)) {
throw webidl.errors.exception({
header: prefix,
message: `Missing required key "${key}".`
})
}
}

let value = dictionary[key]
const hasDefault = Object.hasOwn(options, 'defaultValue')
let value = dictionary?.[key]
const hasDefault = defaultValue !== undefined

// Only use defaultValue if value is undefined and
// a defaultValue options was provided.
if (hasDefault && value !== null) {
value ??= defaultValue()
if (hasDefault && value === undefined) {
value = defaultValue()
}

// A key can be optional and have no default value.
Expand Down
45 changes: 45 additions & 0 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,51 @@ describe('webidl.dictionaryConverter', () => {
converter({ Key: 'this key was required!' }, 'converter', 'converter')
})
})

test('null and undefined still populates defaultValue(s)', () => {
const dict = webidl.dictionaryConverter([
{
key: 'key',
converter: webidl.converters.any,
defaultValue: () => 3
}
])

assert.deepStrictEqual(dict(null), { key: 3 })
assert.deepStrictEqual(dict(undefined), { key: 3 })
})

test('null and undefined throw a webidl TypeError with a required key', () => {
const dict = webidl.dictionaryConverter([
{
key: 'key',
converter: webidl.converters.any,
required: true
}
])

assert.throws(() => dict(null, 'prefix'), new TypeError('prefix: Missing required key "key".'))
assert.throws(() => dict(undefined, 'prefix'), new TypeError('prefix: Missing required key "key".'))
})

test('Object type works for functions and regex (etc.)', () => {
const dict = webidl.dictionaryConverter([
{
key: 'key',
converter: webidl.converters.any,
required: true
}
])

function obj () {}
obj.key = 1

const obj2 = / /
obj2.key = 1

assert.deepStrictEqual(dict(obj), { key: 1 })
assert.deepStrictEqual(dict(obj2), { key: 1 })
})
})

test('ArrayBuffer', () => {
Expand Down
Loading