diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 4d3ca3b577..594c5240cb 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -12,9 +12,11 @@ namespace Web::CSS { -CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector rules) +CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector rules, Optional location) : m_rules(CSSRuleList::create(move(rules))) { + if (location.has_value()) + set_location(location->to_string()); } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index a650009bab..c9ae813d7e 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -24,9 +24,9 @@ class CSSStyleSheet final public: using WrapperType = Bindings::CSSStyleSheetWrapper; - static NonnullRefPtr create(NonnullRefPtrVector rules) + static NonnullRefPtr create(NonnullRefPtrVector rules, Optional location) { - return adopt_ref(*new CSSStyleSheet(move(rules))); + return adopt_ref(*new CSSStyleSheet(move(rules), move(location))); } virtual ~CSSStyleSheet() override = default; @@ -53,7 +53,7 @@ public: void set_style_sheet_list(Badge, StyleSheetList*); private: - explicit CSSStyleSheet(NonnullRefPtrVector); + explicit CSSStyleSheet(NonnullRefPtrVector, Optional location); NonnullRefPtr m_rules; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index f2fe8a590c..bb9fe475be 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -160,14 +160,26 @@ Parser::Parser(ParsingContext const& context, StringView input, String const& en { } -NonnullRefPtr Parser::parse_as_stylesheet() +NonnullRefPtr Parser::parse_as_stylesheet(Optional location) { - return parse_a_stylesheet(m_token_stream); + return parse_a_stylesheet(m_token_stream, location); } +// 5.3.3. Parse a stylesheet +// https://www.w3.org/TR/css-syntax-3/#parse-stylesheet template -NonnullRefPtr Parser::parse_a_stylesheet(TokenStream& tokens) +NonnullRefPtr Parser::parse_a_stylesheet(TokenStream& tokens, Optional location) { + // To parse a stylesheet from an input given an optional url location: + + // 1. If input is a byte stream for stylesheet, decode bytes from input, and set input to the result. + // 2. Normalize input, and set input to the result. + // NOTE: These are done automatically when creating the Parser. + + // 3. Create a new stylesheet, with its location set to location (or null, if location was not passed). + // NOTE: We create the stylesheet at the end. + + // 4. Consume a list of rules from input, with the top-level flag set, and set the stylesheet’s value to the result. auto parser_rules = consume_a_list_of_rules(tokens, true); NonnullRefPtrVector rules; @@ -177,7 +189,8 @@ NonnullRefPtr Parser::parse_a_stylesheet(TokenStream& tokens) rules.append(*rule); } - return CSSStyleSheet::create(rules); + // 5. Return the stylesheet. + return CSSStyleSheet::create(move(rules), move(location)); } Optional Parser::parse_as_selector(SelectorParsingMode parsing_mode) @@ -5224,12 +5237,12 @@ TimePercentage Parser::Dimension::time_percentage() const namespace Web { -RefPtr parse_css_stylesheet(CSS::ParsingContext const& context, StringView css) +RefPtr parse_css_stylesheet(CSS::ParsingContext const& context, StringView css, Optional location) { if (css.is_empty()) - return CSS::CSSStyleSheet::create({}); + return CSS::CSSStyleSheet::create({}, location); CSS::Parser parser(context, css); - return parser.parse_as_stylesheet(); + return parser.parse_as_stylesheet(location); } RefPtr parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index ed1bf1d843..62411d612c 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -90,7 +90,7 @@ public: ~Parser() = default; // The normal parser entry point, for parsing stylesheets. - NonnullRefPtr parse_as_stylesheet(); + NonnullRefPtr parse_as_stylesheet(Optional location); // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of and . NonnullRefPtrVector parse_as_list_of_rules(); // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule. @@ -135,7 +135,7 @@ private: }; template - NonnullRefPtr parse_a_stylesheet(TokenStream&); + NonnullRefPtr parse_a_stylesheet(TokenStream&, Optional location); template NonnullRefPtrVector parse_a_list_of_rules(TokenStream&); template @@ -361,7 +361,7 @@ private: namespace Web { -RefPtr parse_css_stylesheet(CSS::ParsingContext const&, StringView); +RefPtr parse_css_stylesheet(CSS::ParsingContext const&, StringView, Optional location = {}); RefPtr parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&); RefPtr parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid); Optional parse_selector(CSS::ParsingContext const&, StringView);