77
88const darkThemes = [ "dark" , "ayu" ] ;
99window . currentTheme = document . getElementById ( "themeStyle" ) ;
10- window . mainTheme = document . getElementById ( "mainThemeStyle" ) ;
1110
1211// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
1312// If you update this line, then you also need to update the media query with the same
@@ -44,8 +43,6 @@ function getSettingValue(settingName) {
4443
4544const localStoredTheme = getSettingValue ( "theme" ) ;
4645
47- const savedHref = [ ] ;
48-
4946// eslint-disable-next-line no-unused-vars
5047function hasClass ( elem , className ) {
5148 return elem && elem . classList && elem . classList . contains ( className ) ;
@@ -102,6 +99,7 @@ function onEach(arr, func, reversed) {
10299 * @param {function(?) } func - The callback
103100 * @param {boolean } [reversed] - Whether to iterate in reverse
104101 */
102+ // eslint-disable-next-line no-unused-vars
105103function onEachLazy ( lazyArray , func , reversed ) {
106104 return onEach (
107105 Array . prototype . slice . call ( lazyArray ) ,
@@ -125,30 +123,37 @@ function getCurrentValue(name) {
125123 }
126124}
127125
128- function switchTheme ( styleElem , mainStyleElem , newThemeName , saveTheme ) {
126+ // Get a value from the rustdoc-vars div, which is used to convey data from
127+ // Rust to the JS. If there is no such element, return null.
128+ const getVar = ( function getVar ( name ) {
129+ const el = document . getElementById ( "rustdoc-vars" ) ;
130+ if ( el ) {
131+ return el . attributes [ "data-" + name ] . value ;
132+ } else {
133+ return null ;
134+ }
135+ } ) ;
136+
137+ function switchTheme ( newThemeName , saveTheme ) {
129138 // If this new value comes from a system setting or from the previously
130139 // saved theme, no need to save it.
131140 if ( saveTheme ) {
132141 updateLocalStorage ( "theme" , newThemeName ) ;
133142 }
134143
135- if ( savedHref . length === 0 ) {
136- onEachLazy ( document . getElementsByTagName ( "link" ) , el => {
137- savedHref . push ( el . href ) ;
138- } ) ;
144+ let newHref ;
145+
146+ if ( newThemeName === "light" || newThemeName === "dark" || newThemeName === "ayu" ) {
147+ newHref = getVar ( "static-root-path" ) + getVar ( "theme-" + newThemeName + "-css" ) ;
148+ } else {
149+ newHref = getVar ( "root-path" ) + newThemeName + getVar ( "resource-suffix" ) + ".css" ;
139150 }
140- const newHref = savedHref . find ( url => {
141- const m = url . match ( / s t a t i c \. f i l e s \/ ( .* ) - [ a - f 0 - 9 ] { 16 } \. c s s $ / ) ;
142- if ( m && m [ 1 ] === newThemeName ) {
143- return true ;
144- }
145- const m2 = url . match ( / \/ ( [ ^ / ] * ) \. c s s $ / ) ;
146- if ( m2 && m2 [ 1 ] . startsWith ( newThemeName ) ) {
147- return true ;
148- }
149- } ) ;
150- if ( newHref && newHref !== styleElem . href ) {
151- styleElem . href = newHref ;
151+
152+ if ( ! window . currentTheme ) {
153+ document . write ( `<link rel="stylesheet" id="themeStyle" href="${ newHref } ">` ) ;
154+ window . currentTheme = document . getElementById ( "themeStyle" ) ;
155+ } else if ( newHref !== window . currentTheme . href ) {
156+ window . currentTheme . href = newHref ;
152157 }
153158}
154159
@@ -164,7 +169,7 @@ const updateTheme = (function() {
164169 */
165170 function updateTheme ( ) {
166171 const use = ( theme , saveTheme ) => {
167- switchTheme ( window . currentTheme , window . mainTheme , theme , saveTheme ) ;
172+ switchTheme ( theme , saveTheme ) ;
168173 } ;
169174
170175 // maybe the user has disabled the setting in the meantime!
0 commit comments