UNPKG

5.69 kBJavaScriptView Raw
1;(function(win){
2 var store = {},
3 doc = win.document,
4 localStorageName = 'localStorage',
5 storage
6
7 store.disabled = false
8 store.set = function(key, value) {}
9 store.get = function(key) {}
10 store.remove = function(key) {}
11 store.clear = function() {}
12 store.transact = function(key, defaultVal, transactionFn) {
13 var val = store.get(key)
14 if (transactionFn == null) {
15 transactionFn = defaultVal
16 defaultVal = null
17 }
18 if (typeof val == 'undefined') { val = defaultVal || {} }
19 transactionFn(val)
20 store.set(key, val)
21 }
22 store.getAll = function() {}
23 store.forEach = function() {}
24
25 store.serialize = function(value) {
26 return JSON.stringify(value)
27 }
28 store.deserialize = function(value) {
29 if (typeof value != 'string') { return undefined }
30 try { return JSON.parse(value) }
31 catch(e) { return value || undefined }
32 }
33
34 // Functions to encapsulate questionable FireFox 3.6.13 behavior
35 // when about.config::dom.storage.enabled === false
36 // See https://github.com/marcuswestin/store.js/issues#issue/13
37 function isLocalStorageNameSupported() {
38 try { return (localStorageName in win && win[localStorageName]) }
39 catch(err) { return false }
40 }
41
42 if (isLocalStorageNameSupported()) {
43 storage = win[localStorageName]
44 store.set = function(key, val) {
45 if (val === undefined) { return store.remove(key) }
46 storage.setItem(key, store.serialize(val))
47 return val
48 }
49 store.get = function(key) { return store.deserialize(storage.getItem(key)) }
50 store.remove = function(key) { storage.removeItem(key) }
51 store.clear = function() { storage.clear() }
52 store.getAll = function() {
53 var ret = {}
54 store.forEach(function(key, val) {
55 ret[key] = val
56 })
57 return ret
58 }
59 store.forEach = function(callback) {
60 for (var i=0; i<storage.length; i++) {
61 var key = storage.key(i)
62 callback(key, store.get(key))
63 }
64 }
65 } else if (doc.documentElement.addBehavior) {
66 var storageOwner,
67 storageContainer
68 // Since #userData storage applies only to specific paths, we need to
69 // somehow link our data to a specific path. We choose /favicon.ico
70 // as a pretty safe option, since all browsers already make a request to
71 // this URL anyway and being a 404 will not hurt us here. We wrap an
72 // iframe pointing to the favicon in an ActiveXObject(htmlfile) object
73 // (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
74 // since the iframe access rules appear to allow direct access and
75 // manipulation of the document element, even for a 404 page. This
76 // document can be used instead of the current document (which would
77 // have been limited to the current path) to perform #userData storage.
78 try {
79 storageContainer = new ActiveXObject('htmlfile')
80 storageContainer.open()
81 storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></iframe>')
82 storageContainer.close()
83 storageOwner = storageContainer.w.frames[0].document
84 storage = storageOwner.createElement('div')
85 } catch(e) {
86 // somehow ActiveXObject instantiation failed (perhaps some special
87 // security settings or otherwse), fall back to per-path storage
88 storage = doc.createElement('div')
89 storageOwner = doc.body
90 }
91 function withIEStorage(storeFunction) {
92 return function() {
93 var args = Array.prototype.slice.call(arguments, 0)
94 args.unshift(storage)
95 // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
96 // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
97 storageOwner.appendChild(storage)
98 storage.addBehavior('#default#userData')
99 storage.load(localStorageName)
100 var result = storeFunction.apply(store, args)
101 storageOwner.removeChild(storage)
102 return result
103 }
104 }
105
106 // In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
107 var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
108 function ieKeyFix(key) {
109 return key.replace(forbiddenCharsRegex, '___')
110 }
111 store.set = withIEStorage(function(storage, key, val) {
112 key = ieKeyFix(key)
113 if (val === undefined) { return store.remove(key) }
114 storage.setAttribute(key, store.serialize(val))
115 storage.save(localStorageName)
116 return val
117 })
118 store.get = withIEStorage(function(storage, key) {
119 key = ieKeyFix(key)
120 return store.deserialize(storage.getAttribute(key))
121 })
122 store.remove = withIEStorage(function(storage, key) {
123 key = ieKeyFix(key)
124 storage.removeAttribute(key)
125 storage.save(localStorageName)
126 })
127 store.clear = withIEStorage(function(storage) {
128 var attributes = storage.XMLDocument.documentElement.attributes
129 storage.load(localStorageName)
130 for (var i=0, attr; attr=attributes[i]; i++) {
131 storage.removeAttribute(attr.name)
132 }
133 storage.save(localStorageName)
134 })
135 store.getAll = withIEStorage(function(storage) {
136 var ret = {}
137 store.forEach(function(key, val) {
138 ret[key] = val
139 })
140 return ret
141 })
142 store.forEach = withIEStorage(function(storage, callback) {
143 var attributes = storage.XMLDocument.documentElement.attributes
144 for (var i=0, attr; attr=attributes[i]; ++i) {
145 callback(attr.name, store.deserialize(storage.getAttribute(key)))
146 }
147 })
148 }
149
150 try {
151 var testKey = '__storejs__'
152 store.set(testKey, testKey)
153 if (store.get(testKey) != testKey) { store.disabled = true }
154 store.remove(testKey)
155 } catch(e) {
156 store.disabled = true
157 }
158 store.enabled = !store.disabled
159
160 if (typeof module != 'undefined' && module.exports) { module.exports = store }
161 else if (typeof define === 'function' && define.amd) { define(store) }
162 else { win.store = store }
163
164})(this.window || global);