Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#9762 closed bug (fixed)

Autocomplete: Normalization should handle null values

Reported by: johan-ludvigsson-stratsys Owned by: johan-ludvigsson-stratsys
Priority: minor Milestone: 1.11.0
Component: ui.autocomplete Version: 1.8.17
Keywords: Cc:
Blocked by: Blocking:

Description

In the _normalize function the following is returned:

return $.extend({
    label: item.label || item.value,
    value: item.value || item.label
}, item );

Which means if that if the properties label and value are present in item, they will overwrite the values regardless if they are undefined or not.

This could be fixed by reversing the order and use an empty object as target:

return $.extend({}, item, {
    label: item.label || item.value,
    value: item.value || item.label
});

So if the item is:

{ 
  label: 'Hello',
  value: null
}

it will be normalized correctly to:

{ 
  label: 'Hello',
  value: 'Hello'
}

Change History (7)

comment:1 Changed 3 years ago by tj.vantoll

Owner: set to johan-ludvigsson-stratsys
Status: newpending

Hi johan-ludvigsson-stratsys,

Thanks for taking the time to contribute to jQuery UI.

Could you give an example showing this behavior in action? I'm confused how users of the widget would run into this. You can use this test case as a starting point: http://jsfiddle.net/tj_vantoll/hmarM/.

comment:2 Changed 3 years ago by johan-ludvigsson-stratsys

Status: pendingnew

I modified it to give an example: http://jsfiddle.net/hmarM/19/

We mostly ran into this today when we were sending out JSON-objects to populate the suggest list. The main difference you will see, is what happens when you search and select "Hello" compared to "Jquery". In the first case, the input is cleared, while in the second case the input value is changed to "JQuery".

comment:3 Changed 3 years ago by Scott González

Status: newpending

Why are you sending a null value?

comment:4 Changed 3 years ago by johan-ludvigsson-stratsys

Status: pendingnew

It is being serialized from an object on server side, where we may or may not depending on events set a specific value for an item.

comment:5 Changed 3 years ago by Scott González

Status: newopen
Summary: Normalize in jquery autocomplete is faultyAutocomplete: Normalization should handle null values

This wasn't the intention of the normalization, but I don't see any harm in supporting this.

comment:6 Changed 3 years ago by Scott González

Resolution: fixed
Status: openclosed

Autocomplete: Normalize falsy values, not just missing values

Fixes #9762

Changeset: 113e9d0c2cc3f474da719721857c074c983c7157

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

Milestone: none1.11.0
Note: See TracTickets for help on using tickets.