Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc4e4aee

Browse files
committed
update to latest crel,closesmarianoguerra#25
1 parentcf55d41 commitc4e4aee

File tree

1 file changed

+78
-54
lines changed

1 file changed

+78
-54
lines changed

‎lib/crel.js‎

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,115 +9,139 @@
99
/*
1010
1111
This code is not formatted for readability, but rather run-speed and to assist compilers.
12-
12+
1313
However, the code's intention should be transparent.
14-
14+
1515
*** IE SUPPORT ***
16-
16+
1717
If you require this library to work in IE7, add the following after declaring crel.
18-
18+
1919
var testDiv = document.createElement('div'),
2020
testLabel = document.createElement('label');
2121
22-
testDiv.setAttribute('class', 'a');
22+
testDiv.setAttribute('class', 'a');
2323
testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
2424
testDiv.setAttribute('name','a');
2525
testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
2626
element.id = value;
2727
}:undefined;
28-
28+
2929
3030
testLabel.setAttribute('for', 'a');
3131
testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;
32-
33-
32+
33+
3434
3535
*/
3636

3737
(function(root,factory){
3838
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();
4440
}elseif(typeofdefine==='function'&&define.amd){
45-
define(factory.bind(null,window));
41+
define(factory);
4642
}else{
47-
root.crel=factory(root.window);
43+
root.crel=factory();
4844
}
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);
5879
};
5980

81+
6082
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],
6485
child,
6586
settings=args[1],
6687
childIndex=2,
6788
argumentsLength=args.length,
68-
attributeMap=crel.attrMap;
89+
attributeMap=crel[attrMapString];
6990

91+
element=crel[isElementString](element) ?element :d.createElement(element);
7092
// shortcut
7193
if(argumentsLength===1){
7294
returnelement;
7395
}
7496

75-
if(typeofsettings!=='object'||isNode(settings)){
97+
if(!isType(settings,obj)||crel[isNodeString](settings)||isArray(settings)){
7698
--childIndex;
7799
settings=null;
78100
}
79101

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{
84106
for(;childIndex<argumentsLength;++childIndex){
85107
child=args[childIndex];
86-
108+
87109
if(child==null){
88110
continue;
89111
}
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);
93119
}
94-
95-
element.appendChild(child);
96120
}
97121
}
98-
122+
99123
for(varkeyinsettings){
100124
if(!attributeMap[key]){
101-
element.setAttribute(key,settings[key]);
125+
element[setAttribute](key,settings[key]);
102126
}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]);
108132
}
109133
}
110134
}
111-
135+
112136
returnelement;
113137
}
114-
138+
115139
// 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+
122146
returncrel;
123147
}));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp