Skip to content

A platform for community discussion. Free, open, simple.

DEV: animated splash SVGs should be limited to CSS (#37606)

Kris5775d5d4def4
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index cda192e83d0..08e3dac9ea9 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -507,6 +507,11 @@ module ApplicationHelper
@splash_screen_image_animated
end
+ def splash_screen_inline_svg
+ build_splash_screen_image unless defined?(@splash_screen_image_svg)
+ @splash_screen_image_svg&.html_safe
+ end
+
def splash_screen_image_data_uri(dark: false)
build_splash_screen_image unless defined?(@splash_screen_image_svg)
return nil if @splash_screen_image_svg.blank?
@@ -552,24 +557,20 @@ module ApplicationHelper
svg = doc.at_css("svg")
return unless svg
- @splash_screen_image_animated =
- svg.at_xpath(
- ".//*[local-name()='animate' or local-name()='animateTransform' or local-name()='animateMotion' or local-name()='set']",
- ).present? || svg.at_css("style")&.text&.include?("@keyframes")
+ @splash_screen_image_animated = svg.at_css("style")&.text&.include?("@keyframes")
+
+ # Strip SMIL animation elements, these run on the main thread and stutter.
+ # Only CSS transform/opacity animations are recommended.
+ svg.xpath(
+ ".//*[local-name()='animate' or local-name()='animateTransform' or local-name()='animateMotion' or local-name()='set']",
+ ).each(&:remove)
has_scripts = svg.xpath("//script").present?
has_event_handlers = svg.xpath("//@*[starts-with(name(), 'on')]").present?
- # Avoid animations targeting href attributes
- has_href_animations =
- svg
- .xpath(".//*[local-name()='animate' or local-name()='set']/@attributeName")
- .any? { |attr| attr.value.downcase.include?("href") }
-
- return if has_scripts || has_event_handlers || has_href_animations
- @splash_screen_image_svg = content.dup
+ return if has_scripts || has_event_handlers
- @splash_screen_image_svg.sub!(/^<\?xml[^>]*\?>\s*/, "")
+ @splash_screen_image_svg = svg.to_xml
end
public
diff --git a/app/views/common/_discourse_splash.html.erb b/app/views/common/_discourse_splash.html.erb
index 7b5ed2231ec..199e934093e 100644
--- a/app/views/common/_discourse_splash.html.erb
+++ b/app/views/common/_discourse_splash.html.erb
@@ -17,7 +17,13 @@
#d-splash {
--dot-color: #<%= light_color_hex_for_name("tertiary") %>;
<%- if custom_splash_screen_enabled? %>
- --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- if splash_screen_image_animated? %>
+ --primary: #<%= light_color_hex_for_name("primary") %>;
+ --secondary: #<%= light_color_hex_for_name("secondary") %>;
+ --tertiary: #<%= light_color_hex_for_name("tertiary") %>;
+ <%- else %>
+ --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- end %>
<%- end %>
}
<%- elsif forced_light_mode? %>
@@ -28,7 +34,13 @@
#d-splash {
--dot-color: #<%= light_color_hex_for_name("tertiary") %>;
<%- if custom_splash_screen_enabled? %>
- --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- if splash_screen_image_animated? %>
+ --primary: #<%= light_color_hex_for_name("primary") %>;
+ --secondary: #<%= light_color_hex_for_name("secondary") %>;
+ --tertiary: #<%= light_color_hex_for_name("tertiary") %>;
+ <%- else %>
+ --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- end %>
<%- end %>
}
<%- elsif forced_dark_mode? %>
@@ -39,7 +51,13 @@
#d-splash {
--dot-color: #<%= dark_color_hex_for_name("tertiary") %>;
<%- if custom_splash_screen_enabled? %>
- --splash-bg: url("<%= splash_screen_image_data_uri(dark: true) %>");
+ <%- if splash_screen_image_animated? %>
+ --primary: #<%= dark_color_hex_for_name("primary") %>;
+ --secondary: #<%= dark_color_hex_for_name("secondary") %>;
+ --tertiary: #<%= dark_color_hex_for_name("tertiary") %>;
+ <%- else %>
+ --splash-bg: url("<%= splash_screen_image_data_uri(dark: true) %>");
+ <%- end %>
<%- end %>
}
<%- else %>
@@ -54,7 +72,13 @@
#d-splash {
--dot-color: #<%= light_color_hex_for_name("tertiary") %>;
<%- if custom_splash_screen_enabled? %>
- --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- if splash_screen_image_animated? %>
+ --primary: #<%= light_color_hex_for_name("primary") %>;
+ --secondary: #<%= light_color_hex_for_name("secondary") %>;
+ --tertiary: #<%= light_color_hex_for_name("tertiary") %>;
+ <%- else %>
+ --splash-bg: url("<%= splash_screen_image_data_uri %>");
+ <%- end %>
<%- end %>
}
}
@@ -68,7 +92,13 @@
#d-splash {
--dot-color: #<%= dark_color_hex_for_name("tertiary") %>;
<%- if custom_splash_screen_enabled? %>
- --splash-bg: url("<%= splash_screen_image_data_uri(dark: true) %>");
+ <%- if splash_screen_image_animated? %>
+ --primary: #<%= dark_color_hex_for_name("primary") %>;
+ --secondary: #<%= dark_color_hex_for_name("secondary") %>;
+ --tertiary: #<%= dark_color_hex_for_name("tertiary") %>;
+ <%- else %>
+ --splash-bg: url("<%= splash_screen_image_data_uri(dark: true) %>");
+ <%- end %>
<%- end %>
}
}
@@ -93,6 +123,9 @@
@media (width < 700px) {
width: 320px;
}
+ @media (width < 325px) {
+ width: 300px;
+ }
position: relative;
display: flex;
flex-direction: column;
@@ -177,16 +210,27 @@
#d-splash .splash-logo-container {
width: min(250px, 60vw);
height: min(150px, 25vh);
+ <%- if splash_screen_image_animated? %>
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ <%- else %>
background-size: contain;
background-repeat: no-repeat;
background-position: center;
background-image: var(--splash-bg);
- <%- unless splash_screen_image_animated? %>
opacity: 0.4;
animation: d-splash-logo-pulse 3s ease-in-out infinite;
<%- end %>
}
+ <%- if splash_screen_image_animated? %>
+ #d-splash .splash-logo-container svg {
+ max-width: 100%;
+ max-height: 100%;
+ }
+ <%- end %>
+
@keyframes d-splash-logo-pulse {
0%, 100% {
opacity: 0.4;
@@ -200,8 +244,10 @@
<div class="preloader-image" elementtiming="discourse-splash-visible">
<%- if custom_splash_screen_enabled? %>
- <div class="splash-logo-container"></div>
- <%- unless splash_screen_image_animated? %>
+ <%- if splash_screen_image_animated? %>
+ <div class="splash-logo-container"><%= splash_screen_inline_svg %></div>
+ <%- else %>
+ <div class="splash-logo-container"></div>
<div class="dots-container">
<div class="dots" style="--n:-2;"></div>
<div class="dots" style="--n:-1;"></div>
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 09ff8efe41a..f34305f991f 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2720,7 +2720,7 @@ en:
welcome_banner_text_color: "When the 'Welcome banner image' setting has been configured, use this setting to change the banner text color to ensure it remains readable. When left blank or when no image has been uploaded, the active theme controls the color."
splash_screen: "Displays a temporary loading screen while site assets load"
- splash_screen_image: "An SVG image to display on the splash screen while the site loads. Supports animated SVGs. You can use var(--primary), var(--secondary), and var(--tertiary) in your SVG to reference theme colors."
+ splash_screen_image: "An SVG image to display on the splash screen while the site loads. Animated SVGs should only use CSS transform or opacity animations. Use unique names for CSS classes and keyframes. You can use var(--primary), var(--secondary), and var(--tertiary) to reference theme colors."
navigation_menu: "Specify sidebar or header dropdown as the main navigation menu for your site. Sidebar is recommended."
default_navigation_menu_categories: "Selected categories will be displayed under Navigation Menu's Categories section by default."
default_navigation_menu_tags: "Selected tags will be displayed under Navigation Menu's Tags section by default."
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 3f4f1b97c16..5ff25a061ba 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -6,9 +6,6 @@ class UploadCreator
TYPES_TO_CROP = %w[avatar card_background custom_emoji profile_background].each(&:freeze)
ALLOWED_SVG_ELEMENTS = %w[
- animate
- animateMotion
- animateTransform
circle
clipPath
defs
@@ -24,7 +21,6 @@ class UploadCreator
polyline
radialGradient
rect
- set
stop
style
svg
@@ -517,14 +513,6 @@ class UploadCreator
use_el.remove_attribute("xlink:href")
end
- # Remove animation elements that target href/xlink:href attributes
- doc
- .css("animate, animateTransform, animateMotion, set")
- .each do |anim_el|
- attr_name = anim_el.attr("attributeName")&.downcase
- anim_el.remove if attr_name&.include?("href")
- end
-
File.write(@file.path, doc.to_s)
@file.rewind
end