User:Mike Dillon/Scripts/easydom.js

This is an old revision of this page, as edited by Mike Dillon (talk | contribs) at 00:44, 23 October 2006 (comment). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <pre><nowiki>

function createEasyDomFunction(name) {
    var stringType = typeof '';
    var functionType = typeof function() {};

    // Detects if the first element is a hash of attributes and if so,
    // uses it to set attributes on the DOM node
    //
    // Returns the number of elements processed to let the caller know
    // how many of the arguments to skip
    var processAttributes = function(elem, args) {
        if (args.length == 0) {
            return 0;
        }

        // Detect string arguments
        var firstArgType = typeof args[0];
        if (firstArgType == stringType) {
            return 0;
        }

        // Detect function arguments
        if (firstArgType == functionType) {
            return 0;
        }

        // Detect DOM nodes
        if (typeof args[0].nodeType != 'undefined') {
            return 0;
        }

        // Assume that we got a hash of attributes as the first arg...
        var attrs = args[0];
        for (var attrName in attrs) {
            var attrValue = attrs[attrName];
            // Invoke functions and use their result as the value
            if (typeof attrValue == functionType) {
                attrValue = attrValue();
            }
            // Skip null values
            if (attrValue == null) {
                continue;
            }
            // Set the attribute
            elem.setAttribute(attrName, attrValue);
        }

        // Return the number of arguments processed
        return 1;
    };

    // Return the function that creates new DOM elements
    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];
            if (typeof child == stringType) {
                child = document.createTextNode(child);
            }
            elem.appendChild(child);
        }

        return elem;
    };
}

var easyDomTags = [
    "script",
    "p",
    "a",
    "div", "span",
    "ul", "ol", "li",
    "img",
    "hr",
    "br",
    "em", "strong",
    "table", "tbody", "tr", "th", "td",
    "input",
    "h1", "h2", "h3", "h4", "h5"
];

var easyDom = {};
for (var i in easyDomTags) {
    easyDom[easyDomTags[i]] = createEasyDomFunction(easyDomTags[i]);
}

// Namespace pollution
var easydom = easyDom;
var easyDOM = easyDom;

// </nowiki></pre>