@@ -146,6 +146,23 @@ if (!Array.prototype.findIndex) {
146
146
} ) ;
147
147
} ) ( [ Element . prototype , Document . prototype , DocumentFragment . prototype ] ) ;
148
148
149
+ // from: https://github.com/jserz/js_piece/blob/master/DOM/Element/prepend()/prepend().md
150
+ ( function ( arr ) {
151
+ arr . forEach ( function ( item ) {
152
+ item . prepend = item . prepend || function ( ) {
153
+ var argArr = Array . prototype . slice . call ( arguments ) ,
154
+ docFrag = document . createDocumentFragment ( ) ;
155
+
156
+ argArr . forEach ( function ( argItem ) {
157
+ var isNode = argItem instanceof Node ;
158
+ docFrag . appendChild ( isNode ? argItem : document . createTextNode ( String ( argItem ) ) ) ;
159
+ } ) ;
160
+
161
+ this . insertBefore ( docFrag , this . firstChild ) ;
162
+ } ;
163
+ } ) ;
164
+ } ) ( [ Element . prototype , Document . prototype , DocumentFragment . prototype ] ) ;
165
+
149
166
// Production steps of ECMA-262, Edition 5, 15.4.4.18
150
167
// Reference: http://es5.github.io/#x15.4.4.18
151
168
@@ -202,4 +219,138 @@ if (!Array.prototype['forEach']) {
202
219
}
203
220
// 8. return undefined
204
221
} ;
222
+ }
223
+
224
+ // https://developer.mozilla.org/zh-CN/docs/Web/API/CustomEvent/CustomEvent
225
+ ( function ( ) {
226
+ try {
227
+ new window . CustomEvent ( 'T' ) ;
228
+ } catch ( e ) {
229
+ var CustomEvent = function ( event , params ) {
230
+ params = params || { bubbles : false , cancelable : false , detail : undefined } ;
231
+
232
+ var evt = document . createEvent ( 'CustomEvent' ) ;
233
+
234
+ evt . initCustomEvent ( event , params . bubbles , params . cancelable , params . detail ) ;
235
+
236
+ return evt ;
237
+ } ;
238
+
239
+ CustomEvent . prototype = window . Event . prototype ;
240
+
241
+ window . CustomEvent = CustomEvent ;
242
+ }
243
+ } ) ( ) ;
244
+
245
+ // https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
246
+ ( function ( arr ) {
247
+ arr . forEach ( function ( item ) {
248
+ if ( item . hasOwnProperty ( 'remove' ) ) {
249
+ return ;
250
+ }
251
+ Object . defineProperty ( item , 'remove' , {
252
+ configurable : true ,
253
+ enumerable : true ,
254
+ writable : true ,
255
+ value : function remove ( ) {
256
+ this . parentNode . removeChild ( this ) ;
257
+ }
258
+ } ) ;
259
+ } ) ;
260
+ } ) ( [ Element . prototype , CharacterData . prototype , DocumentType . prototype ] ) ;
261
+
262
+ // https://developer.mozilla.org/zh-CN/docs/Web/API/Element/matches
263
+ if ( ! Element . prototype . matches ) {
264
+ Element . prototype . matches =
265
+ Element . prototype . matchesSelector ||
266
+ Element . prototype . mozMatchesSelector ||
267
+ Element . prototype . msMatchesSelector ||
268
+ Element . prototype . oMatchesSelector ||
269
+ Element . prototype . webkitMatchesSelector ||
270
+ function ( s ) {
271
+ var matches = ( this . document || this . ownerDocument ) . querySelectorAll ( s ) ,
272
+ i = matches . length ;
273
+ while ( -- i >= 0 && matches . item ( i ) !== this ) { }
274
+ return i > - 1 ;
275
+ } ;
276
+ }
277
+
278
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
279
+ // Production steps of ECMA-262, Edition 6, 22.1.2.1
280
+ if ( ! Array . from ) {
281
+ Array . from = ( function ( ) {
282
+ var toStr = Object . prototype . toString ;
283
+ var isCallable = function ( fn ) {
284
+ return typeof fn === 'function' || toStr . call ( fn ) === '[object Function]' ;
285
+ } ;
286
+ var toInteger = function ( value ) {
287
+ var number = Number ( value ) ;
288
+ if ( isNaN ( number ) ) { return 0 ; }
289
+ if ( number === 0 || ! isFinite ( number ) ) { return number ; }
290
+ return ( number > 0 ? 1 : - 1 ) * Math . floor ( Math . abs ( number ) ) ;
291
+ } ;
292
+ var maxSafeInteger = Math . pow ( 2 , 53 ) - 1 ;
293
+ var toLength = function ( value ) {
294
+ var len = toInteger ( value ) ;
295
+ return Math . min ( Math . max ( len , 0 ) , maxSafeInteger ) ;
296
+ } ;
297
+
298
+ // The length property of the from method is 1.
299
+ return function from ( arrayLike /*, mapFn, thisArg */ ) {
300
+ // 1. Let C be the this value.
301
+ var C = this ;
302
+
303
+ // 2. Let items be ToObject(arrayLike).
304
+ var items = Object ( arrayLike ) ;
305
+
306
+ // 3. ReturnIfAbrupt(items).
307
+ if ( arrayLike == null ) {
308
+ throw new TypeError ( "Array.from requires an array-like object - not null or undefined" ) ;
309
+ }
310
+
311
+ // 4. If mapfn is undefined, then let mapping be false.
312
+ var mapFn = arguments . length > 1 ? arguments [ 1 ] : void undefined ;
313
+ var T ;
314
+ if ( typeof mapFn !== 'undefined' ) {
315
+ // 5. else
316
+ // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
317
+ if ( ! isCallable ( mapFn ) ) {
318
+ throw new TypeError ( 'Array.from: when provided, the second argument must be a function' ) ;
319
+ }
320
+
321
+ // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
322
+ if ( arguments . length > 2 ) {
323
+ T = arguments [ 2 ] ;
324
+ }
325
+ }
326
+
327
+ // 10. Let lenValue be Get(items, "length").
328
+ // 11. Let len be ToLength(lenValue).
329
+ var len = toLength ( items . length ) ;
330
+
331
+ // 13. If IsConstructor(C) is true, then
332
+ // 13. a. Let A be the result of calling the [[Construct]] internal method
333
+ // of C with an argument list containing the single item len.
334
+ // 14. a. Else, Let A be ArrayCreate(len).
335
+ var A = isCallable ( C ) ? Object ( new C ( len ) ) : new Array ( len ) ;
336
+
337
+ // 16. Let k be 0.
338
+ var k = 0 ;
339
+ // 17. Repeat, while k < len… (also steps a - h)
340
+ var kValue ;
341
+ while ( k < len ) {
342
+ kValue = items [ k ] ;
343
+ if ( mapFn ) {
344
+ A [ k ] = typeof T === 'undefined' ? mapFn ( kValue , k ) : mapFn . call ( T , kValue , k ) ;
345
+ } else {
346
+ A [ k ] = kValue ;
347
+ }
348
+ k += 1 ;
349
+ }
350
+ // 18. Let putStatus be Put(A, "length", len, true).
351
+ A . length = len ;
352
+ // 20. Return A.
353
+ return A ;
354
+ } ;
355
+ } ( ) ) ;
205
356
}
0 commit comments