DEV: Allow 3-digit HEX color code in single icon route

DEV: Allow 3-digit HEX color code in single icon route

Followup to aee8e62

diff --git a/app/controllers/svg_sprite_controller.rb b/app/controllers/svg_sprite_controller.rb
index 1a7aad9..81b9705 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 353af09..d3a1082 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 5402355..60584b8 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)

GitHub sha: 5ff2a235