Skip to content

Commit 86b06aa

Browse files
committed
When setting name attribute in old IE, make sure special characters are handled appropriately.
1 parent 4d63fce commit 86b06aa

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

spec/defaultBindings/attrBehaviors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ describe('Binding: Attr', function() {
5050
expect(testNode.childNodes[0].outerHTML).toNotMatch('name="?([^">]+)');
5151
}
5252
expect(testNode.childNodes[0].getAttribute("name")).toEqual("");
53+
54+
// Check that special characters are handled appropriately
55+
myValue("<A name with special &'\" chars>");
56+
expect(testNode.childNodes[0].name).toEqual("<A name with special &'\" chars>");
57+
if (testNode.childNodes[0].outerHTML) { // Old Firefox doesn't support outerHTML
58+
expect(testNode.childNodes[0].outerHTML).toMatch('name="?<A name with special &amp;\'&quot; chars>"?');
59+
}
60+
expect(testNode.childNodes[0].getAttribute("name")).toEqual("<A name with special &'\" chars>");
5361
});
5462

5563
it('Should respond to changes in an observable value', function() {

src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ ko.utils = (function () {
482482
// - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
483483
if (ieVersion <= 7) {
484484
try {
485-
element.mergeAttributes(document.createElement("<input name='" + element.name + "'/>"), false);
485+
var escapedName = element.name.replace(/[&<>'"]/g, function(r){ return "&#" + r.charCodeAt(0) + ";"; });
486+
element.mergeAttributes(document.createElement("<input name='" + escapedName + "'/>"), false);
486487
}
487488
catch(e) {} // For IE9 with doc mode "IE9 Standards" and browser mode "IE9 Compatibility View"
488489
}

0 commit comments

Comments
 (0)