Skip to content

Commit

Permalink
harden memory recall logic to be fail-safe
Browse files Browse the repository at this point in the history
Hardened the CSS class memory recall function against critical inpupt errors in exceptional cases.

One could never know if this code will run in the apps or hardware for critical life support.
Or maybe inside of machines that need to work 100% all the time to support safety of humans.


Now the functon exits the loop, if any errors of the input variables are found.
Limits possible attack vectors, where safety is an issue.


If you want to test the function for critical errors or with false data input,
use those console log variables for testing.

Fake the input data and see what happens in the console logs.


   console.log( "load state was: " + document.readyState ) ;

   console.log( "Tag : " + Tag ) ;

   console.log( "TagNotAlive : " + TagNotAlive ) ;

   console.log( "MemoryName : " + MemoryName ) ;

   console.log( "MemoryValue : " + MemoryValue ) ;
  • Loading branch information
Dorson authored Apr 22, 2020
1 parent 3408781 commit 10dd683
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,42 @@
window.onerror = null ;


/* During load time, we remember the saved CSS classes from local memory */
/*
During load time, we remember the saved CSS classes from localStorage
Only works with HTML Tags that are already loaded before the script run !
Optimally the HTML tag. Works for the first Tag of it's type only !
*/

document.addEventListener( 'DOMContentLoaded' , function() {

RememberClasses() ;

});

document.addEventListener("DOMContentLoaded", RememberClasses() , true ) ;

function RememberClasses() {

var ClassMemory = [
const ClassMemory = [
["html" , "ColorMode"] ,
["html" , "TextSize"]
] ;

for ( let [ Tag, MemoryName ] of ClassMemory ) {
/* On Errors we skip one loop. */
if (!localStorage.getItem( MemoryName ) || !document.querySelector( Tag ) ) {

for ( let [ Tag , MemoryName ] of ClassMemory ) {

let TagNotAlive = !(document.querySelector( Tag )) ;

let MemoryValue = window.localStorage.getItem( MemoryName ) ;

/* On logic Errors we skip one loop. */

if ( !MemoryValue || TagNotAlive ) {
continue ;
}
else {

/* Else we remember and set CSS classes. */
var MemoryValue = localStorage.getItem( MemoryName );
else {

document.querySelector( Tag ).classList.add( MemoryValue );
}
}
Expand Down

0 comments on commit 10dd683

Please sign in to comment.