Skip to content

Instantly share code, notes, and snippets.

@insin
Created May 6, 2014 15:18
Show Gist options
  • Save insin/641bc008d044ddc32fd0 to your computer and use it in GitHub Desktop.
Save insin/641bc008d044ddc32fd0 to your computer and use it in GitHub Desktop.
dombuilder-2006-09-19
/*
Copyright (c) 2006 Dan Webb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/*
JB 2006-09-19
Updates:
- Added setting of event handlers via Prototype's Event.observe.
- Now uses conditional comments for IE detection instead of user agent string
sniffing.
- Did a general tidy up, improved code readability and documented some of the
more obscure stuff for ease of future maintainence.
*/
DomBuilder =
{
/**
* IE is broken in numerous ways which, unfortunately, require browser
* detection to fix.
*/
isIE: false,
/** Translations for attribute names which IE would otherwise choke on. */
IE_TRANSLATIONS: {
"class": "className",
"for": "htmlFor"
},
/**
* Deals with special cases related to setting attribute values in IE.
*/
ieSetAttribute:function(el, name, value)
{
if (typeof(this.IE_TRANSLATIONS[name]) == "string")
{
el[this.IE_TRANSLATIONS[name]] = value;
}
else if (name == "style")
{
el.style.cssText = value;
}
else
{
el.setAttribute(name, value);
}
},
/**
* Sets up element creation functions in the given target object.
*/
apply : function(target)
{
target = target || window;
var tagNames =
("p div span strong em img table tr td th thead tbody tfoot " +
"pre code h1 h2 h3 h4 h5 h6 ul ol li form input textarea legend " +
"fieldset select option blockquote cite br hr dd dl dt address a " +
"button abbr acronym script link style bdo ins del object param " +
"col colgroup optgroup caption label dfn kbd samp var").split(" ");
for (var i = 0, tagName; tagName = tagNames[i]; i++)
{
target[tagName.toUpperCase()] =
DomBuilder.createElementFunction(tagName);
}
return target;
},
/**
* Creates an element creation function for the given tagName.
*/
createElementFunction: function(tagName)
{
return function()
{
var args = arguments;
// The arguments object is not an Array - it doesn't have the
// slice method by default.
args.slice = [].slice;
var attributes = {};
var children = [];
if (args.length > 0)
{
if (typeof(args[0].nodeName) == "string" ||
typeof(args[0]) == "string")
{
children = args;
}
else
{
attributes = args[0];
children = args.slice(1);
}
}
return DomBuilder.createElement(tagName, attributes, children);
};
},
/**
* Creates an element with the given attributes and children.
*/
createElement: function(tagName, attributes, children)
{
attributes = attributes || {};
children = children || [];
// Create the element itself
if (this.isIE && typeof(attributes.name) == "string")
{
// The goggles, they do nothing!
var el = document.createElement("<" + tagName +
" name=" + attributes.name + ">");
}
else
{
var el = document.createElement(tagName);
}
// Add attributes
for (var attr in attributes)
{
// Guard against additions to Object.prototype
if (attributes.hasOwnProperty(attr))
{
if (attr.match(/^on/i) && typeof(attributes[attr]) == "function")
{
// Trust the user with the event name
Event.observe(el,
attr.replace(/^on/i, ""),
attributes[attr]);
}
else if (this.isIE)
{
this.ieSetAttribute(el, attr, attributes[attr]);
}
else
{
el.setAttribute(attr, attributes[attr]);
}
}
}
// Append children
for (var i = 0; i < children.length; i++)
{
if (typeof(children[i]) == "string")
{
children[i] = document.createTextNode(children[i]);
}
el.appendChild(children[i]);
}
return el;
}
};
// Detect Internet Explorer using conditional comments
/*@cc_on @*/
/*@if (@_win32)
DomBuilder.isIE = true;
/*@end @*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment