PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/files//1.3.6/store.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 175 lines | 129 code | 8 blank | 38 comment | 27 complexity | 54ed01b68e6fe5731e84d2bf40b03d62 MD5 | raw file
  1. /* Copyright (c) 2010-2012 Marcus Westin
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. ;(function(){
  22. var store = {},
  23. win = window,
  24. doc = win.document,
  25. localStorageName = 'localStorage',
  26. namespace = '__storejs__',
  27. storage
  28. store.disabled = false
  29. store.set = function(key, value) {}
  30. store.get = function(key) {}
  31. store.remove = function(key) {}
  32. store.clear = function() {}
  33. store.transact = function(key, defaultVal, transactionFn) {
  34. var val = store.get(key)
  35. if (transactionFn == null) {
  36. transactionFn = defaultVal
  37. defaultVal = null
  38. }
  39. if (typeof val == 'undefined') { val = defaultVal || {} }
  40. transactionFn(val)
  41. store.set(key, val)
  42. }
  43. store.getAll = function() {}
  44. store.serialize = function(value) {
  45. return JSON.stringify(value)
  46. }
  47. store.deserialize = function(value) {
  48. if (typeof value != 'string') { return undefined }
  49. try { return JSON.parse(value) }
  50. catch(e) { return value || undefined }
  51. }
  52. // Functions to encapsulate questionable FireFox 3.6.13 behavior
  53. // when about.config::dom.storage.enabled === false
  54. // See https://github.com/marcuswestin/store.js/issues#issue/13
  55. function isLocalStorageNameSupported() {
  56. try { return (localStorageName in win && win[localStorageName]) }
  57. catch(err) { return false }
  58. }
  59. if (isLocalStorageNameSupported()) {
  60. storage = win[localStorageName]
  61. store.set = function(key, val) {
  62. if (val === undefined) { return store.remove(key) }
  63. storage.setItem(key, store.serialize(val))
  64. return val
  65. }
  66. store.get = function(key) { return store.deserialize(storage.getItem(key)) }
  67. store.remove = function(key) { storage.removeItem(key) }
  68. store.clear = function() { storage.clear() }
  69. store.getAll = function() {
  70. var ret = {}
  71. for (var i=0; i<storage.length; ++i) {
  72. var key = storage.key(i)
  73. ret[key] = store.get(key)
  74. }
  75. return ret
  76. }
  77. } else if (doc.documentElement.addBehavior) {
  78. var storageOwner,
  79. storageContainer
  80. // Since #userData storage applies only to specific paths, we need to
  81. // somehow link our data to a specific path. We choose /favicon.ico
  82. // as a pretty safe option, since all browsers already make a request to
  83. // this URL anyway and being a 404 will not hurt us here. We wrap an
  84. // iframe pointing to the favicon in an ActiveXObject(htmlfile) object
  85. // (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
  86. // since the iframe access rules appear to allow direct access and
  87. // manipulation of the document element, even for a 404 page. This
  88. // document can be used instead of the current document (which would
  89. // have been limited to the current path) to perform #userData storage.
  90. try {
  91. storageContainer = new ActiveXObject('htmlfile')
  92. storageContainer.open()
  93. storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></frame>')
  94. storageContainer.close()
  95. storageOwner = storageContainer.w.frames[0].document
  96. storage = storageOwner.createElement('div')
  97. } catch(e) {
  98. // somehow ActiveXObject instantiation failed (perhaps some special
  99. // security settings or otherwse), fall back to per-path storage
  100. storage = doc.createElement('div')
  101. storageOwner = doc.body
  102. }
  103. function withIEStorage(storeFunction) {
  104. return function() {
  105. var args = Array.prototype.slice.call(arguments, 0)
  106. args.unshift(storage)
  107. // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
  108. // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
  109. storageOwner.appendChild(storage)
  110. storage.addBehavior('#default#userData')
  111. storage.load(localStorageName)
  112. var result = storeFunction.apply(store, args)
  113. storageOwner.removeChild(storage)
  114. return result
  115. }
  116. }
  117. // In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
  118. var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
  119. function ieKeyFix(key) {
  120. return key.replace(forbiddenCharsRegex, '___')
  121. }
  122. store.set = withIEStorage(function(storage, key, val) {
  123. key = ieKeyFix(key)
  124. if (val === undefined) { return store.remove(key) }
  125. storage.setAttribute(key, store.serialize(val))
  126. storage.save(localStorageName)
  127. return val
  128. })
  129. store.get = withIEStorage(function(storage, key) {
  130. key = ieKeyFix(key)
  131. return store.deserialize(storage.getAttribute(key))
  132. })
  133. store.remove = withIEStorage(function(storage, key) {
  134. key = ieKeyFix(key)
  135. storage.removeAttribute(key)
  136. storage.save(localStorageName)
  137. })
  138. store.clear = withIEStorage(function(storage) {
  139. var attributes = storage.XMLDocument.documentElement.attributes
  140. storage.load(localStorageName)
  141. for (var i=0, attr; attr=attributes[i]; i++) {
  142. storage.removeAttribute(attr.name)
  143. }
  144. storage.save(localStorageName)
  145. })
  146. store.getAll = withIEStorage(function(storage) {
  147. var attributes = storage.XMLDocument.documentElement.attributes
  148. storage.load(localStorageName)
  149. var ret = {}
  150. for (var i=0, attr; attr=attributes[i]; ++i) {
  151. ret[attr] = store.get(attr)
  152. }
  153. return ret
  154. })
  155. }
  156. try {
  157. store.set(namespace, namespace)
  158. if (store.get(namespace) != namespace) { store.disabled = true }
  159. store.remove(namespace)
  160. } catch(e) {
  161. store.disabled = true
  162. }
  163. store.enabled = !store.disabled
  164. if (typeof module != 'undefined' && typeof module != 'function') { module.exports = store }
  165. else if (typeof define === 'function' && define.amd) { define(store) }
  166. else { this.store = store }
  167. })();