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

Followup to aee8e62
This commit is contained in:
Penar Musaraj
2020-05-14 16:37:45 -04:00
parent 1b7f23a1bc
commit 5ff2a235f6
3 changed files with 21 additions and 2 deletions

View File

@ -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