1313var currentTheme = document . getElementById ( "themeStyle" ) ;
1414var mainTheme = document . getElementById ( "mainThemeStyle" ) ;
1515
16+ var savedHref = [ ] ;
17+
18+ function onEach ( arr , func ) {
19+ if ( arr && arr . length > 0 && func ) {
20+ for ( var i = 0 ; i < arr . length ; i ++ ) {
21+ if ( func ( arr [ i ] ) === true ) {
22+ break ;
23+ }
24+ }
25+ }
26+ }
27+
1628function updateLocalStorage ( name , value ) {
1729 if ( typeof ( Storage ) !== "undefined" ) {
1830 localStorage [ name ] = value ;
@@ -29,8 +41,24 @@ function getCurrentValue(name) {
2941}
3042
3143function switchTheme ( styleElem , mainStyleElem , newTheme ) {
32- styleElem . href = mainStyleElem . href . replace ( "rustdoc.css" , newTheme + ".css" ) ;
33- updateLocalStorage ( 'theme' , newTheme ) ;
44+ var newHref = mainStyleElem . href . replace ( "rustdoc.css" , newTheme + ".css" ) ;
45+ var found = false ;
46+
47+ if ( savedHref . length === 0 ) {
48+ onEach ( document . getElementsByTagName ( "link" ) , function ( el ) {
49+ savedHref . push ( el . href ) ;
50+ } ) ;
51+ }
52+ onEach ( savedHref , function ( el ) {
53+ if ( el === newHref ) {
54+ found = true ;
55+ return true ;
56+ }
57+ } ) ;
58+ if ( found === true ) {
59+ styleElem . href = newHref ;
60+ updateLocalStorage ( 'rustdoc-theme' , newTheme ) ;
61+ }
3462}
3563
36- switchTheme ( currentTheme , mainTheme , getCurrentValue ( 'theme' ) || 'main' ) ;
64+ switchTheme ( currentTheme , mainTheme , getCurrentValue ( 'rustdoc- theme' ) || 'main' ) ;
0 commit comments