##// END OF EJS Templates
setup.py: include additional package data
setup.py: include additional package data

File last commit:

r0:548a840d
r193:cfc96dbf
Show More
crel.js
123 lines | 4.9 KiB | application/javascript | JavascriptLexer
//Copyright (C) 2012 Kory Nunn
//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.
/*
This code is not formatted for readability, but rather run-speed and to assist compilers.
However, the code's intention should be transparent.
*** IE SUPPORT ***
If you require this library to work in IE7, add the following after declaring crel.
var testDiv = document.createElement('div'),
testLabel = document.createElement('label');
testDiv.setAttribute('class', 'a');
testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
testDiv.setAttribute('name','a');
testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
element.id = value;
}:undefined;
testLabel.setAttribute('for', 'a');
testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;
*/
(function (root, factory) {
if (typeof exports === 'object') {
if (!root.window) {
var jsdom = require('jsdom').jsdom;
root.window = jsdom().parentWindow;
}
module.exports = factory(root.window);
} else if (typeof define === 'function' && define.amd) {
define(factory.bind(null, window));
} else {
root.crel = factory(root.window);
}
}(this, function (window) {
// based on http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
var isNode = typeof Node === 'object'
? function (object) { return object instanceof Node }
: function (object) {
return object
&& typeof object === 'object'
&& typeof object.nodeType === 'number'
&& typeof object.nodeName === 'string';
};
function crel(){
var document = window.document,
args = arguments, //Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance.
element = document.createElement(args[0]),
child,
settings = args[1],
childIndex = 2,
argumentsLength = args.length,
attributeMap = crel.attrMap;
// shortcut
if(argumentsLength === 1){
return element;
}
if(typeof settings !== 'object' || isNode(settings)) {
--childIndex;
settings = null;
}
// shortcut if there is only one child that is a string
if((argumentsLength - childIndex) === 1 && typeof args[childIndex] === 'string' && element.textContent !== undefined){
element.textContent = args[childIndex];
}else{
for(; childIndex < argumentsLength; ++childIndex){
child = args[childIndex];
if(child == null){
continue;
}
if(!isNode(child)){
child = document.createTextNode(child);
}
element.appendChild(child);
}
}
for(var key in settings){
if(!attributeMap[key]){
element.setAttribute(key, settings[key]);
}else{
var attr = crel.attrMap[key];
if(typeof attr === 'function'){
attr(element, settings[key]);
}else{
element.setAttribute(attr, settings[key]);
}
}
}
return element;
}
// Used for mapping one kind of attribute to the supported version of that in bad browsers.
// String referenced so that compilers maintain the property name.
crel['attrMap'] = {};
// String referenced so that compilers maintain the property name.
crel["isNode"] = isNode;
return crel;
}));