Preserve absolute stylesheet URLs

This commit is contained in:
Sami Samhuri 2026-02-07 18:39:54 -08:00
parent c69c12db99
commit ae5057f223
No known key found for this signature in database
2 changed files with 26 additions and 1 deletions

View file

@ -88,7 +88,7 @@ module Pressa
link(rel: "dns-prefetch", href: "https://gist.github.com")
all_styles.each do |style|
link(rel: "stylesheet", type: "text/css", href: absolute_asset(style.href))
link(rel: "stylesheet", type: "text/css", href: style_href(style.href))
end
end
@ -191,6 +191,12 @@ module Pressa
absolute_asset(src)
end
def style_href(href)
return href if href.start_with?("http://", "https://")
absolute_asset(href)
end
def absolute_asset(path)
normalized = path.start_with?("/") ? path : "/#{path}"
site.url_for(normalized)

View file

@ -44,4 +44,23 @@ RSpec.describe Pressa::Views::Layout do
expect(html).to include("<title>samhuri.net: &lt;img src=x onerror=alert(1)&gt;</title>")
end
it "preserves absolute stylesheet URLs" do
cdn_site = Pressa::Site.new(
author: "Sami Samhuri",
email: "sami@samhuri.net",
title: "samhuri.net",
description: "blog",
url: "https://samhuri.net",
styles: [Pressa::Stylesheet.new(href: "https://cdn.example.com/site.css")]
)
html = described_class.new(
site: cdn_site,
canonical_url: "https://samhuri.net/posts/",
content: test_content_view
).call
expect(html).to include(%(<link rel="stylesheet" type="text/css" href="https://cdn.example.com/site.css">))
end
end