forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate methods for theme style and meta color tags (mastodon#29802)
- Loading branch information
1 parent
52ab8a5
commit b61ae28
Showing
5 changed files
with
123 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module ThemeHelper | ||
def theme_style_tags(theme) | ||
if theme == 'system' | ||
concat stylesheet_pack_tag('mastodon-light', media: 'not all and (prefers-color-scheme: dark)', crossorigin: 'anonymous') | ||
concat stylesheet_pack_tag('default', media: '(prefers-color-scheme: dark)', crossorigin: 'anonymous') | ||
else | ||
stylesheet_pack_tag theme, media: 'all', crossorigin: 'anonymous' | ||
end | ||
end | ||
|
||
def theme_color_tags(theme) | ||
if theme == 'system' | ||
concat tag.meta(name: 'theme-color', content: Themes::THEME_COLORS[:dark], media: '(prefers-color-scheme: dark)') | ||
concat tag.meta(name: 'theme-color', content: Themes::THEME_COLORS[:light], media: '(prefers-color-scheme: light)') | ||
else | ||
tag.meta name: 'theme-color', content: theme_color_for(theme) | ||
end | ||
end | ||
|
||
private | ||
|
||
def theme_color_for(theme) | ||
theme == 'mastodon-light' ? Themes::THEME_COLORS[:light] : Themes::THEME_COLORS[:dark] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe ThemeHelper do | ||
describe 'theme_style_tags' do | ||
let(:result) { helper.theme_style_tags(theme) } | ||
|
||
context 'when using system theme' do | ||
let(:theme) { 'system' } | ||
|
||
it 'returns the mastodon-light and default stylesheets with correct color schemes' do | ||
expect(html_links.first.attributes.symbolize_keys) | ||
.to include( | ||
href: have_attributes(value: match(/mastodon-light/)), | ||
media: have_attributes(value: 'not all and (prefers-color-scheme: dark)') | ||
) | ||
expect(html_links.last.attributes.symbolize_keys) | ||
.to include( | ||
href: have_attributes(value: match(/default/)), | ||
media: have_attributes(value: '(prefers-color-scheme: dark)') | ||
) | ||
end | ||
end | ||
|
||
context 'when using other theme' do | ||
let(:theme) { 'contrast' } | ||
|
||
it 'returns the theme stylesheet without color scheme information' do | ||
expect(html_links.first.attributes.symbolize_keys) | ||
.to include( | ||
href: have_attributes(value: match(/contrast/)), | ||
media: have_attributes(value: 'all') | ||
) | ||
end | ||
end | ||
end | ||
|
||
describe 'theme_color_tags' do | ||
let(:result) { helper.theme_color_tags(theme) } | ||
|
||
context 'when using system theme' do | ||
let(:theme) { 'system' } | ||
|
||
it 'returns the mastodon-light and default stylesheets with correct color schemes' do | ||
expect(html_theme_colors.first.attributes.symbolize_keys) | ||
.to include( | ||
content: have_attributes(value: Themes::THEME_COLORS[:dark]), | ||
media: have_attributes(value: '(prefers-color-scheme: dark)') | ||
) | ||
expect(html_theme_colors.last.attributes.symbolize_keys) | ||
.to include( | ||
content: have_attributes(value: Themes::THEME_COLORS[:light]), | ||
media: have_attributes(value: '(prefers-color-scheme: light)') | ||
) | ||
end | ||
end | ||
|
||
context 'when using mastodon-light theme' do | ||
let(:theme) { 'mastodon-light' } | ||
|
||
it 'returns the theme stylesheet without color scheme information' do | ||
expect(html_theme_colors.first.attributes.symbolize_keys) | ||
.to include( | ||
content: have_attributes(value: Themes::THEME_COLORS[:light]) | ||
) | ||
end | ||
end | ||
|
||
context 'when using other theme' do | ||
let(:theme) { 'contrast' } | ||
|
||
it 'returns the theme stylesheet without color scheme information' do | ||
expect(html_theme_colors.first.attributes.symbolize_keys) | ||
.to include( | ||
content: have_attributes(value: Themes::THEME_COLORS[:dark]) | ||
) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def html_links | ||
Nokogiri::HTML5.fragment(result).css('link') | ||
end | ||
|
||
def html_theme_colors | ||
Nokogiri::HTML5.fragment(result).css('meta[name=theme-color]') | ||
end | ||
end |