D
David Mark
But it has the same old attr method.
attr: function( elem, name, value ) {
// don't set attributes on text and comment nodes
Don't pass them! Put that in the docs.
if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
return undefined;
}
Don't pass null, 0, '', undefined, etc. for elem either. What would
be the point of this, other than to make it harder to find bugs?
if ( name in jQuery.fn && name !== "attr" ) {
return jQuery(elem)[name](value);
}
It's the do-everything function.
var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
// Whether we are setting (or getting)
set = value !== undefined;
They still seem to think this method is appropriate for XML. Why not
call get/setAttribute on XML nodes? That's all this ends up doing.
// Try to normalize/fix the name
name = notxml && jQuery.props[ name ] || name;
Normalize/fix? And jQuery.props is still a long way from complete:-
jQuery.props = {
"for": "htmlFor",
"class": "className",
readonly: "readOnly",
maxlength: "maxLength",
cellspacing: "cellSpacing",
rowspan: "rowSpan",
colspan: "colSpan",
tabindex: "tabIndex",
usemap: "useMap",
frameborder: "frameBorder"
};
How many years does it take for a million code monkeys to come up with
the list of attributes that have camel-case property names? More than
three apparently.
// Only do all the following if this is a node (faster for style)
What?!
if ( elem.nodeType === 1 ) {
// These attributes require special treatment
var special = /href|src|style/.test( name );
What sort of "special" treatment? How are href, src and style
related?
// Safari mis-reports the default selected property of a hidden
option
// Accessing the parent's selectedIndex property fixes it
if ( name == "selected" && elem.parentNode ) {
elem.parentNode.selectedIndex;
}
Mystical incantation.
// If applicable, access the attribute via the DOM 0 way
Read its property?
if ( name in elem && notxml && !special ) {
So, if it is - in - the elem, elem is not an XML node and it is not
href, src or style, get or set the property.
if ( set ) {
// We can't allow the type property to be changed (since it
causes problems in IE)
if ( name == "type" && /(button|input)/i.test(elem.nodeName) &&
elem.parentNode ) {
throw "type property can't be changed";
}
Misguided waste of space.
elem[ name ] = value;
}
// browsers index elements by id/name on forms, give priority to
attributes.
if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode
(name) ) {
return elem.getAttributeNode( name ).nodeValue;
}
// elem.tabIndex doesn't always return the correct value when it
hasn't been explicitly set
// http://fluidproject.org/blog/2008/0...and-removing-tabindex-values-with-javascript/
That article is very confused.
if ( name == "tabIndex" ) {
var attributeNode = elem.getAttributeNode( "tabIndex" );
return attributeNode && attributeNode.specified
? attributeNode.value
That's a string.
: /(button|input|object|select|textarea)/i.test(elem.nodeName)
? 0
That's a number.
: /^(a|area)$/i.test(elem.nodeName) && elem.href
? 0
: undefined;
}
So, tabindex is treated very oddly, returning a string, number or
undefined. This attribute is singled out because that article singled
it out. This is programming by observation of misinterpreted
observations.
return elem[ name ];
}
Then it switches gears to attributes.
if ( !jQuery.support.style && notxml && name == "style" ) {
if ( set ) {
elem.style.cssText = "" + value;
What sort of value would this be that it would make sense to convert
it to a string?
}
return elem.style.cssText;
}
if ( set ) {
// convert the value to a string (all browsers do this but IE) see
#1070
LOL. I'll pass on #1070. They seem to think all IE's are the same.
Of course, IE8 varies wildly here depending on the mode.
elem.setAttribute( name, "" + value );
But they already "fixed" the name (converted to a property name):-
// Try to normalize/fix the name
name = notxml && jQuery.props[ name ] || name;
This doesn't make any sense. By coincidence, the - in - check above
keeps some properties out of here. One that would fall through (in
some browsers, e.g. FF) is "onclick". Passing a function like this:-
attr(el, 'onclick', function() { ... });
Would set an odd attribute indeed, considering what
Function.prototype.toString does. Of course, if the property existed
previously, the previous fork would apply and the method would seem to
work.
}
var attr = !jQuery.support.hrefNormalized && notxml && special
// Some attributes require a special call on IE
More than they know.
? elem.getAttribute( name, 2 )
: elem.getAttribute( name );
This will vary across IE versions and modes.
http://www.cinsoft.net/attributes.html
// Non-existent attributes return null, we normalize to undefined
They don't in IE (except IE8 standards mode).
return attr === null ? undefined : attr;
}
// elem is actually elem.style ... set the style
// Using attr for specific style information is now deprecated. Use
style insead.
return jQuery.style(elem, name, value);
}
I thought they were going to re-factor this for 2010? I thought they
would install at least some versions of IE for testing as well. Maybe
next year.
So they still have trouble reading documents. Odd handicap for a DOM
library. And how many times have they been told about these
problems? They did like the event detection bit. I don't care for
the copy and paste implementation though.
// Technique from Juriy Zaytsev
I guess they didn't read the article.
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
var eventSupported = function( eventName ) {
var el = document.createElement("div");
eventName = "on" + eventName;
var isSupported = (eventName in el);
Spotty inference. Keeps IE out of the following:-
if ( !isSupported ) {
el.setAttribute(eventName, "return;");
isSupported = typeof el[eventName] === "function";
Won't work in IE6/7 or 8 in compatibility mode. But by coincidence,
those never get here. Standard IE8 should get here, but relies on the
weaker inference above.
}
el = null;
return isSupported;
};
attr: function( elem, name, value ) {
// don't set attributes on text and comment nodes
Don't pass them! Put that in the docs.
if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
return undefined;
}
Don't pass null, 0, '', undefined, etc. for elem either. What would
be the point of this, other than to make it harder to find bugs?
if ( name in jQuery.fn && name !== "attr" ) {
return jQuery(elem)[name](value);
}
It's the do-everything function.
var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
// Whether we are setting (or getting)
set = value !== undefined;
They still seem to think this method is appropriate for XML. Why not
call get/setAttribute on XML nodes? That's all this ends up doing.
// Try to normalize/fix the name
name = notxml && jQuery.props[ name ] || name;
Normalize/fix? And jQuery.props is still a long way from complete:-
jQuery.props = {
"for": "htmlFor",
"class": "className",
readonly: "readOnly",
maxlength: "maxLength",
cellspacing: "cellSpacing",
rowspan: "rowSpan",
colspan: "colSpan",
tabindex: "tabIndex",
usemap: "useMap",
frameborder: "frameBorder"
};
How many years does it take for a million code monkeys to come up with
the list of attributes that have camel-case property names? More than
three apparently.
// Only do all the following if this is a node (faster for style)
What?!
if ( elem.nodeType === 1 ) {
// These attributes require special treatment
var special = /href|src|style/.test( name );
What sort of "special" treatment? How are href, src and style
related?
// Safari mis-reports the default selected property of a hidden
option
// Accessing the parent's selectedIndex property fixes it
if ( name == "selected" && elem.parentNode ) {
elem.parentNode.selectedIndex;
}
Mystical incantation.
// If applicable, access the attribute via the DOM 0 way
Read its property?
if ( name in elem && notxml && !special ) {
So, if it is - in - the elem, elem is not an XML node and it is not
href, src or style, get or set the property.
if ( set ) {
// We can't allow the type property to be changed (since it
causes problems in IE)
if ( name == "type" && /(button|input)/i.test(elem.nodeName) &&
elem.parentNode ) {
throw "type property can't be changed";
}
Misguided waste of space.
elem[ name ] = value;
}
// browsers index elements by id/name on forms, give priority to
attributes.
if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode
(name) ) {
return elem.getAttributeNode( name ).nodeValue;
}
// elem.tabIndex doesn't always return the correct value when it
hasn't been explicitly set
// http://fluidproject.org/blog/2008/0...and-removing-tabindex-values-with-javascript/
That article is very confused.
if ( name == "tabIndex" ) {
var attributeNode = elem.getAttributeNode( "tabIndex" );
return attributeNode && attributeNode.specified
? attributeNode.value
That's a string.
: /(button|input|object|select|textarea)/i.test(elem.nodeName)
? 0
That's a number.
: /^(a|area)$/i.test(elem.nodeName) && elem.href
? 0
: undefined;
}
So, tabindex is treated very oddly, returning a string, number or
undefined. This attribute is singled out because that article singled
it out. This is programming by observation of misinterpreted
observations.
return elem[ name ];
}
Then it switches gears to attributes.
if ( !jQuery.support.style && notxml && name == "style" ) {
if ( set ) {
elem.style.cssText = "" + value;
What sort of value would this be that it would make sense to convert
it to a string?
}
return elem.style.cssText;
}
if ( set ) {
// convert the value to a string (all browsers do this but IE) see
#1070
LOL. I'll pass on #1070. They seem to think all IE's are the same.
Of course, IE8 varies wildly here depending on the mode.
elem.setAttribute( name, "" + value );
But they already "fixed" the name (converted to a property name):-
// Try to normalize/fix the name
name = notxml && jQuery.props[ name ] || name;
This doesn't make any sense. By coincidence, the - in - check above
keeps some properties out of here. One that would fall through (in
some browsers, e.g. FF) is "onclick". Passing a function like this:-
attr(el, 'onclick', function() { ... });
Would set an odd attribute indeed, considering what
Function.prototype.toString does. Of course, if the property existed
previously, the previous fork would apply and the method would seem to
work.
}
var attr = !jQuery.support.hrefNormalized && notxml && special
// Some attributes require a special call on IE
More than they know.
? elem.getAttribute( name, 2 )
: elem.getAttribute( name );
This will vary across IE versions and modes.
http://www.cinsoft.net/attributes.html
// Non-existent attributes return null, we normalize to undefined
They don't in IE (except IE8 standards mode).
return attr === null ? undefined : attr;
}
// elem is actually elem.style ... set the style
// Using attr for specific style information is now deprecated. Use
style insead.
return jQuery.style(elem, name, value);
}
I thought they were going to re-factor this for 2010? I thought they
would install at least some versions of IE for testing as well. Maybe
next year.
So they still have trouble reading documents. Odd handicap for a DOM
library. And how many times have they been told about these
problems? They did like the event detection bit. I don't care for
the copy and paste implementation though.
// Technique from Juriy Zaytsev
I guess they didn't read the article.
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
var eventSupported = function( eventName ) {
var el = document.createElement("div");
eventName = "on" + eventName;
var isSupported = (eventName in el);
Spotty inference. Keeps IE out of the following:-
if ( !isSupported ) {
el.setAttribute(eventName, "return;");
isSupported = typeof el[eventName] === "function";
Won't work in IE6/7 or 8 in compatibility mode. But by coincidence,
those never get here. Standard IE8 should get here, but relies on the
weaker inference above.
}
el = null;
return isSupported;
};