Skip to content

even() and odd() are swapped relative to jQuery and the :even/:odd pseudo-classes #145

Open

Description

@gravitypdf-bot

Description

QueryPath's even() and odd() are the opposite of jQuery's. jQuery 3.5 added .even() and .odd() using zero-based indexes, so .even() keeps positions 0, 2, 4, … QueryPath's even() keeps positions 1, 3, 5, …:

$qp = qp('<?xml version="1.0"?><r><i id="a"/><i id="b"/><i id="c"/><i id="d"/><i id="e"/></r>', 'i');
Call jQuery 3.7.1 QueryPath main
even() a, c, e b, d
odd() b, d a, c, e

QueryPath's even() and odd() (added in 2.1) predate jQuery's. They count from one, and their DocBlocks say so: "Get the even elements, so counter-intuitively 1, 3, 5, etc." They also disagree with QueryPath's own :even and :odd pseudo-classes, which follow jQuery's zero-based counting (#66): filter(':even') keeps a, c, e.

Options

  1. Swap them to match jQuery and :even/:odd in the next major version. Add an upgrade note. Any code calling either method would silently get the other half of the set, so this can't go in a minor release.
  2. Keep them and point out the difference from filter(':even')/filter(':odd') in the DocBlocks.

Separately, they mishandle text nodes when counting. That is #143.

QueryPath version

main at 4d05c9e. The jQuery values come from jQuery 3.7.1 on jsdom.

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions