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

Content deleted Content added
m check .arity of function-typed attrValue
update to code from dev; adds attribute handler functions, options to change namespace and tag names, special handling for "style" in IE
Line 1:
// <pre><nowiki>
 
function buildEasyDomNamespace(namespace, options) {
var isType = function (o, t) { return typeof o == t };
var isBool = function (o) { return isType(o, typeof true); };
Line 15:
var isNode = function (o) { return isDefined(o) && o != null && isNumber(o.nodeType); }
 
// TheDefault tag names that willto be createdinstalled ininto the namespace as functions
var tagsdefaultTagNames = [
"bdo",
"script",
Line 30:
"br",
"em", "strong", "sup", "sub", "tt", "abbr", "acronym",
"del", "ins", "cite", "blockquote", "code",
"table", "tbody", "tfoot", "tr", "th", "td", "col", "colgroup", "caption",
"form", "input", "select", "option", "optgroup", "button", "textarea",
"h1", "h2", "h3", "h4", "h5", "h6",
"label",
"canvas"
];
 
// CreatesDefault theevent DOM elementtypes
var createDomElementdefaultEventTypes = function(name) {[
var// elemHTML = document4.createElement(name);0
"Abort", "Blur", "Change", "Click", "DblClick", "DragDrop", "Error",
return elem;
"Focus", "KeyDown", "KeyPress", "KeyUp", "Load", "MouseDown",
};
"MouseMove", "MouseOut", "MouseOver", "MouseUp", "Move", "Reset",
"Resize", "Select", "Submit", "Unload"
];
 
// Create an anonymous namespace if none was provided
var attrNameTranslations = {};
if (isUndefined(namespace)) namespace = {};
 
// Settings
// Conditionally add I.E. name overrides
var settings = {
/*@cc_on
"namespaceUri": "http://www.w3.org/1999/xhtml",
attrNameTranslations["for"] = "htmlFor";
attrNameTranslations["maxlength"] = "maxLength";
attrNameTranslations["class"] = "className";
attrNameTranslations["accesskey"] = "accessKey";
@*/
 
"invokeFunctions": true,
var processSingleDomAttribute = function(elem, attrName, attrValue) {
 
// Translate DOM attribute name to match implementation
"tagNames": defaultTagNames,
if (attrNameTranslations[attrName] != null) {
 
attrName = attrNameTranslations[attrName];
"eventTypes": defaultEventTypes
};
 
// Override default settings with specified options
if (options) {
for (var p in options) {
settings[p] = options[p];
}
}
 
// Creates the DOM element
// Invoke function callbacks and use their result as the value,
var createDomElement = function(name) {
// unless the attribute name starts with "on" (i.e. an event handler)
var elem = document.createElementNS(settings.namespaceUri, name);
if (isFunction(attrValue)) {
return elem;
// Use direct object property assignment for "on" attributes
};
// These properties will not be copied by Node.cloneNode
if (attrName.indexOf("on") == 0) {
elem[attrName] = attrValue;
return;
}
 
var defaultAttributeHandler = function (elem, attrName, attrValue) {
// Invoke the callback otherwise if it has zero or one argument
// Invoke function callbacks of zero or one argument and use their result as the new attrValue
if (attrValue.arity <= 1) {
if (settings.invokeFunctions && isFunction(attrValue) && attrValue.length <= attrValue(elem1); {
}attrValue = attrValue(elem);
}
 
Line 88 ⟶ 93:
elem.setAttribute(attrName, attrValue);
};
 
var createAttributeOverrideHandler = function (overrideName) {
return function (elem, attrName, attrValue) {
defaultAttributeHandler(elem, overrideName, attrValue);
};
};
 
var createEventHandlerAttributeHandler = function (overrideName) {
return function (elem, attrName, attrValue) {
if (!isFunction(attrValue)) {
attrValue = new Function(attrValue);
}
elem[overrideName || attrName] = attrValue;
};
};
 
var attributeHandlers = {};
 
for (var i in settings.eventTypes) {
var handlerName = "on" + settings.eventTypes[i];
var internalName = handlerName.toLowerCase();
// Force lower case
attributeHandlers[internalName] = createEventHandlerAttributeHandler();
// Allow mixed case (with lower case internal name)
attributeHandlers[handlerName] = createEventHandlerAttributeHandler(internalName);
}
 
// Conditionally add I.E. name overrides
/*@cc_on
attributeHandlers["for"] = createAttributeOverrideHandler("htmlFor");
attributeHandlers["maxlength"] = createAttributeOverrideHandler("maxLength");
attributeHandlers["class"] = createAttributeOverrideHandler("className");
attributeHandlers["accesskey"] = createAttributeOverrideHandler("accessKey");
 
attributeHandlers["style"] = function (elem, attrName, attrValue) {
elem.style.cssText = attrValue;
};
@*/
 
// Detects if the first element is a hash of attributes and if so,
Line 117 ⟶ 160:
var attrs = args[0];
for (var attrName in attrs) {
processSingleDomAttribute(elem,if attrName, attrs(isUndefined(attributeHandlers[attrName]);) {
defaultAttributeHandler(elem, attrName, attrs[attrName]);
} else {
attributeHandlers[attrName](elem, attrName, attrs[attrName]);
}
}
 
Line 148 ⟶ 195:
};
};
 
// Create an anonymous namespace if none was provided
if (isUndefined(namespace)) namespace = {};
 
// Populate the namespace
for (var i in tagssettings.tagNames) {
namespace[tags[i]]var tagName = createDomElementBuilder(tagssettings.tagNames[i]);
namespace[tagName] = createDomElementBuilder(tagName);
}