#5557·cheerio

data() setters skip all elements when the selection starts with text

Author: xu91102Created Sep 26, 2026Updated Sep 26, 2026

Calling a .data() setter on a mixed selection returned by .contents() skips every element when the first node is text.

javascript
const $ = load('<div>text<span></span></div>');
const contents = $('div').contents();
contents.data('foo', 'bar');
console.log($('span').data('foo')); // undefined, expected 'bar'

The object-map setter has the same problem. Both setters also return undefined instead of the selection in this case. Existing behavior and tests set data on all elements when the selection starts with an element; the leading text should simply be skipped.

Reproduced against main 0ab9a8e943bb86704ceb962770bc46647404ea45 on macOS / Node.js. Baseline attributes tests pass; a public API regression fails. Proposed fix: execute the existing setter iteration before applying the first-element check that is needed for getters. Correct the two setter docstrings which currently describe first-element-only behavior.

Investigated with OpenAI Codex assistance.