Skip to content

Commit 42d9c88

Browse files
committed
Escape optgroup label when appending to HTML.
1 parent 908b0bf commit 42d9c88

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

coffee/lib/abstract-chosen.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class AbstractChosen
131131

132132
group_el = document.createElement("li")
133133
group_el.className = classes.join(" ")
134-
group_el.innerHTML = group.highlighted_html or group.label
134+
group_el.innerHTML = group.highlighted_html or this.escape_html(group.label)
135135
group_el.title = group.title if group.title
136136

137137
this.outerHTML(group_el)

spec/jquery/searching.spec.coffee

+16-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@ describe "Searching", ->
4747
expect(div.find(".active-result").length).toBe(1)
4848
expect(div.find(".active-result").first().html()).toBe("<em>A</em> &amp; B")
4949

50-
it "renders optgroups correctly when they contain characters that require HTML encoding", ->
50+
it "renders optgroups correctly when they contain html encoded tags", ->
51+
div = $("<div>").html("""
52+
<select>
53+
<optgroup label="A &lt;b&gt;hi&lt;/b&gt; B">
54+
<option value="Item">Item</option>
55+
</optgroup>
56+
</select>
57+
""")
58+
59+
div.find("select").chosen()
60+
div.find(".chosen-container").trigger("mousedown") # open the drop
61+
62+
expect(div.find(".group-result").length).toBe(1)
63+
expect(div.find(".group-result").first().html()).toBe("A &lt;b&gt;hi&lt;/b&gt; B")
64+
65+
it "renders optgroups correctly when they contain characters that require HTML encoding when searching", ->
5166
div = $("<div>").html("""
5267
<select>
5368
<optgroup label="A &amp; B">

spec/proto/searching.spec.coffee

+16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ describe "Searching", ->
5151
expect(div.down(".active-result").innerHTML).toBe("<em>A</em> &amp; B")
5252

5353
it "renders optgroups correctly when they contain characters that require HTML encoding", ->
54+
div = new Element("div")
55+
div.update("""
56+
<select>
57+
<optgroup label="A &lt;b&gt;hi&lt;/b&gt; B">
58+
<option value="Item">Item</option>
59+
</optgroup>
60+
</select>
61+
""")
62+
63+
new Chosen(div.down("select"))
64+
simulant.fire(div.down(".chosen-container"), "mousedown") # open the drop
65+
66+
expect(div.select(".group-result").length).toBe(1)
67+
expect(div.down(".group-result").innerHTML).toBe("A &lt;b&gt;hi&lt;/b&gt; B")
68+
69+
it "renders optgroups correctly when they contain characters that require HTML encoding when searching", ->
5470
div = new Element("div")
5571
div.update("""
5672
<select>

0 commit comments

Comments
 (0)