Przeglądaj źródła

LibWeb: Fix resolving relative URLs in style sheets

Relative URLs in style sheets should be resolved relative to the
style sheet they're in instead of the document.
Simon Wanner 3 lat temu
rodzic
commit
1ed5e79478

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp

@@ -77,7 +77,7 @@ void CSSImportRule::resource_did_load()
         dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
     }
 
-    auto sheet = parse_css(CSS::ParsingContext(*m_document), resource()->encoded_data());
+    auto sheet = parse_css(CSS::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
     if (!sheet) {
         dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
         return;

+ 10 - 1
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -34,13 +34,21 @@ static void log_parse_error(const SourceLocation& location = SourceLocation::cur
 
 namespace Web::CSS {
 
+ParsingContext::ParsingContext(DOM::Document const& document, Optional<AK::URL> const url)
+    : m_document(&document)
+    , m_url(move(url))
+{
+}
+
 ParsingContext::ParsingContext(DOM::Document const& document)
     : m_document(&document)
+    , m_url(document.url())
 {
 }
 
 ParsingContext::ParsingContext(DOM::ParentNode& parent_node)
     : m_document(&parent_node.document())
+    , m_url(parent_node.document().url())
 {
 }
 
@@ -49,9 +57,10 @@ bool ParsingContext::in_quirks_mode() const
     return m_document ? m_document->in_quirks_mode() : false;
 }
 
+// https://www.w3.org/TR/css-values-4/#relative-urls
 AK::URL ParsingContext::complete_url(String const& addr) const
 {
-    return m_document ? m_document->url().complete_url(addr) : AK::URL::create_with_url_or_path(addr);
+    return m_url.has_value() ? m_url->complete_url(addr) : AK::URL::create_with_url_or_path(addr);
 }
 
 template<typename T>

+ 2 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -39,6 +39,7 @@ class ParsingContext {
 public:
     ParsingContext() = default;
     explicit ParsingContext(DOM::Document const&);
+    explicit ParsingContext(DOM::Document const&, Optional<AK::URL> const);
     explicit ParsingContext(DOM::ParentNode&);
 
     bool in_quirks_mode() const;
@@ -51,6 +52,7 @@ public:
 private:
     DOM::Document const* m_document { nullptr };
     PropertyID m_current_property_id { PropertyID::Invalid };
+    Optional<AK::URL> m_url;
 };
 
 template<typename T>

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp

@@ -90,7 +90,7 @@ void HTMLLinkElement::resource_did_load()
         dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, has encoded data. URL: {}", resource()->url());
     }
 
-    auto sheet = parse_css(CSS::ParsingContext(document()), resource()->encoded_data());
+    auto sheet = parse_css(CSS::ParsingContext(document(), resource()->url()), resource()->encoded_data());
     if (!sheet) {
         dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());
         return;