Skip to content

Commit 0f7e406

Browse files
committedFeb 23, 2021
Fix character escapes in combobox options
1 parent 767311e commit 0f7e406

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
 

‎packages/controls/src/widget_string.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,13 @@ export class ComboboxView extends TextView {
531531
this.isInitialRender = false;
532532

533533
const opts = this.model.get('options') as string[];
534-
const optLines = opts.map(o => {
535-
return `<option value="${o}"></option>`;
536-
});
537-
this.datalist.innerHTML = optLines.join('\n');
534+
const optionFragment = document.createDocumentFragment();
535+
for (const v of opts) {
536+
const o = document.createElement('option');
537+
o.value = v;
538+
optionFragment.appendChild(o);
539+
}
540+
this.datalist.appendChild(optionFragment);
538541
}
539542

540543
isValid(value: string): boolean {

‎packages/controls/test/src/widget_string_test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,27 @@ describe('ComboboxView', function() {
6666
view.textbox.classList.contains('jpwidgets-invalidComboValue')
6767
).to.equal(true);
6868
});
69+
70+
it('escapes characters in options', function() {
71+
const input = [
72+
'foo"',
73+
'"><script>alert("foo")</script><a "',
74+
'" onmouseover=alert(1) "'
75+
];
76+
this.model.set({
77+
value: 'ABC',
78+
options: input,
79+
ensure_option: true
80+
});
81+
const options = { model: this.model };
82+
const view = new widgets.ComboboxView(options);
83+
view.render();
84+
expect(view.datalist!.children.length).to.equal(3);
85+
for (let i = 0; i < view.datalist!.children.length; ++i) {
86+
const el = view.datalist!.children[i];
87+
expect(el.tagName.toLowerCase()).to.equal('option');
88+
expect(el.getAttributeNames()).to.eqls(['value']);
89+
expect(el.getAttribute('value')).to.equal(input[i]);
90+
}
91+
});
6992
});

0 commit comments

Comments
 (0)