Content deleted Content added
Mike Dillon (talk | contribs) m check .arity of function-typed attrValue |
Mike Dillon (talk | contribs) fix broken browsers without createElementNS |
||
(8 intermediate revisions by the same user not shown) | |||
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); }
//
var
"bdo", "script", "style", "object", "param", "iframe", "link", "meta", "p",
"pre", "a", "div", "span", "ul", "ol", "li", "img", "hr", "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", "fieldset", "legend"
];
//
var
"Abort", "Blur", "Change", "Click", "DblClick", "DragDrop", "Error",
"Focus", "KeyDown", "KeyPress", "KeyUp", "Load", "MouseDown",
"MouseMove", "MouseOut", "MouseOver", "MouseUp", "Move", "Reset",
"Resize", "Select", "Submit", "Unload"
];
// Create an anonymous namespace if none was provided
if (isUndefined(namespace)) namespace = {};
// Settings
var settings = {
"namespaceUri": "http://www.w3.org/1999/xhtml",
"invokeFunctions": true,
"tagNames": defaultTagNames,
"eventTypes": defaultEventTypes
// Override default settings with specified options
if (options) {
}
}
// If the browser doesn't understand createElementNS, fake it (God help them...)
if (isUndefined(document.createElementNS)) {
document.createElementNS = function (ns, name) {
return document.createElement(name);
};
}
// Creates the DOM element
var createDomElement = function(name) {
return document.createElementNS(settings.namespaceUri, name);
var defaultAttributeHandler = function (elem, attrName, attrValue) {
// Invoke function callbacks of zero or one argument and use their result as the new attrValue
if (settings.invokeFunctions && isFunction(attrValue) && attrValue.length <= 1) {
attrValue = attrValue(elem);
}
Line 88 ⟶ 83:
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 ⟶ 150:
var attrs = args[0];
for (var attrName in attrs) {
defaultAttributeHandler(elem, attrName, attrs[attrName]);
} else {
attributeHandlers[attrName](elem, attrName, attrs[attrName]);
}
}
Line 148 ⟶ 185:
};
};
// Populate the namespace
for (var i in
namespace[tagName] = createDomElementBuilder(tagName);
}
|