polyfill-service
Version:
A polyfill combinator
1 lines • 3.91 kB
JSON
{"aliases":["caniuse:namevalue-storage","modernizr:localstorage"],"browsers":{"ie":"6 - 7","op_mini":"*"},"dependencies":["Window"],"docs":"https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage","spec":"http://dev.w3.org/html5/webstorage/#dom-localstorage","baseDir":"localStorage","hasTests":false,"rawSource":"\n// localStorage\n(function (global) {\n\tfunction Storage() {}\n\n\tStorage.prototype = {\n\t\tclear: function () {\n\t\t\tgetKeys(this).forEach(this.removeItem, this);\n\t\t},\n\t\tconstructor: Storage,\n\t\tgetItem: function () {\n\t\t\tvar key = String(arguments[0]);\n\n\t\t\treturn key in this ? this[key] : null;\n\t\t},\n\t\tkey: function () {\n\t\t\tvar index = parseInt(arguments[0], 10) || 0;\n\n\t\t\treturn getKeys(this)[index] || null;\n\t\t},\n\t\tremoveItem: function () {\n\t\t\tvar key = String(arguments[0]);\n\n\t\t\tfor (key in this) {\n\t\t\t\tdelete this[key];\n\n\t\t\t\t--this.length;\n\t\t\t}\n\n\t\t\tupdateKeys();\n\t\t},\n\t\tsetItem: function () {\n\t\t\tvar key = String(arguments[0]), value = String(arguments[1]);\n\n\t\t\tif (!(key in this)) {\n\t\t\t\t++this.length;\n\t\t\t}\n\n\t\t\tthis[key] = value;\n\n\t\t\tupdateKeys();\n\t\t}\n\t};\n\n\tfunction getKeys(object) {\n\t\tvar buffer = [], key;\n\n\t\tfor (key in object) {\n\t\t\tif (Object.prototype.hasOwnProperty.call(object, key) && key !== 'length') {\n\t\t\t\tbuffer.push(key);\n\t\t\t}\n\t\t}\n\n\t\treturn buffer;\n\t}\n\n\tfunction updateKeys() {\n\t\tvar unloadkeys = keys;\n\n\t\tkeys = getKeys(localStorage);\n\n\t\tunloadkeys.concat(keys).forEach(function (key) {\n\t\t\tif (key in localStorage) {\n\t\t\t\telement.setAttribute(userdata + key, localStorage[key]);\n\t\t\t} else {\n\t\t\t\telement.removeAttribute(userdata + key);\n\t\t\t}\n\t\t});\n\n\t\telement.setAttribute(userdata, keys.join(','));\n\n\t\telement.save(userdata);\n\t}\n\n\tif (!global.localStorage) {\n\t\tvar\n\t\t// <Global>.localStorage\n\t\tlocalStorage = global.localStorage = new Storage(),\n\t\t// set storage element\n\t\telement = global.document.lastChild.lastChild.appendChild(global.document.createElement('x-local-storage')),\n\t\t// set userdata key and prefix\n\t\tuserdata = 'userdata',\n\t\tkeys;\n\n\t\t// proprietary ie local storage\n\t\ttry {\n\t\t\telement.addBehavior('#default#' + userdata);\n\t\t\telement.load(userdata);\n\t\t} catch (error) {}\n\n\t\t// get keys\n\t\tkeys = element.getAttribute(userdata) ? element.getAttribute(userdata).split(',') : [];\n\n\t\tlocalStorage.length = keys.length;\n\n\t\t// assign keys to localStorage\n\t\tkeys.forEach(function (key) {\n\t\t\tlocalStorage[key] = element.getAttribute(userdata + key);\n\t\t});\n\n\t\tglobal.attachEvent('onunload', updateKeys);\n\t}\n})(this);\n","minSource":"!function(t){function e(){}function n(t){var e,n=[];for(e in t)Object.prototype.hasOwnProperty.call(t,e)&&\"length\"!==e&&n.push(e);return n}function i(){var t=r;r=n(o),t.concat(r).forEach(function(t){t in o?a.setAttribute(c+t,o[t]):a.removeAttribute(c+t)}),a.setAttribute(c,r.join(\",\")),a.save(c)}if(e.prototype={clear:function(){n(this).forEach(this.removeItem,this)},constructor:e,getItem:function(){var t=String(arguments[0]);return t in this?this[t]:null},key:function(){var t=parseInt(arguments[0],10)||0;return n(this)[t]||null},removeItem:function(){var t=String(arguments[0]);for(t in this)delete this[t],--this.length;i()},setItem:function(){var t=String(arguments[0]),e=String(arguments[1]);t in this||++this.length,this[t]=e,i()}},!t.localStorage){var r,o=t.localStorage=new e,a=t.document.lastChild.lastChild.appendChild(t.document.createElement(\"x-local-storage\")),c=\"userdata\";try{a.addBehavior(\"#default#\"+c),a.load(c)}catch(h){}r=a.getAttribute(c)?a.getAttribute(c).split(\",\"):[],o.length=r.length,r.forEach(function(t){o[t]=a.getAttribute(c+t)}),t.attachEvent(\"onunload\",i)}}(this);","detectSource":"'localStorage' in this"}