Opened 10 years ago

Closed 10 years ago

Last modified 6 years ago

#5403 closed bug (notabug)

:data selector fails to return results when the data is set to empty string

Reported by: neonskimmer Owned by:
Priority: major Milestone: none
Component: ui.core Version: 1.8
Keywords: :data, selector Cc:
Blocked by: Blocking:

Description

The :data additional selector in jquery.ui.core.js does not match elements that contain empty strings as data.

HTML:

    <ol id="test">
      <li id="hasdata">hello</li>
      <li>foobar</li>
    </ol>

CODE:

$("#hasdata").data("empty", "");
$("#hasdata").data("hello", "oh hai");

console.debug('has data:', $("#hasdata").data() );
        
var cant_find = $("#test").find("li:data(empty)");
console.debug("selector fails to see the empty string in the data", cant_find);
        
var can_find = $("#test").find("li:data(hello)");
console.debug("selector is cool with a non empty string", can_find);

This is likely due to this in ui.core.js, line 181:

	data: function(elem, i, match) {
		return !!$.data(elem, match[3]);
	}

Because "empty string" evaluates to false, !!"" returns false, even though the key exists in the data. Should probably be something like !($.data(elem, match[3]) === undefined), depending on how $.data works.

Change History (5)

comment:1 Changed 10 years ago by Scott González

Resolution: invalid
Status: newclosed

This is working as intended.

comment:2 Changed 8 years ago by Scott González

Milestone: TBD

Milestone TBD deleted

comment:18 Changed 7 years ago by Scott González

#9053 is a duplicate of this ticket.

comment:19 Changed 6 years ago by Rycochet

This may be an old ticket, but it's a valid bug - it also hits on "false", "0", and anything else that evaluates as false - however jQuery.hasData() will in fact return true on all those cases, and only returns false when there isn't any data set.

As a rather obvious question - what happens when you want to store an array index in .data without modifying it...

comment:20 in reply to:  19 Changed 6 years ago by Scott González

Milestone: none

Replying to Rycochet:

This may be an old ticket, but it's a valid bug

It's not a valid bug because it's not designed for generic values. Feel free to file a feature request to change the intent of the selector.

As a rather obvious question - what happens when you want to store an array index in .data without modifying it...

I'm not sure what you mean by "store an array index in .data without modifying it." Without modifying what?

Note: See TracTickets for help on using tickets.