|
9 | 9 | /*
|
10 | 10 |
|
11 | 11 | This code is not formatted for readability, but rather run-speed and to assist compilers.
|
12 |
| -
|
| 12 | +
|
13 | 13 | However, the code's intention should be transparent.
|
14 |
| -
|
| 14 | +
|
15 | 15 | *** IE SUPPORT ***
|
16 |
| -
|
| 16 | +
|
17 | 17 | If you require this library to work in IE7, add the following after declaring crel.
|
18 |
| -
|
| 18 | +
|
19 | 19 | var testDiv = document.createElement('div'),
|
20 | 20 | testLabel = document.createElement('label');
|
21 | 21 |
|
22 |
| - testDiv.setAttribute('class', 'a'); |
| 22 | + testDiv.setAttribute('class', 'a'); |
23 | 23 | testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
|
24 | 24 | testDiv.setAttribute('name','a');
|
25 | 25 | testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
|
26 | 26 | element.id = value;
|
27 | 27 | }:undefined;
|
28 |
| -
|
| 28 | +
|
29 | 29 |
|
30 | 30 | testLabel.setAttribute('for', 'a');
|
31 | 31 | testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;
|
32 |
| -
|
33 |
| -
|
| 32 | +
|
| 33 | +
|
34 | 34 |
|
35 | 35 | */
|
36 | 36 |
|
37 | 37 | (function(root,factory){
|
38 | 38 | if(typeofexports==='object'){
|
39 |
| -if(!root.window){ |
40 |
| -varjsdom=require('jsdom').jsdom; |
41 |
| -root.window=jsdom().parentWindow; |
42 |
| -} |
43 |
| -module.exports=factory(root.window); |
| 39 | +module.exports=factory(); |
44 | 40 | }elseif(typeofdefine==='function'&&define.amd){
|
45 |
| -define(factory.bind(null,window)); |
| 41 | +define(factory); |
46 | 42 | }else{
|
47 |
| -root.crel=factory(root.window); |
| 43 | +root.crel=factory(); |
48 | 44 | }
|
49 |
| -}(this,function(window){ |
50 |
| -// based on http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object |
51 |
| -varisNode=typeofNode==='object' |
52 |
| - ?function(object){returnobjectinstanceofNode} |
53 |
| - :function(object){ |
54 |
| -returnobject |
55 |
| -&&typeofobject==='object' |
56 |
| -&&typeofobject.nodeType==='number' |
57 |
| -&&typeofobject.nodeName==='string'; |
| 45 | +}(this,function(){ |
| 46 | +varfn='function', |
| 47 | +obj='object', |
| 48 | +nodeType='nodeType', |
| 49 | +textContent='textContent', |
| 50 | +setAttribute='setAttribute', |
| 51 | +attrMapString='attrMap', |
| 52 | +isNodeString='isNode', |
| 53 | +isElementString='isElement', |
| 54 | +d=typeofdocument===obj ?document :{}, |
| 55 | +isType=function(a,type){ |
| 56 | +returntypeofa===type; |
| 57 | +}, |
| 58 | +isNode=typeofNode===fn ?function(object){ |
| 59 | +returnobjectinstanceofNode; |
| 60 | +} : |
| 61 | +// in IE <= 8 Node is an object, obviously.. |
| 62 | +function(object){ |
| 63 | +returnobject&& |
| 64 | +isType(object,obj)&& |
| 65 | +(nodeTypeinobject)&& |
| 66 | +isType(object.ownerDocument,obj); |
| 67 | +}, |
| 68 | +isElement=function(object){ |
| 69 | +returncrel[isNodeString](object)&&object[nodeType]===1; |
| 70 | +}, |
| 71 | +isArray=function(a){ |
| 72 | +returnainstanceofArray; |
| 73 | +}, |
| 74 | +appendChild=function(element,child){ |
| 75 | +if(!crel[isNodeString](child)){ |
| 76 | +child=d.createTextNode(child); |
| 77 | +} |
| 78 | +element.appendChild(child); |
58 | 79 | };
|
59 | 80 |
|
| 81 | + |
60 | 82 | functioncrel(){
|
61 |
| -vardocument=window.document, |
62 |
| -args=arguments,//Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance. |
63 |
| -element=document.createElement(args[0]), |
| 83 | +varargs=arguments,//Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance. |
| 84 | +element=args[0], |
64 | 85 | child,
|
65 | 86 | settings=args[1],
|
66 | 87 | childIndex=2,
|
67 | 88 | argumentsLength=args.length,
|
68 |
| -attributeMap=crel.attrMap; |
| 89 | +attributeMap=crel[attrMapString]; |
69 | 90 |
|
| 91 | +element=crel[isElementString](element) ?element :d.createElement(element); |
70 | 92 | // shortcut
|
71 | 93 | if(argumentsLength===1){
|
72 | 94 | returnelement;
|
73 | 95 | }
|
74 | 96 |
|
75 |
| -if(typeofsettings!=='object'||isNode(settings)){ |
| 97 | +if(!isType(settings,obj)||crel[isNodeString](settings)||isArray(settings)){ |
76 | 98 | --childIndex;
|
77 | 99 | settings=null;
|
78 | 100 | }
|
79 | 101 |
|
80 |
| -// shortcut if there is only one child that is a string |
81 |
| -if((argumentsLength-childIndex)===1&&typeofargs[childIndex]==='string'&&element.textContent!==undefined){ |
82 |
| -element.textContent=args[childIndex]; |
83 |
| -}else{ |
| 102 | +// shortcut if there is only one child that is a string |
| 103 | +if((argumentsLength-childIndex)===1&&isType(args[childIndex],'string')&&element[textContent]!==undefined){ |
| 104 | +element[textContent]=args[childIndex]; |
| 105 | +}else{ |
84 | 106 | for(;childIndex<argumentsLength;++childIndex){
|
85 | 107 | child=args[childIndex];
|
86 |
| -
|
| 108 | + |
87 | 109 | if(child==null){
|
88 | 110 | continue;
|
89 | 111 | }
|
90 |
| - |
91 |
| -if(!isNode(child)){ |
92 |
| -child=document.createTextNode(child); |
| 112 | + |
| 113 | +if(isArray(child)){ |
| 114 | +for(vari=0;i<child.length;++i){ |
| 115 | +appendChild(element,child[i]); |
| 116 | +} |
| 117 | +}else{ |
| 118 | +appendChild(element,child); |
93 | 119 | }
|
94 |
| - |
95 |
| -element.appendChild(child); |
96 | 120 | }
|
97 | 121 | }
|
98 |
| -
|
| 122 | + |
99 | 123 | for(varkeyinsettings){
|
100 | 124 | if(!attributeMap[key]){
|
101 |
| -element.setAttribute(key,settings[key]); |
| 125 | +element[setAttribute](key,settings[key]); |
102 | 126 | }else{
|
103 |
| -varattr=crel.attrMap[key]; |
104 |
| -if(typeofattr==='function'){ |
105 |
| -attr(element,settings[key]); |
106 |
| -}else{ |
107 |
| -element.setAttribute(attr,settings[key]); |
| 127 | +varattr=attributeMap[key]; |
| 128 | +if(typeofattr===fn){ |
| 129 | +attr(element,settings[key]); |
| 130 | +}else{ |
| 131 | +element[setAttribute](attr,settings[key]); |
108 | 132 | }
|
109 | 133 | }
|
110 | 134 | }
|
111 |
| -
|
| 135 | + |
112 | 136 | returnelement;
|
113 | 137 | }
|
114 |
| -
|
| 138 | + |
115 | 139 | // Used for mapping one kind of attribute to the supported version of that in bad browsers.
|
116 |
| -// String referenced so that compilers maintain the property name. |
117 |
| -crel['attrMap']={}; |
118 |
| - |
119 |
| -// String referenced so that compilers maintain the property name. |
120 |
| -crel["isNode"]=isNode; |
121 |
| -
|
| 140 | +crel[attrMapString]={}; |
| 141 | + |
| 142 | +crel[isElementString]=isElement; |
| 143 | + |
| 144 | +crel[isNodeString]=isNode; |
| 145 | + |
122 | 146 | returncrel;
|
123 | 147 | }));
|