Testcase for jQuery Ticket 7500
attr(name) and attr(name, value) fail in 1.4.4 for non DOM element nodes
by jitter
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<p>JS code looks a bit messy but that's just to get nice output instead of the test stopping because of exceptions</p>
<p>All it does is trying to get/set/remove an attribute named "foo" </p>
<p>The important code without all the noise is (where ele is a DOM element/attribute/comment/document node or a plain js object)</p>
<br />
<pre>
$(ele).attr("foo")
$(ele).attr("foo", "bar")
$(ele).attr("foo")
$(ele).removeAttr("foo")
$(ele).attr("foo")
</pre>
<p id="pid" style="display: none"><!-- comment -->a</p>
JavaScript
var p = document.getElementById("pid"),
a = p.getAttributeNode("id"),
c = p.firstChild,
t = p.lastChild,
//fake attributes to get nice output
obj = {
nodeName: "plainjsobject",
nodeType: "plainjsobject"
};
function get(val, $val) {
try { console.log(val.nodeName + "[foo]: " + $val.attr("foo")); }
catch (e1) { fail( val, "Getting", e1); return false; }
return true;
}
function fail( ele, prefix, ex ) {
console.log( prefix + " attribute foo failed for node of type " + ele.nodeType + " " + ex.message);
}
$.each([document, p, a, c, t, obj], function(i, val) {
var getOk,
$val = $(val);
getOk = get(val, $val);
try { $val.attr("foo", "bar") }
catch (e2) { fail( val, "Setting", e2); }
//if getting failed above it will fail here also
if( getOk ) {
get(val, $val);
}
try { $val.removeAttr("foo"); }
catch (e4) { fail( val, "Removing", e4); }
//if getting failed above it will fail here also
if( getOk ) {
get(val, $val);
}
});