OSDN Git Service

Version 0.6.90, performance fix for HTML5Audio & rename SilverLight -> Silverlight.
[pettanr/clientJs.git] / 0.6.x / js / 00_core / 00_builtin.js
1 /**\r
2  * @preserve Copyright 2012-2014 pettanR team.\r
3  * https://sourceforge.jp/projects/pettanr/\r
4  * BSD 3-Clause License\r
5  */\r
6 \r
7 /* \r
8  * ビルトインオブジェクトに拡張したい10のメソッド\r
9  * http://d.hatena.ne.jp/ofk/20080922/1222047483\r
10  */\r
11 \r
12 Function.prototype.apply || (Function.prototype.apply = function (x, y) {\r
13         var a, i, r, j;\r
14         x = x || window;\r
15         y = y || [];\r
16         \r
17         // apply 内で apply を呼んだ場合に備える\r
18         if( x === window ){\r
19                 x.__apply = void 0;\r
20         } else {\r
21                 if( x.constructor && x.constructor.prototype.__apply ){\r
22                         delete x.constructor.prototype.__apply;\r
23                 } else\r
24                 if( x.__apply ) delete x.__apply;\r
25         };\r
26         \r
27         x.__apply = this;\r
28         if (!x.__apply) x.constructor.prototype.__apply = this;\r
29         j = y.length;\r
30         switch (j) {\r
31                 case 0: r = x.__apply(); break;\r
32                 case 1: r = x.__apply(y[0]); break;\r
33                 case 2: r = x.__apply(y[0], y[1]); break;\r
34                 case 3: r = x.__apply(y[0], y[1], y[2]); break;\r
35                 case 4: r = x.__apply(y[0], y[1], y[2], y[3]); break;\r
36                 case 5: r = x.__apply(y[0], y[1], y[2], y[3], y[4]); break;\r
37                 case 6: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5]); break;\r
38                 case 7: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6]); break;\r
39                 case 8: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7]); break;\r
40                 case 9: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8]); break;\r
41                 default:\r
42                         a = [];\r
43                         for (i = 0; i < j; ++i)\r
44                                 a[i] = 'y[' + i + ']';\r
45                         //r = eval('x.__apply(' + a.join(',') + ')');\r
46                         // closuer compiler 対策\r
47                         r = (new Function( 'x,y', 'return x.__apply(' + a.join(',') + ')' ))( x, y );\r
48                         break;\r
49         };\r
50         // ie5\r
51         if( x === window ){\r
52                 x.__apply = void 0;\r
53         } else {\r
54                 //alert( typeof x );\r
55                 if( x.constructor && x.constructor.prototype.__apply ){\r
56                         delete x.constructor.prototype.__apply;\r
57                 } else\r
58                 if( x.__apply ) delete x.__apply;\r
59         };\r
60         return r;\r
61 });\r
62 Function.prototype.call || (Function.prototype.call = function () {\r
63         var a = arguments, x = a[0], y = [], i = 1, j = a.length;\r
64         for (; i < j; ++i)\r
65                 y[i - 1] = a[i];\r
66         return this.apply(x, y);\r
67 });\r
68 \r
69 Array.prototype.pop || (Array.prototype.pop = function () {\r
70         var r = this[this.length - 1];\r
71         --this.length;\r
72         return r;\r
73 });\r
74 Array.prototype.push || (Array.prototype.push = function () {\r
75         var a = arguments, i = 0, j = a.length, l = this.length;\r
76         for (; i < j; ++i)\r
77                 this[l + i] = a[i];\r
78         return this.length;\r
79 });\r
80 Array.prototype.shift || (Array.prototype.shift = function () {\r
81         var r = this[0], i = 1, j = this.length;\r
82         for( ; i < j; ++i)\r
83                 this[i - 1] = this[i];\r
84         --this.length;\r
85         return r;\r
86 });\r
87 Array.prototype.unshift || (Array.prototype.unshift = function () {\r
88         var a = arguments, l = a.length, j = this.length += l - 1, i = j;\r
89         for (; i >= l; --i)\r
90                 this[i] = this[i - l];\r
91         for (i = 0; i < l; ++i)\r
92                 this[i] = a[i];\r
93         return j;\r
94 });\r
95 Array.prototype.splice || (Array.prototype.splice = function (x, y) {\r
96         var a = arguments, s = a.length - 2 - y, r = this.slice(x, x + y),i,j;\r
97         if (s > 0) {\r
98                 for (i = this.length - 1, j = x + y; i >= j; --i)\r
99                         this[i + s] = this[i];\r
100         }\r
101         else if (s < 0) {\r
102                 for (i = x + y, j = this.length; i < j; ++i)\r
103                         this[i + s] = this[i];\r
104                 this.length += s;\r
105         }\r
106         for (i = 2, j = a.length; i < j; ++i)\r
107                 this[i - 2 + x] = a[i];\r
108         return r;\r
109 });\r
110 \r
111 /*\r
112  * original\r
113  * JavaScript 1.6, Array.indexOfを下位互換実装する\r
114  * http://www.inazumatv.com/contents/archives/7965\r
115  */\r
116 \r
117 Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, fromIndex ){\r
118         var l = this.length >>> 0, i;\r
119         \r
120         if( l === 0 ) return -1;\r
121         \r
122         if( fromIndex ){\r
123             i = fromIndex || 0;\r
124             i = i === -Infinity ? 0 : ( i < 0 ? -i : i ) | 0; // Math.floor\r
125             if( l <= i ) return -1;\r
126         };\r
127 \r
128         for( i = 0 <= i ? i : 0 < l + i ? l + i : 0; i < l; ++i ){\r
129             if( this[ i ] === searchElement ) return i;\r
130         };\r
131         return -1;\r
132         });\r
133 \r
134 \r
135 /*\r
136  * Window\r
137  * by http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html\r
138  */\r
139 \r
140 /* 正規表現が使われているため、まだ投入しない itozyun\r
141 window.encodeURI || (window.encodeURI = function (x) {\r
142         return ("" + x).replace(/[^!#$&-;=?-Z_a-z~]/g, function (s) {\r
143                 var c = s.charCodeAt(0), p = "%";\r
144                 return (\r
145                         c < 16 ? "%0" + c.toString(16) :\r
146                         c < 128 ? p + c.toString(16) :\r
147                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
148                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
149                 ).toUpperCase();\r
150         });\r
151 });\r
152 \r
153 window.encodeURIComponent || (window.encodeURIComponent = function (x) {\r
154         return ("" + x).replace(/[^!'-*.0-9A-Z_a-z~-]/g, function (s) {\r
155                 var c = s.charCodeAt(0), p = "%";\r
156                 return (\r
157                         c < 16 ? "%0" + c.toString(16) :\r
158                         c < 128 ? p + c.toString(16) :\r
159                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
160                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
161                 ).toUpperCase();\r
162         });\r
163 });\r
164 \r
165 // 手抜き\r
166 window.decodeURI || (window.decodeURI = function (x) {\r
167         return ("" + x).replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig, function (s) {\r
168                 var c = parseInt(s.substring(1), 16);\r
169                 return String.fromCharCode(\r
170                         c < 128 ? c :\r
171                         c < 224 ? (c & 31) << 6 | parseInt(s.substring(4), 16) & 63 :\r
172                         ((c & 15) << 6 | parseInt(s.substring(4), 16) & 63) << 6 | parseInt(s.substring(7), 16) & 63\r
173                 );\r
174         });\r
175 });\r
176 */\r
177 \r
178 \r
179 //window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);\r
180 \r
181 \r
182 /*\r
183  * String\r
184  */\r
185 \r
186 // replace(RegExp, Function)対応\r
187 /*\r
188 if (window.ActiveXObject ? !Number.prototype.toFixed : (!navigator.taintEnabled && !document.createElement("input").setSelectionRange))\r
189         (function () {\r
190                 var g = String.prototype.replace;\r
191                 String.prototype.replace = function (x, y) {\r
192                         var s = this, z = y;\r
193                         // 第二引数が関数\r
194                         if (y instanceof Function) {\r
195                                 // 第一引数が正規表現\r
196                                 if (x instanceof RegExp) {\r
197                                         // その上、グローバルマッチ\r
198                                         if (x.global || /^\/.*g$/.test(x)) {\r
199                                                 var r = [], m;\r
200                                                 while ((m = x.exec(s)) != null) {\r
201                                                         var i = m.index;\r
202                                                         r[r.length] = s.slice(0, i);\r
203                                                         s = s.slice(i + m[0].length);\r
204                                                         r[r.length] = y.apply(null, m.concat(i, this));\r
205                                                 }\r
206                                                 r[r.length] = s;\r
207                                                 return r.join("");\r
208                                         }\r
209                                         var m = x.exec(s);\r
210                                         if (!m)\r
211                                                 return s;\r
212                                         z = y.apply(null, m.concat(m.index, s));\r
213                                 }\r
214                                 else {\r
215                                         var i = s.indexOf(x);\r
216                                         if (i < 0)\r
217                                                 return s;\r
218                                         z = y(x, i, s);\r
219                                 }\r
220                         }\r
221                         return g.call(s, x, z);\r
222                 };\r
223         })(); */\r
224         \r
225 /*\r
226  * Safari の JavaScript の不備 \r
227  * http://nanto.asablo.jp/blog/2006/01/13/209495\r
228  * \r
229  * web.paulownia.jp - JavaScriptとクロージャ\r
230  * https://web.archive.org/web/20070526063400/http://web.paulownia.jp/script/oop/closure.html\r
231  * MacOSX 10.3のsafariにはhasOwnPropertyが実装されていないので、独自に追加する必要があります。\r
232  */\r
233 /*\r
234 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
235                 var proto = this.constructor && this.constructor.prototype,\r
236                         __p__ = proto && proto.__proto__,\r
237                         v     = this[ p ],\r
238                         r     = false;\r
239                 \r
240                 if( __p__ ) proto.__proto__ = null;\r
241                 \r
242                 if( p in this ){\r
243                         if( v !== v ){\r
244                                 if( proto && ( p in proto ) && proto[ p ] !== proto[ p ] ){ // proto[ p ] is NaN\r
245                                         proto[ p ] = 0; // different value\r
246                                         r = this[ p ] !== this[ p ]; // isNaN?\r
247                                         proto[ p ] = v; // set NaN\r
248                                 } else {\r
249                                         r = true;\r
250                                 };\r
251                         } else\r
252                         if( proto && p in proto && proto[ p ] === v ){\r
253                                 // this と proto に同名で同値が書かれている可能性あり\r
254                                 proto[ p ] = v + ' '; // different value\r
255                                 r = v === this[ p ];\r
256                                 proto[ p ] = v;\r
257                         } else {\r
258                                 r = true;\r
259                         };\r
260                 };\r
261                 \r
262                 if( __p__ ) proto.__proto__ = __p__;\r
263                 \r
264                 return r;\r
265   }); */\r
266 /*\r
267 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
268                 var proto = this.constructor && this.constructor.prototype,\r
269                         __p__ = proto && proto.__proto__,\r
270                         r     = false,//!!( __p__ && ( proto.__proto__ = null ) )\r
271                         _pro_, v, isNaN;\r
272                 \r
273                 if( __p__ ) proto.__proto__ = null;\r
274                 if( this.__proto__ ){\r
275                         _pro_ = this.__proto__;\r
276                         this.__proto__ = null;\r
277                 };\r
278                 \r
279                 if( p === '__proto__' ){\r
280                         r = !!_pro_;\r
281                 } else {\r
282                         v     = this[ p ];\r
283                         isNaN = v !== v;                \r
284                         \r
285                         if( p in this ){\r
286                                 if( proto && p in proto && ( proto[ p ] === v ) ^ isNaN ){ //true + false, false + true\r
287                                         // this と proto に同名で同値が書かれている可能性あり\r
288                                         proto[ p ] = v + ' '; // different value\r
289                                         r = ( v === this[ p ] ) ^ isNaN; // true + false, false + true\r
290                                         proto[ p ] = v;\r
291                                 } else {\r
292                                         r = true;\r
293                                 };\r
294                         };                      \r
295                 };\r
296 \r
297                 if( __p__ ) proto.__proto__ = __p__;\r
298                 if( _p_ ) this.__proto__ = _pro_;\r
299                 return r;\r
300   }); */