Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
8b8ac608
Commit
8b8ac608
authored
Jun 19, 2017
by
Sean Hammond
Committed by
GitHub
Jun 19, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #443 from hypothesis/local-storage-decaf
Convert localStorage service to JS
parents
e1388ab4
5e1c1910
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
116 deletions
+152
-116
local-storage.coffee
src/sidebar/local-storage.coffee
+0
-35
local-storage.js
src/sidebar/local-storage.js
+68
-0
local-storage-test.coffee
src/sidebar/test/local-storage-test.coffee
+0
-81
local-storage-test.js
src/sidebar/test/local-storage-test.js
+84
-0
No files found.
src/sidebar/local-storage.coffee
deleted
100644 → 0
View file @
e1388ab4
module
.
exports
=
[
'$window'
,
(
$window
)
->
# Detection is needed because we run often as a third party widget and
# third party storage blocking often blocks cookies and local storage
# https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js
storage
=
do
->
key
=
'hypothesis.testKey'
try
$window
.
localStorage
.
setItem
key
,
key
$window
.
localStorage
.
removeItem
key
$window
.
localStorage
catch
memoryStorage
=
{}
getItem
:
(
key
)
->
if
key
of
memoryStorage
then
memoryStorage
[
key
]
else
null
setItem
:
(
key
,
value
)
->
memoryStorage
[
key
]
=
value
removeItem
:
(
key
)
->
delete
memoryStorage
[
key
]
return
{
getItem
:
(
key
)
->
storage
.
getItem
key
getObject
:
(
key
)
->
json
=
storage
.
getItem
key
return
JSON
.
parse
json
if
json
null
setItem
:
(
key
,
value
)
->
storage
.
setItem
key
,
value
setObject
:
(
key
,
value
)
->
repr
=
JSON
.
stringify
value
storage
.
setItem
key
,
repr
removeItem
:
(
key
)
->
storage
.
removeItem
key
}
]
src/sidebar/local-storage.js
0 → 100644
View file @
8b8ac608
'use strict'
;
/**
* Fallback in-memory store if `localStorage` is not read/writable.
*/
class
InMemoryStorage
{
constructor
()
{
this
.
_store
=
{};
}
getItem
(
key
)
{
return
(
key
in
this
.
_store
)
?
this
.
_store
[
key
]
:
null
;
}
setItem
(
key
,
value
)
{
this
.
_store
[
key
]
=
value
;
}
removeItem
(
key
)
{
delete
this
.
_store
[
key
];
}
}
/**
* A wrapper around the `localStorage` API which provides a fallback to
* in-memory storage in browsers that block access to `window.localStorage`.
* in third-party iframes.
*/
// @ngInject
function
localStorage
(
$window
)
{
var
storage
=
$window
.
localStorage
;
try
{
// Test whether we can read/write localStorage.
var
key
=
'hypothesis.testKey'
;
$window
.
localStorage
.
setItem
(
key
,
key
);
$window
.
localStorage
.
getItem
(
key
);
$window
.
localStorage
.
removeItem
(
key
);
}
catch
(
e
)
{
storage
=
new
InMemoryStorage
();
}
return
{
getItem
(
key
)
{
return
storage
.
getItem
(
key
);
},
getObject
(
key
)
{
var
item
=
storage
.
getItem
(
key
);
return
item
?
JSON
.
parse
(
item
)
:
null
;
},
setItem
(
key
,
value
)
{
storage
.
setItem
(
key
,
value
);
},
setObject
(
key
,
value
)
{
var
repr
=
JSON
.
stringify
(
value
);
storage
.
setItem
(
key
,
repr
);
},
removeItem
(
key
)
{
storage
.
removeItem
(
key
);
},
};
}
module
.
exports
=
localStorage
;
src/sidebar/test/local-storage-test.coffee
deleted
100644 → 0
View file @
e1388ab4
{
module
,
inject
}
=
angular
.
mock
describe
'localStorage'
,
->
fakeWindow
=
null
sandbox
=
null
before
->
angular
.
module
(
'h'
,
[])
.
service
(
'localStorage'
,
require
(
'../local-storage'
))
beforeEach
module
(
'h'
)
describe
'memory fallback'
,
->
localStorage
=
null
key
=
null
beforeEach
module
(
$provide
)
->
sandbox
=
sinon
.
sandbox
.
create
()
fakeWindow
=
{
localStorage
:
{}
}
$provide
.
value
'$window'
,
fakeWindow
return
afterEach
->
sandbox
.
restore
()
beforeEach
inject
(
_localStorage_
)
->
localStorage
=
_localStorage_
key
=
'test.memory.key'
it
'sets/gets Item'
,
->
value
=
'What shall we do with a drunken sailor?'
localStorage
.
setItem
key
,
value
actual
=
localStorage
.
getItem
key
assert
.
equal
value
,
actual
it
'removes item'
,
->
localStorage
.
setItem
key
,
''
localStorage
.
removeItem
key
result
=
localStorage
.
getItem
key
assert
.
isNull
result
it
'sets/gets Object'
,
->
data
=
{
'foo'
:
'bar'
}
localStorage
.
setObject
key
,
data
stringified
=
localStorage
.
getItem
key
assert
.
equal
stringified
,
JSON
.
stringify
data
actual
=
localStorage
.
getObject
key
assert
.
deepEqual
actual
,
data
describe
'browser localStorage'
,
->
localStorage
=
null
beforeEach
module
(
$provide
)
->
sandbox
=
sinon
.
sandbox
.
create
()
fakeWindow
=
{
localStorage
:
{
getItem
:
sandbox
.
stub
()
setItem
:
sandbox
.
stub
()
removeItem
:
sandbox
.
stub
()
}
}
$provide
.
value
'$window'
,
fakeWindow
return
afterEach
->
sandbox
.
restore
()
beforeEach
inject
(
_localStorage_
)
->
localStorage
=
_localStorage_
it
'uses window.localStorage functions to handle data'
,
->
key
=
'test.storage.key'
data
=
'test data'
localStorage
.
setItem
key
,
data
assert
.
calledWith
fakeWindow
.
localStorage
.
setItem
,
key
,
data
src/sidebar/test/local-storage-test.js
0 → 100644
View file @
8b8ac608
'use strict'
;
var
angular
=
require
(
'angular'
);
describe
(
'sidebar.localStorage'
,
()
=>
{
var
fakeWindow
;
before
(()
=>
angular
.
module
(
'h'
,
[])
.
service
(
'localStorage'
,
require
(
'../local-storage'
))
);
context
(
'when browser localStorage is *not* accessible'
,
()
=>
{
var
localStorage
=
null
;
var
key
=
null
;
beforeEach
(()
=>
{
angular
.
mock
.
module
(
'h'
,
{
$window
:
{
localStorage
:
{},
},
});
});
beforeEach
(
angular
.
mock
.
inject
((
_localStorage_
)
=>
{
localStorage
=
_localStorage_
;
key
=
'test.memory.key'
;
}));
it
(
'sets/gets Item'
,
()
=>
{
var
value
=
'What shall we do with a drunken sailor?'
;
localStorage
.
setItem
(
key
,
value
);
var
actual
=
localStorage
.
getItem
(
key
);
assert
.
equal
(
value
,
actual
);
});
it
(
'removes item'
,
()
=>
{
localStorage
.
setItem
(
key
,
''
);
localStorage
.
removeItem
(
key
);
var
result
=
localStorage
.
getItem
(
key
);
assert
.
isNull
(
result
);
});
it
(
'sets/gets Object'
,
()
=>
{
var
data
=
{
'foo'
:
'bar'
};
localStorage
.
setObject
(
key
,
data
);
var
stringified
=
localStorage
.
getItem
(
key
);
assert
.
equal
(
stringified
,
JSON
.
stringify
(
data
));
var
actual
=
localStorage
.
getObject
(
key
);
assert
.
deepEqual
(
actual
,
data
);
});
});
context
(
'when browser localStorage is accessible'
,
()
=>
{
var
localStorage
;
beforeEach
(()
=>
{
fakeWindow
=
{
localStorage
:
{
getItem
:
sinon
.
stub
(),
setItem
:
sinon
.
stub
(),
removeItem
:
sinon
.
stub
(),
},
};
angular
.
mock
.
module
(
'h'
,
{
$window
:
fakeWindow
,
});
});
beforeEach
(()
=>
{
angular
.
mock
.
inject
(
_localStorage_
=>
localStorage
=
_localStorage_
);
});
it
(
'uses window.localStorage functions to handle data'
,
()
=>
{
var
key
=
'test.storage.key'
;
var
data
=
'test data'
;
localStorage
.
setItem
(
key
,
data
);
assert
.
calledWith
(
fakeWindow
.
localStorage
.
setItem
,
key
,
data
);
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment