FIX: color scheme selection with non-default theme

FIX: color scheme selection with non-default theme

This fixes an issue where a non-default theme set to use the base color scheme (i.e. the theme had an empty color_scheme_id) was loading the default theme’s color scheme instead.

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 84d7909..e8c260f 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -452,7 +452,7 @@ module ApplicationHelper
 
   def discourse_color_scheme_stylesheets
     result = +""
-    result << Stylesheet::Manager.color_scheme_stylesheet_link_tag(scheme_id)
+    result << Stylesheet::Manager.color_scheme_stylesheet_link_tag(scheme_id, 'all', theme_ids)
 
     user_dark_scheme_id = current_user&.user_option&.dark_scheme_id
     dark_scheme_id =  user_dark_scheme_id || SiteSetting.default_dark_mode_color_scheme_id
diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 5416e38..0101847 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -93,13 +93,15 @@ class Stylesheet::Manager
     end
   end
 
-  def self.color_scheme_stylesheet_details(color_scheme_id = nil, media)
+  def self.color_scheme_stylesheet_details(color_scheme_id = nil, media, theme_id)
     color_scheme = begin
       ColorScheme.find(color_scheme_id)
     rescue
       # don't load fallback when requesting dark color scheme
       return false if media != "all"
-      Theme.find_by_id(SiteSetting.default_theme_id)&.color_scheme || ColorScheme.base
+
+      theme_id = theme_id || SiteSetting.default_theme_id
+      Theme.find_by_id(theme_id)&.color_scheme || ColorScheme.base
     end
 
     return false if !color_scheme
@@ -121,8 +123,9 @@ class Stylesheet::Manager
     stylesheet
   end
 
-  def self.color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = 'all')
-    stylesheet = color_scheme_stylesheet_details(color_scheme_id, media)
+  def self.color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = 'all', theme_ids = nil)
+    theme_id = theme_ids&.first
+    stylesheet = color_scheme_stylesheet_details(color_scheme_id, media, theme_id)
     return '' if !stylesheet
 
     href = stylesheet[:new_href]
diff --git a/spec/components/stylesheet/manager_spec.rb b/spec/components/stylesheet/manager_spec.rb
index 0aa0a6c..a544d9e 100644
--- a/spec/components/stylesheet/manager_spec.rb
+++ b/spec/components/stylesheet/manager_spec.rb
@@ -208,7 +208,18 @@ describe Stylesheet::Manager do
       expect(link).to include("/stylesheets/color_definitions_funky_#{cs.id}_")
     end
 
-    it "uses the correct scheme when colors are passed" do
+    it "uses the correct color scheme when a non-default theme is selected and it uses the base 'Light' scheme" do
+      cs = Fabricate(:color_scheme, name: 'Not This')
+      default_theme = Fabricate(:theme, color_scheme_id: cs.id)
+      SiteSetting.default_theme_id = default_theme.id
+
+      user_theme = Fabricate(:theme, color_scheme_id: nil)
+
+      link = Stylesheet::Manager.color_scheme_stylesheet_link_tag(nil, "all", [user_theme.id])
+      expect(link).to include("/stylesheets/color_definitions_base_")
+    end
+
+    it "uses the correct scheme when a valid scheme id is used" do
       link = Stylesheet::Manager.color_scheme_stylesheet_link_tag(ColorScheme.first.id)
       slug = Slug.for(ColorScheme.first.name) + "_" + ColorScheme.first.id.to_s
       expect(link).to include("/stylesheets/color_definitions_#{slug}_")

GitHub sha: 6dd9f2ec

1 Like