OSDN Git Service

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