Skip to content

Commit

Permalink
Fix issue knockout#88 - optionsText should HTML-encode text content o…
Browse files Browse the repository at this point in the history
…f <OPTION> nodes
  • Loading branch information
SteveSanderson committed May 15, 2011
1 parent a64b88a commit f2ee36d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions spec/defaultBindingsBehaviors.js
Expand Up @@ -378,12 +378,12 @@ describe('Binding: Options', {
'Should accept function in optionsText param to display subproperties of the model values': function() {
var modelValues = new ko.observableArray([
{ name: 'bob', job: 'manager' },
{ name: 'frank', job: 'coder' }
{ name: 'frank', job: 'coder & tester' }
]);
testNode.innerHTML = "<select data-bind='options:myValues, optionsText: function (v) { return v[\"name\"] + \" (\" + v[\"job\"] + \")\"; }, optionsValue: \"id\"'><option>should be deleted</option></select>";
ko.applyBindings({ myValues: modelValues }, testNode);
var displayedText = ko.utils.arrayMap(testNode.childNodes[0].childNodes, function (node) { return node.innerHTML; });
value_of(displayedText).should_be(["bob (manager)", "frank (coder)"]);
var displayedText = ko.utils.arrayMap(testNode.childNodes[0].childNodes, function (node) { return node.innerText || node.textContent; });
value_of(displayedText).should_be(["bob (manager)", "frank (coder & tester)"]);
},

'Should update the SELECT node\'s options if the model changes': function () {
Expand Down
17 changes: 11 additions & 6 deletions src/binding/defaultBindings.js
Expand Up @@ -197,21 +197,26 @@ ko.bindingHandlers['options'] = {
}
for (var i = 0, j = value.length; i < j; i++) {
var option = document.createElement("OPTION");

// Apply a value to the option element
var optionValue = typeof allBindings['optionsValue'] == "string" ? value[i][allBindings['optionsValue']] : value[i];
optionValue = ko.utils.unwrapObservable(optionValue);
ko.selectExtensions.writeValue(option, optionValue);

// Pick some text to appear in the drop-down list for this data value
// Apply some text to the option element
var optionsTextValue = allBindings['optionsText'];
if (typeof optionsTextValue == "function")
optionText = optionsTextValue(value[i]); // Given a function; run it against the data value
else if (typeof optionsTextValue == "string")
optionText = value[i][optionsTextValue]; // Given a string; treat it as a property name on the data value
else
optionText = optionValue; // Given no optionsText arg; use the data value itself

optionValue = ko.utils.unwrapObservable(optionValue);
optionText = ko.utils.unwrapObservable(optionText);
ko.selectExtensions.writeValue(option, optionValue);
option.innerHTML = optionText.toString();
if ((optionText === null) || (optionText === undefined))
optionText = "";
optionText = ko.utils.unwrapObservable(optionText).toString();
typeof option.innerText == "string" ? option.innerText = optionText
: option.textContent = optionText;

element.appendChild(option);
}

Expand Down

0 comments on commit f2ee36d

Please sign in to comment.