User:Mike Dillon/Scripts/easydom.js: Difference between revisions

Content deleted Content added
mNo edit summary
modularize attribute handling; skip nulls after evalation of function-valued attributes
Line 3:
function createEasyDomFunction(name) {
var stringType = typeof '';
var functionType = typeof function() {};
 
// Detects if the first element is a hash of attributes and if so,
return function() {
// uses it to set attributes on the DOM node
var elem = document.createElement(name);
//
// Returns the number of elements processed to let the caller know
// how many of the arguments to skip
var processAttributes = function(elem, args) {
if (attrValueargs.length == null0) {
return 0;
}
 
if// (arguments.lengthDetect ==string 0) return elem;arguments
var firstArgType = typeof args[0];
if (firstArgType == stringType) {
return 0;
}
 
var// firstChildDetect =function 0;arguments
if (typeoffirstArgType arguments[0] !== functionType) 'string'{
&&return typeof arguments[0].nodeType == 'undefined') {;
}
 
// Detect DOM var attrs = arguments[0];nodes
forif (vartypeof attrNameargs[0].nodeType in!= attrs'undefined') {
return var attrValue = attrs[attrName]0;
// Skip null values}
 
if (attrValue == null) {
// Assume that we got a hash of attributes as the first continue;arg...
var attrs = }args[0];
for (var attrName in attrs) {
// Invoke functions and use their result as the value
var if (typeof(attrValue) == 'function') {attrs[attrName];
// Invoke functions and use their result as attrValuethe = attrValue();value
if (typeof attrValue == }functionType) {
//attrValue Set= the attributeattrValue();
elem.setAttribute(attrName, attrValue);
}
firstChild// =Skip 1;null values
if (attrValue == null) {
continue;
}
// Set the attribute
elem.setAttribute(attrName, attrValue);
}
 
// Return the number of arguments processed
return 1;
};
 
return function() {
var elem = document.createElement(name);
 
// Process attribute hash, if any and skip the argument count returned
var firstChild = processAttributes(elem, arguments);
 
// Process the remaining children, if any
for (var i = firstChild; i < arguments.length; i++) {
var child = arguments[i];