1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 16:47:42 +00:00

LibWeb: Spec-comment parse_a_stylesheet()

Also introduce a `location` parameter when parsing a CSSStyleSheet. This
is not provided by anyone yet.
This commit is contained in:
Sam Atkins 2022-03-29 16:51:31 +01:00 committed by Andreas Kling
parent 05bd0ca3ee
commit 87b125dcb9
4 changed files with 29 additions and 14 deletions

View file

@ -12,9 +12,11 @@
namespace Web::CSS {
CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules)
CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> 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

View file

@ -24,9 +24,9 @@ class CSSStyleSheet final
public:
using WrapperType = Bindings::CSSStyleSheetWrapper;
static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules)
static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> 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>, StyleSheetList*);
private:
explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);
explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>, Optional<AK::URL> location);
NonnullRefPtr<CSSRuleList> m_rules;

View file

@ -160,14 +160,26 @@ Parser::Parser(ParsingContext const& context, StringView input, String const& en
{
}
NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet()
NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet(Optional<AK::URL> 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<typename T>
NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens)
NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens, Optional<AK::URL> 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 stylesheets value to the result.
auto parser_rules = consume_a_list_of_rules(tokens, true);
NonnullRefPtrVector<CSSRule> rules;
@ -177,7 +189,8 @@ NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens)
rules.append(*rule);
}
return CSSStyleSheet::create(rules);
// 5. Return the stylesheet.
return CSSStyleSheet::create(move(rules), move(location));
}
Optional<SelectorList> Parser::parse_as_selector(SelectorParsingMode parsing_mode)
@ -5224,12 +5237,12 @@ TimePercentage Parser::Dimension::time_percentage() const
namespace Web {
RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const& context, StringView css)
RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const& context, StringView css, Optional<AK::URL> 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<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element)

View file

@ -90,7 +90,7 @@ public:
~Parser() = default;
// The normal parser entry point, for parsing stylesheets.
NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet(Optional<AK::URL> location);
// For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
NonnullRefPtrVector<CSSRule> 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<typename T>
NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&);
NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
template<typename T>
NonnullRefPtrVector<CSSRule> parse_a_list_of_rules(TokenStream<T>&);
template<typename T>
@ -361,7 +361,7 @@ private:
namespace Web {
RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const&, StringView);
RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const&, StringView, Optional<AK::URL> location = {});
RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&);
RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);