Skip to content

Commit

Permalink
fix(dropdown): multiselect values encoding, removing label
Browse files Browse the repository at this point in the history
When a multiple dropdown (non select) was used and selected data has characters like & or ' set, there were stored entity encoded (returning into false positives when comparing later to their original values). Even worse this was only done for all previous selected values, the current to be added value was kept raw already. dropdowns using select tags do not store the selected data in an input field, so the issue does not happen there.

Original data should be kept, as it is already the case for dropdown made out of select tags. Also SUI does not do this.

Additionally this PR fixes a situation when a value has double quotes which led to a js error when trying to remove that label .
Double quotes in select menus are now also kept encoded instead of being removed to have the same behavior for select/non select dropdowns

Double quotes are always encoded (when text needs to be kept) or removed (everywhere else where they dont make sense like classnames) for security reasons, because all internal templates use them for HTML generation
  • Loading branch information
lubber-de authored Nov 16, 2020
1 parent 2093c46 commit 19806d4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,7 @@ $.fn.dropdown = function(parameters) {
: value
;
},
values: function() {
values: function(raw) {
var
value = module.get.value()
;
Expand All @@ -1939,7 +1939,7 @@ $.fn.dropdown = function(parameters) {
}
return ( !module.has.selectInput() && module.is.multiple() )
? (typeof value == 'string') // delimited string
? module.escape.htmlEntities(value).split(settings.delimiter)
? (raw ? value : module.escape.htmlEntities(value)).split(settings.delimiter)
: ''
: value
;
Expand Down Expand Up @@ -2970,7 +2970,7 @@ $.fn.dropdown = function(parameters) {
},
value: function(addedValue, addedText, $selectedItem) {
var
currentValue = module.get.values(),
currentValue = module.get.values(true),
newValue
;
if(module.has.value(addedValue)) {
Expand Down Expand Up @@ -3179,8 +3179,9 @@ $.fn.dropdown = function(parameters) {
},
label: function(value, shouldAnimate) {
var
escapedValue = module.escape.value(value),
$labels = $module.find(selector.label),
$removedLabel = $labels.filter('[data-' + metadata.value + '="' + module.escape.string(settings.ignoreCase ? value.toLowerCase() : value) +'"]')
$removedLabel = $labels.filter('[data-' + metadata.value + '="' + module.escape.string(settings.ignoreCase ? escapedValue.toLowerCase() : escapedValue) +'"]')
;
module.verbose('Removing label', $removedLabel);
$removedLabel.remove();
Expand Down Expand Up @@ -3329,7 +3330,7 @@ $.fn.dropdown = function(parameters) {
},
valueMatchingCase: function(value) {
var
values = module.get.values(),
values = module.get.values(true),
hasValue = Array.isArray(values)
? values && ($.inArray(value, values) !== -1)
: (values == value)
Expand All @@ -3341,7 +3342,7 @@ $.fn.dropdown = function(parameters) {
},
valueIgnoringCase: function(value) {
var
values = module.get.values(),
values = module.get.values(true),
hasValue = false
;
if(!Array.isArray(values)) {
Expand Down Expand Up @@ -4166,8 +4167,8 @@ $.fn.dropdown.settings = {

/* Templates */
$.fn.dropdown.settings.templates = {
deQuote: function(string) {
return String(string).replace(/"/g,"");
deQuote: function(string, encode) {
return String(string).replace(/"/g,encode ? """ : "");
},
escape: function(string, preserveHTML) {
if (preserveHTML){
Expand Down Expand Up @@ -4231,13 +4232,13 @@ $.fn.dropdown.settings.templates = {
if( itemType === 'item' ) {
var
maybeText = (option[fields.text])
? ' data-text="' + deQuote(option[fields.text]) + '"'
? ' data-text="' + deQuote(option[fields.text],true) + '"'
: '',
maybeDisabled = (option[fields.disabled])
? className.disabled+' '
: ''
;
html += '<div class="'+ maybeDisabled + (option[fields.class] ? deQuote(option[fields.class]) : className.item)+'" data-value="' + deQuote(option[fields.value]) + '"' + maybeText + '>';
html += '<div class="'+ maybeDisabled + (option[fields.class] ? deQuote(option[fields.class]) : className.item)+'" data-value="' + deQuote(option[fields.value],true) + '"' + maybeText + '>';
if(option[fields.image]) {
html += '<img class="'+(option[fields.imageClass] ? deQuote(option[fields.imageClass]) : className.image)+'" src="' + deQuote(option[fields.image]) + '">';
}
Expand Down

0 comments on commit 19806d4

Please sign in to comment.