Skip to content

Standardize "invalid input" behavior #2134

@gibson042

Description

@gibson042

Many jQuery collection operations are only meaningful on elements or (somewhat less commonly) DOM nodes, but the collections can logically contain anything. Such operations should always assume valid input, potentially throwing exceptions when they encounter an invalid member—especially given the ease of manually cleaning like $collection.filter("*").

  • Single-member read (e.g., .attr( name ), .css( name ), .offset())
  • Whole-collection read (e.g., .hasClass( clazz ))
  • Whole-collection write (e.g., .attr( name, val ), .css( props ), .offset( n ), .addClass( clazz ))

Special consideration is warranted for single-member reads responding to empty collections. Current behavior usually returns undefined, which seems reasonable to standardize on (including changing those methods that return null, like .scrollTop() and .width()). There are cases where this collides with real output, but such collisions seem to be sane—$el.css( unknownProperty ), $el.prop( nonexistent ), $hasNoData.data(), etc.

Related:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions