From 48c73b88bf2f3a5cdb404b20afa34cb2cb940e0f Mon Sep 17 00:00:00 2001 From: AbdealiJK Date: Tue, 25 Feb 2020 01:06:31 +0530 Subject: [PATCH] htmlfiles: Handle localStorage not accessible 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. --- CHANGES.rst | 6 ++++++ CONTRIBUTORS.txt | 1 + coverage/htmlfiles/coverage_html.js | 9 +++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6efe083c7..37c0ddf67 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 65919f1e4..a89c3bfa6 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -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 diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js index 22152333e..3bf04bf92 100644 --- a/coverage/htmlfiles/coverage_html.js +++ b/coverage/htmlfiles/coverage_html.js @@ -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 + ']]'); @@ -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) {} }); };