store.js 6.4 KB

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