mastodon/app/helpers/theme_helper.rb
Shlee bdad4f78f3
Some checks failed
Check i18n / check-i18n (push) Has been cancelled
Chromatic / Check for relevant changes (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (ruby) (push) Has been cancelled
Check formatting / lint (push) Has been cancelled
Ruby Linting / lint (push) Has been cancelled
Historical data migration test / test (14-alpine) (push) Has been cancelled
Historical data migration test / test (15-alpine) (push) Has been cancelled
Historical data migration test / test (16-alpine) (push) Has been cancelled
Historical data migration test / test (17-alpine) (push) Has been cancelled
Chromatic / Run Chromatic (push) Has been cancelled
Ruby Testing / build (production) (push) Has been cancelled
Ruby Testing / build (test) (push) Has been cancelled
Ruby Testing / test (.ruby-version) (push) Has been cancelled
Ruby Testing / test (3.3) (push) Has been cancelled
Ruby Testing / test (3.4) (push) Has been cancelled
Ruby Testing / End to End testing (.ruby-version) (push) Has been cancelled
Ruby Testing / End to End testing (3.3) (push) Has been cancelled
Ruby Testing / End to End testing (3.4) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Has been cancelled
Bundler Audit / security (push) Has been cancelled
Fallback to default theme when admin-selected theme does not exist (#38703)
2026-04-17 16:57:38 +00:00

89 lines
2.5 KiB
Ruby

# frozen_string_literal: true
module ThemeHelper
def javascript_inline_tag(path)
entry = InlineScriptManager.instance.file(path)
# Only add hash if we don't allow arbitrary includes already, otherwise it's going
# to break the React Tools browser extension or other inline scripts
unless Rails.env.development? && request.content_security_policy.dup.script_src.include?("'unsafe-inline'")
request.content_security_policy = request.content_security_policy.clone.tap do |policy|
values = policy.script_src
values << "'sha256-#{entry[:digest]}'"
policy.script_src(*values)
end
end
content_tag(:script, entry[:contents], type: 'text/javascript')
end
def theme_style_tags(theme)
vite_stylesheet_tag "themes/#{theme}", type: :virtual, media: 'all', crossorigin: 'anonymous'
end
def theme_color_tags(color_scheme)
case color_scheme
when 'auto'
''.html_safe.tap do |tags|
tags << tag.meta(name: 'theme-color', content: Themes::THEME_COLORS[:dark], media: '(prefers-color-scheme: dark)')
tags << tag.meta(name: 'theme-color', content: Themes::THEME_COLORS[:light], media: '(prefers-color-scheme: light)')
end
when 'light'
tag.meta name: 'theme-color', content: Themes::THEME_COLORS[:light]
when 'dark'
tag.meta name: 'theme-color', content: Themes::THEME_COLORS[:dark]
end
end
def custom_stylesheet
return if active_custom_stylesheet.blank?
stylesheet_link_tag(
custom_css_path(active_custom_stylesheet),
host: root_url,
media: :all,
skip_pipeline: true
)
end
def current_theme
available_themes = Themes.instance.names
user_theme = current_user&.setting_theme
return user_theme if user_theme && available_themes.include?(user_theme)
site_theme = Setting.theme
return site_theme if available_themes.include?(site_theme)
'default' # Fallback
end
def color_scheme
current_user&.setting_color_scheme || 'auto'
end
def contrast
current_user&.setting_contrast || 'auto'
end
def page_color_scheme
content_for(:force_color_scheme).presence || color_scheme
end
private
def active_custom_stylesheet
return if cached_custom_css_digest.blank?
[:custom, cached_custom_css_digest.to_s.first(8)]
.compact_blank
.join('-')
end
def cached_custom_css_digest
Rails.cache.fetch(:setting_digest_custom_css) do
Setting.custom_css&.then { |content| Digest::SHA256.hexdigest(content) }
end
end
end