Skip to content

Commit

Permalink
htmlfiles: Handle localStorage not accessible
Browse files Browse the repository at this point in the history
In some cases, if based on the browser's settings - localStorage
is not accessible, fallback and don't save the sort-columns into
localStorage.
While the UX is a little inconvenient, at least the page doesn't
break - sorting on columns is still possible, but not retained
between pages.
  • Loading branch information
AbdealiLoKo authored and nedbat committed Feb 29, 2020
1 parent 0f82d27 commit 48c73b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ Unreleased

- Windows 3.8 wheels were incorrectly built, but are now fixed. (`issue 949`_)

- HTML report couldn't be sorted if localStorage wasn't available. This is now
fixed: sorting works even though the sorting setting isn't retained. (`issue
944`_ and `pull request 945`_). Thanks, Abdeali Kothari.

.. _issue 943: https://github.com/nedbat/coveragepy/issues/943
.. _issue 944: https://github.com/nedbat/coveragepy/issues/944
.. _pull request 945: https://github.com/nedbat/coveragepy/pull/945
.. _issue 949: https://github.com/nedbat/coveragepy/issues/949


Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extended and maintained by Ned Batchelder.
Other contributions, including writing code, updating docs, and submitting
useful bug reports, have been made by:

Abdeali Kothari
Adi Roiban
Agbonze O. Jeremiah
Albertas Agejevas
Expand Down
9 changes: 7 additions & 2 deletions coverage/htmlfiles/coverage_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ coverage.index_ready = function ($) {
// Look for a localStorage item containing previous sort settings:
var sort_list = [];
var storage_name = "COVERAGE_INDEX_SORT";
var stored_list = localStorage.getItem(storage_name);
var stored_list = undefined;
try {
stored_list = localStorage.getItem(storage_name);
} catch(err) {}

if (stored_list) {
sort_list = JSON.parse('[[' + stored_list + ']]');
Expand Down Expand Up @@ -222,7 +225,9 @@ coverage.index_ready = function ($) {

// Watch for page unload events so we can save the final sort settings:
$(window).unload(function () {
localStorage.setItem(storage_name, sort_list.toString())
try {
localStorage.setItem(storage_name, sort_list.toString())
} catch(err) {}
});
};

Expand Down

0 comments on commit 48c73b8

Please sign in to comment.