From 5ff2a235f6e566e4d42e3fd14034fa8e1424e964 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 14 May 2020 16:37:45 -0400 Subject: [PATCH] DEV: Allow 3-digit HEX color code in single icon route Followup to aee8e62 --- app/controllers/svg_sprite_controller.rb | 12 +++++++++++- config/routes.rb | 2 +- spec/requests/svg_sprite_controller_spec.rb | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/controllers/svg_sprite_controller.rb b/app/controllers/svg_sprite_controller.rb index 1a7aad9f06c..81b970547e1 100644 --- a/app/controllers/svg_sprite_controller.rb +++ b/app/controllers/svg_sprite_controller.rb @@ -64,7 +64,7 @@ class SvgSpriteController < ApplicationController doc = Nokogiri.XML(icon) doc.at_xpath("symbol").name = "svg" doc.at_xpath("svg")['xmlns'] = "http://www.w3.org/2000/svg" - doc.at_xpath("svg")['fill'] = "##{params[:color]}" if params[:color] + doc.at_xpath("svg")['fill'] = adjust_hex(params[:color]) if params[:color] response.headers["Last-Modified"] = 1.years.ago.httpdate response.headers["Content-Length"] = doc.to_s.bytesize.to_s @@ -74,4 +74,14 @@ class SvgSpriteController < ApplicationController end end end + + private + + def adjust_hex(hex) + if hex.size == 3 + chars = hex.scan(/\w/) + hex = chars.zip(chars).flatten.join + end + "##{hex}" + end end diff --git a/config/routes.rb b/config/routes.rb index 353af09a49e..d3a1082d435 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -503,7 +503,7 @@ Discourse::Application.routes.draw do get "svg-sprite/:hostname/svg-:theme_ids-:version.js" => "svg_sprite#show", constraints: { hostname: /[\w\.-]+/, version: /\h{40}/, theme_ids: /([0-9]+(,[0-9]+)*)?/, format: :js } get "svg-sprite/search/:keyword" => "svg_sprite#search", format: false, constraints: { keyword: /[-a-z0-9\s\%]+/ } get "svg-sprite/picker-search" => "svg_sprite#icon_picker_search", defaults: { format: :json } - get "svg-sprite/:hostname/icon(/:color)/:name.svg" => "svg_sprite#svg_icon", constraints: { hostname: /[\w\.-]+/, name: /[-a-z0-9\s\%]+/, color: /(\h{6})/, format: :svg } + get "svg-sprite/:hostname/icon(/:color)/:name.svg" => "svg_sprite#svg_icon", constraints: { hostname: /[\w\.-]+/, name: /[-a-z0-9\s\%]+/, color: /(\h{3}{1,2})/, format: :svg } get "highlight-js/:hostname/:version.js" => "highlight_js#show", constraints: { hostname: /[\w\.-]+/, format: :js } diff --git a/spec/requests/svg_sprite_controller_spec.rb b/spec/requests/svg_sprite_controller_spec.rb index 540235552a2..60584b8ea8b 100644 --- a/spec/requests/svg_sprite_controller_spec.rb +++ b/spec/requests/svg_sprite_controller_spec.rb @@ -115,6 +115,15 @@ describe SvgSpriteController do expect(response.headers["Cache-Control"]).to eq("max-age=86400, public, immutable") end + it "returns SVG given an icon name and a 3-character HEX color" do + get "/svg-sprite/#{Discourse.current_hostname}/icon/C00/fab-github.svg" + expect(response.status).to eq(200) + + expect(response.body).to include('fab-github') + expect(response.body).to include('fill="#CC0000"') + expect(response.headers["Cache-Control"]).to eq("max-age=86400, public, immutable") + end + it "ignores non-HEX colors" do get "/svg-sprite/#{Discourse.current_hostname}/icon/orange/fab-github.svg" expect(response.status).to eq(404)