Skip to content

FileList fails to enumerate with console.log #37993

Open

Description

@yezhizhen

Describe the bug:
Try below.

To Reproduce:
Select two files. Click button.

<!DOCTYPE html>
<html>
<body>
    <input type="file" multiple>
    <button onclick="logFiles()">Log Files</button>
    
    <script>
        function logFiles() {
            const files = document.querySelector('input').files;
            console.log(files[0].name);
            console.log(files);
            return files;
        }
    </script>
</body>
</html>

Output is:

Cargo.lock
FileList {
  "0": {},
  "1": {}
}

Activity

  1. added
    C-has-manual-testcase
    on Jul 11, 2025
  2. yezhizhen commented on Jul 11, 2025

    @yezhizhen
    MemberAuthor
    // FileList.webidl
    [Exposed=(Window,Worker)]
    interface FileList {
      getter File? item(unsigned long index);
      readonly attribute unsigned long length;
    };
  3. added
    A-content/script
    on Jul 11, 2025
  4. changed the title [-]`FileList` has empty print with `console.log`[/-] [+]`FileList` fails to enumerate with `console.log`[/+] on Jul 11, 2025
  5. jdm commented on Jul 11, 2025

    @jdm
    Member

    This behaviour comes from

    fn maybe_stringify_dom_object(cx: JSContext, value: HandleValue) -> Option<DOMString> {
    . I haven't figured out a better system for deciding how to stringify arbitrary DOM interfaces yet.

  6. yezhizhen commented on Jul 11, 2025

    @yezhizhen
    MemberAuthor

    Does #37992 also relies on this?

  7. jdm commented on Jul 11, 2025

    @jdm
    Member

    Does #37992 also relies on this?

    It doesn't call the same code; returning a FileList with webdriver goes through

    } else if object_has_to_json_property(cx, global_scope, object.handle()) {
    let name = CString::new("toJSON").unwrap();
    rooted!(in(cx) let mut value = UndefinedValue());
    if JS_CallFunctionName(
    cx,
    object.handle(),
    name.as_ptr(),
    &HandleValueArray::empty(),
    value.handle_mut(),
    ) {
    Ok(jsval_to_webdriver_inner(
    cx,
    global_scope,
    value.handle(),
    seen,
    )?)
    , so it returns an array containing the result of calling toJSON on each member of the FileList.

  8. jdm commented on Jul 11, 2025

    @jdm
    Member

    Actually that's incorrect; File/Blob don't have a toJSON, so we fall through to

    let mut result = HashMap::new();
    let mut ids = IdVector::new(cx);
    if !GetPropertyKeys(
    cx,
    object.handle().into(),
    jsapi::JSITER_OWNONLY,
    ids.handle_mut(),
    ) {
    return Err(WebDriverJSError::JSError);
    }
    for id in ids.iter() {
    rooted!(in(cx) let id = *id);
    rooted!(in(cx) let mut desc = PropertyDescriptor::default());
    let mut is_none = false;
    if !JS_GetOwnPropertyDescriptorById(
    cx,
    object.handle().into(),
    id.handle().into(),
    desc.handle_mut().into(),
    &mut is_none,
    ) {
    return Err(WebDriverJSError::JSError);
    }
    rooted!(in(cx) let mut property = UndefinedValue());
    if !JS_GetPropertyById(
    cx,
    object.handle().into(),
    id.handle().into(),
    property.handle_mut().into(),
    ) {
    return Err(WebDriverJSError::JSError);
    }
    if !property.is_undefined() {
    let Some(name) = jsid_to_string(cx, id.handle()) else {
    return Err(WebDriverJSError::JSError);
    };
    if let Ok(value) =
    jsval_to_webdriver_inner(cx, global_scope, property.handle(), seen)
    {
    result.insert(name.into(), value);
    } else {
    return Err(WebDriverJSError::JSError);
    }
    which just tries to obtain each property on the File/Blob object. Since there are no properties defined on the object (only on the prototype), we serialize it as {}. This is exactly the problem that the current implementation of console.log attempts to avoid, since DOM objects generally have more interesting properties on their prototype and JSON.stringify shows those.

  9. jdm commented on Jul 11, 2025

    @jdm
    Member

    https://w3c.github.io/webdriver/#dfn-clone-an-object says For each enumerable property in value, run the following substeps: , and I don't have a good sense for whether that's supposed to only be own properties or all properties.

  10. jdm commented on Jul 11, 2025

    @jdm
    Member

    You can try changing

    jsapi::JSITER_OWNONLY,
    to 0 and see what happens!

  11. yezhizhen commented on Jul 12, 2025

    @yezhizhen
    MemberAuthor

    I don't think I can work on this before middle of next week. Anyone is welcome to try :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/bindingsA-content/scriptA-devtoolsC-has-manual-testcase

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions