-
Notifications
You must be signed in to change notification settings - Fork 38
/
nolib-support.js
204 lines (171 loc) · 5.5 KB
/
nolib-support.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
(function(global) {
var sup = global.writeCaptureSupport = global.writeCaptureSupport || {};
// scriptEval & globalEval copied almost verbatim from jQuery 1.3.2
var scriptEval = (function() {
var script = document.createElement("script");
var id = "script" + (new Date).getTime();
var root = document.documentElement;
script.type = "text/javascript";
try {
script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
} catch(e){}
root.insertBefore( script, root.firstChild );
// Make sure that the execution of code works by injecting a script
// tag with appendChild/createTextNode
// (IE doesn't support this, fails, and uses .text instead)
if ( window[ id ] ) {
delete window[ id ];
return true;
}
return false;
})();
function attrPattern(name) {
return new RegExp(name+'=(?:(["\'])([\\s\\S]*?)\\1|([^\\s>]+))','i');
}
function matchAttr(name) {
var regex = attrPattern(name);
return function(tag) {
var match = regex.exec(tag) || [];
return match[2] || match[3];
};
}
var TYPE_ATTR = matchAttr('type'),
LANG_ATTR = matchAttr('language');
function isJs(scriptTag) {
var type = TYPE_ATTR(scriptTag) || '',
lang = LANG_ATTR(scriptTag) || '';
return (!type && !lang) || // no type or lang assumes JS
type.toLowerCase().indexOf('javascript') !== -1 ||
lang.toLowerCase().indexOf('javascript') !== -1
}
function globalEval(data) {
if ( data && /\S/.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
if ( scriptEval )
script.appendChild( document.createTextNode( data ) );
else
script.text = data;
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}
}
global.writeCaptureSupport = {
_original: global.writeCaptureSupport,
noConflict: function() {
global.writeCaptureSupport = this._original;
return this;
},
// the code in this function is based on code from jQuery 1.3.2
ajax: function(options) {
if(options.dataType === 'script') {
loadXDomain(options.url,options.success,options.error);
return;
}
var xhr = newXhr(), requestDone = false, checkTimer;
xhr.open("GET", options.url, options.async);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Accept","text/javascript, application/javascript, */*");
function checkXhr(){
if ( !requestDone && xhr && (xhr.readyState == 4) ) {
requestDone = true;
if (checkTimer) {
clearInterval(checkTimer);
checkTimer = null;
}
var suc = false;
try {
// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
suc = !xhr.status && location.protocol == "file:" ||
( xhr.status >= 200 && xhr.status < 300 ) ||
xhr.status == 304 || xhr.status == 1223;
} catch(e){}
if ( suc ) {
options.success(xhr.responseText);
} else {
options.error(xhr,"error","xhr.status="+ xhr.status);
}
// Stop memory leaks
if ( options.async )
xhr = null;
}
}
if ( options.async ) {
// poll for changes
checkTimer = setInterval(checkXhr, 20);
}
try {
xhr.send();
} catch(e) {
options.error(xhr, null, e);
}
if(!options.async) {
checkXhr();
}
},
$: $,
replaceWith: function(selector,content) {
var i, len, el = $(selector),
parent = el.parentNode || el.ownerDocument,
work = document.createElement('div'),
scripts = [],
clearHTML = content.replace(/<script([\s\S]*?)>([\S\s]*?)<\/script>/gi,function(all,attrs,code) {
if(isJs(attrs)) {
scripts.push(code);
return "";
} else {
return all;
}
});
work.innerHTML = clearHTML;
while (work.firstChild) {
parent.insertBefore(work.removeChild(work.firstChild),el);
}
parent.removeChild(el);
for(i = 0, len = scripts.length; i < len; i++) {
globalEval(scripts[i]);
}
}
};
function isElement(o) {
return o && o.nodeType == 1;
}
function $(s) {
if(isElement(s)) return s;
// trim the selector
s = s && s.replace(/^\s*/,'').replace(/\s*$/,'');
if(!/^#[a-zA-Z0-9_:\.\-]+$/.test(s))
throw "nolib-support only allows id based selectors. selector=" + s;
return document.getElementById(s.substring(1));
}
var newXhr = global.ActiveXObject ? function() {
return new ActiveXObject("Microsoft.XMLHTTP");
} : function () {
return new XMLHttpRequest();
};
// the code in this function is copied and slightly modified from jQuery 1.3.2
function loadXDomain(url,success) {
// TODO what about scripts that fail to load? bad url, etc.?
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
var done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function(){
if ( !done && (!this.readyState ||
this.readyState == "loaded" || this.readyState == "complete") ) {
done = true;
success();
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
head.removeChild( script );
}
};
head.appendChild(script);
}
})(this);