2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'stylesheet/compiler'
|
|
|
|
|
|
|
|
describe Stylesheet::Compiler do
|
2017-07-26 09:49:39 +08:00
|
|
|
describe 'compilation' do
|
|
|
|
Dir["#{Rails.root.join("app/assets/stylesheets")}/*.scss"].each do |path|
|
|
|
|
path = File.basename(path, '.scss')
|
|
|
|
|
|
|
|
it "can compile '#{path}' css" do
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset(path)
|
|
|
|
expect(css.length).to be > 1000
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
context "with a theme" do
|
|
|
|
let!(:theme) { Fabricate(:theme) }
|
|
|
|
let!(:upload) { Fabricate(:upload) }
|
|
|
|
let!(:upload_theme_field) { ThemeField.create!(theme: theme, target_id: 0, name: "primary", upload: upload, value: "", type_id: ThemeField.types[:theme_upload_var]) }
|
|
|
|
let!(:stylesheet_theme_field) { ThemeField.create!(theme: theme, target_id: 0, name: "scss", value: "body { background: $primary }", type_id: ThemeField.types[:scss]) }
|
|
|
|
before { stylesheet_theme_field.save! }
|
|
|
|
|
|
|
|
it "theme stylesheet should be able to access theme asset variables" do
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset("desktop_theme", theme_id: theme.id)
|
|
|
|
expect(css).to include(upload.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a plugin" do
|
|
|
|
before do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
|
|
|
plugin.register_css "body { background: $primary }"
|
|
|
|
Discourse.plugins << plugin
|
|
|
|
plugin.activate!
|
|
|
|
Stylesheet::Importer.register_imports!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Discourse.plugins.pop
|
|
|
|
Stylesheet::Importer.register_imports!
|
2019-09-20 20:32:43 +08:00
|
|
|
DiscoursePluginRegistry.reset!
|
2019-09-17 00:06:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include theme variables in plugins" do
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset("my_plugin", theme_id: theme.id)
|
|
|
|
expect(css).not_to include(upload.url)
|
|
|
|
expect(css).to include("background:")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
it "supports asset-url" do
|
2017-08-31 12:06:56 +08:00
|
|
|
css, _map = Stylesheet::Compiler.compile(".body{background-image: asset-url('/images/favicons/github.png');}", "test.scss")
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(css).to include("url('/images/favicons/github.png')")
|
2017-04-12 22:52:52 +08:00
|
|
|
expect(css).not_to include('asset-url')
|
|
|
|
end
|
2017-05-02 03:31:51 +08:00
|
|
|
|
|
|
|
it "supports image-url" do
|
2017-08-31 12:06:56 +08:00
|
|
|
css, _map = Stylesheet::Compiler.compile(".body{background-image: image-url('/favicons/github.png');}", "test.scss")
|
2017-05-02 03:31:51 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(css).to include("url('/favicons/github.png')")
|
2017-05-02 03:31:51 +08:00
|
|
|
expect(css).not_to include('image-url')
|
|
|
|
end
|
2020-08-04 10:57:10 +08:00
|
|
|
|
|
|
|
context "with a color scheme" do
|
|
|
|
it "returns the default color definitions when no color scheme is specified" do
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset("color_definitions")
|
|
|
|
expect(css).to include("--header_background:")
|
|
|
|
expect(css).to include("--primary:")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns color definitions for a custom color scheme" do
|
|
|
|
cs = Fabricate(:color_scheme, name: 'Stylish', color_scheme_colors: [
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_primary', hex: '88af8e'),
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_background', hex: 'f8745c')
|
|
|
|
])
|
|
|
|
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset("color_definitions", color_scheme_id: cs.id)
|
|
|
|
|
|
|
|
expect(css).to include("--header_background: #f8745c")
|
|
|
|
expect(css).to include("--header_primary: #88af8e")
|
|
|
|
expect(css).to include("--header_background-rgb: 248,116,92")
|
|
|
|
end
|
2020-08-06 21:46:17 +08:00
|
|
|
|
|
|
|
context "with a plugin" do
|
|
|
|
before do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/color_definition/plugin.rb"
|
|
|
|
Discourse.plugins << plugin
|
|
|
|
plugin.activate!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Discourse.plugins.pop
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes color definitions from plugins" do
|
|
|
|
css, _map = Stylesheet::Compiler.compile_asset("color_definitions")
|
|
|
|
|
|
|
|
expect(css).to include("--plugin-color")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-04 10:57:10 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|