Skip to content

Commit 84a4d4f

Browse files
Fix issue in old versions of IE with checkbox reading.
Some of the tests added in 4a22a5 apparently never worked in old IE. I added a capability check for the buggy behavior and a new path for IE. Also, changed a `getAttribute` in a test to `readAttribute` — otherwise the test would never pass in IE 6-7, since `getAttribute` doesn't work right.
1 parent 9d49b1b commit 84a4d4f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/prototype/dom/dom.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2385,13 +2385,36 @@
23852385
return element;
23862386
}
23872387

2388+
// Test whether checkboxes work properly with `hasAttribute`.
2389+
var PROBLEMATIC_HAS_ATTRIBUTE_WITH_CHECKBOXES = (function () {
2390+
if (!HAS_EXTENDED_CREATE_ELEMENT_SYNTAX) {
2391+
// Only IE browsers are known to exhibit this one, so we'll take a
2392+
// shortcut.
2393+
return false;
2394+
}
2395+
var checkbox = document.createElement('<input type="checkbox">');
2396+
checkbox.checked = true;
2397+
var node = checkbox.getAttributeNode('checked');
2398+
var buggy = !node.specified;
2399+
return !node.specified;
2400+
})();
2401+
23882402
function hasAttribute(element, attribute) {
23892403
attribute = ATTRIBUTE_TRANSLATIONS.has[attribute] || attribute;
23902404
var node = $(element).getAttributeNode(attribute);
23912405
return !!(node && node.specified);
23922406
}
23932407

2394-
GLOBAL.Element.Methods.Simulated.hasAttribute = hasAttribute;
2408+
function hasAttribute_IE(element, attribute) {
2409+
if (attribute === 'checked') {
2410+
return element.checked;
2411+
}
2412+
return hasAttribute(element, attribute);
2413+
}
2414+
2415+
GLOBAL.Element.Methods.Simulated.hasAttribute =
2416+
PROBLEMATIC_HAS_ATTRIBUTE_WITH_CHECKBOXES ?
2417+
hasAttribute_IE : hasAttribute;
23952418

23962419
/** deprecated
23972420
* Element.classNames(@element) -> [String...]

test/unit/dom_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ new Test.Unit.Runner({
11041104
checkedCheckbox = $('write_attribute_checked_checkbox');
11051105
this.assert( checkbox. writeAttribute('checked'). checked);
11061106
this.assert( checkbox. writeAttribute('checked'). hasAttribute('checked'));
1107-
this.assertEqual('checked', checkbox.writeAttribute('checked').getAttribute('checked'));
1107+
this.assertEqual('checked', checkbox.writeAttribute('checked').readAttribute('checked'));
11081108
this.assert(!checkbox. writeAttribute('checked'). hasAttribute('undefined'));
11091109
this.assert( checkbox. writeAttribute('checked', true). checked);
11101110
this.assert( checkbox. writeAttribute('checked', true). hasAttribute('checked'));

0 commit comments

Comments
 (0)