From 004ae453d17a5d1d215d3c719fb199bed8919cb5 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 2 Jul 2021 20:25:13 +0100 Subject: LibWeb: Add context to new CSS parser, and deprecate the old one The new one is the same as the old one, just in the new Parser's source files. This isn't the most elegant solution but it seemed like the best option. And it's all temporary, after all. --- .../LibWeb/CSS/Parser/DeprecatedCSSParser.cpp | 34 +++++++++++----------- .../LibWeb/CSS/Parser/DeprecatedCSSParser.h | 22 +++++++------- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 30 +++++++++++++++++-- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 18 +++++++++++- Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp | 2 +- Userland/Libraries/LibWeb/CSS/StyleResolver.cpp | 6 ++-- 6 files changed, 77 insertions(+), 35 deletions(-) (limited to 'Userland/Libraries/LibWeb/CSS') diff --git a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp index 18b6bfc8f5..a8e2143045 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp @@ -33,33 +33,33 @@ namespace Web { namespace CSS { -ParsingContext::ParsingContext() +DeprecatedParsingContext::DeprecatedParsingContext() { } -ParsingContext::ParsingContext(const DOM::Document& document) +DeprecatedParsingContext::DeprecatedParsingContext(const DOM::Document& document) : m_document(&document) { } -ParsingContext::ParsingContext(const DOM::ParentNode& parent_node) +DeprecatedParsingContext::DeprecatedParsingContext(const DOM::ParentNode& parent_node) : m_document(&parent_node.document()) { } -bool ParsingContext::in_quirks_mode() const +bool DeprecatedParsingContext::in_quirks_mode() const { return m_document ? m_document->in_quirks_mode() : false; } -URL ParsingContext::complete_url(const String& addr) const +URL DeprecatedParsingContext::complete_url(const String& addr) const { return m_document ? m_document->url().complete_url(addr) : URL::create_with_url_or_path(addr); } } -static Optional parse_css_color(const CSS::ParsingContext&, const StringView& view) +static Optional parse_css_color(const CSS::DeprecatedParsingContext&, const StringView& view) { if (view.equals_ignoring_case("transparent")) return Color::from_rgba(0x00000000); @@ -144,7 +144,7 @@ static Optional try_parse_float(const StringView& string) return is_negative ? -value : value; } -static CSS::Length parse_length(const CSS::ParsingContext& context, const StringView& view, bool& is_bad_length) +static CSS::Length parse_length(const CSS::DeprecatedParsingContext& context, const StringView& view, bool& is_bad_length) { CSS::Length::Type type = CSS::Length::Type::Undefined; Optional value; @@ -229,7 +229,7 @@ static StringView parse_custom_property_name(const StringView& value) return value.substring_view(4, substring_length); } -RefPtr parse_css_value(const CSS::ParsingContext& context, const StringView& string, CSS::PropertyID property_id) +RefPtr parse_css_value(const CSS::DeprecatedParsingContext& context, const StringView& string, CSS::PropertyID property_id) { bool is_bad_length = false; @@ -269,7 +269,7 @@ RefPtr parse_css_value(const CSS::ParsingContext& context, cons return CSS::StringStyleValue::create(string); } -RefPtr parse_line_width(const CSS::ParsingContext& context, const StringView& part) +RefPtr parse_line_width(const CSS::DeprecatedParsingContext& context, const StringView& part) { auto value = parse_css_value(context, part); if (value && value->is_length()) @@ -277,7 +277,7 @@ RefPtr parse_line_width(const CSS::ParsingContext& contex return nullptr; } -RefPtr parse_color(const CSS::ParsingContext& context, const StringView& part) +RefPtr parse_color(const CSS::DeprecatedParsingContext& context, const StringView& part) { auto value = parse_css_value(context, part); if (value && value->is_color()) @@ -285,7 +285,7 @@ RefPtr parse_color(const CSS::ParsingContext& context, con return nullptr; } -RefPtr parse_line_style(const CSS::ParsingContext& context, const StringView& part) +RefPtr parse_line_style(const CSS::DeprecatedParsingContext& context, const StringView& part) { auto parsed_value = parse_css_value(context, part); if (!parsed_value || parsed_value->type() != CSS::StyleValue::Type::Identifier) @@ -316,7 +316,7 @@ RefPtr parse_line_style(const CSS::ParsingContext& co class CSSParser { public: - CSSParser(const CSS::ParsingContext& context, const StringView& input) + CSSParser(const CSS::DeprecatedParsingContext& context, const StringView& input) : m_context(context) , css(input) { @@ -1027,7 +1027,7 @@ public: } private: - CSS::ParsingContext m_context; + CSS::DeprecatedParsingContext m_context; NonnullRefPtrVector rules; @@ -1045,13 +1045,13 @@ private: StringView css; }; -Optional parse_selector(const CSS::ParsingContext& context, const StringView& selector_text) +Optional parse_selector(const CSS::DeprecatedParsingContext& context, const StringView& selector_text) { CSSParser parser(context, selector_text); return parser.parse_individual_selector(); } -RefPtr parse_css(const CSS::ParsingContext& context, const StringView& css) +RefPtr parse_css(const CSS::DeprecatedParsingContext& context, const StringView& css) { if (css.is_empty()) return CSS::CSSStyleSheet::create({}); @@ -1059,7 +1059,7 @@ RefPtr parse_css(const CSS::ParsingContext& context, const S return parser.parse_sheet(); } -RefPtr parse_css_declaration(const CSS::ParsingContext& context, const StringView& css) +RefPtr parse_css_declaration(const CSS::DeprecatedParsingContext& context, const StringView& css) { if (css.is_empty()) return CSS::CSSStyleDeclaration::create({}, {}); @@ -1072,6 +1072,6 @@ RefPtr parse_html_length(const DOM::Document& document, const S auto integer = string.to_int(); if (integer.has_value()) return CSS::LengthStyleValue::create(CSS::Length::make_px(integer.value())); - return parse_css_value(CSS::ParsingContext(document), string); + return parse_css_value(CSS::DeprecatedParsingContext(document), string); } } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.h b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.h index c40d4c6387..64f755f0b4 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.h @@ -11,11 +11,11 @@ #include namespace Web::CSS { -class ParsingContext { +class DeprecatedParsingContext { public: - ParsingContext(); - explicit ParsingContext(const DOM::Document&); - explicit ParsingContext(const DOM::ParentNode&); + DeprecatedParsingContext(); + explicit DeprecatedParsingContext(const DOM::Document&); + explicit DeprecatedParsingContext(const DOM::ParentNode&); bool in_quirks_mode() const; @@ -28,14 +28,14 @@ private: namespace Web { -RefPtr parse_css(const CSS::ParsingContext&, const StringView&); -RefPtr parse_css_declaration(const CSS::ParsingContext&, const StringView&); -RefPtr parse_css_value(const CSS::ParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); -Optional parse_selector(const CSS::ParsingContext&, const StringView&); +RefPtr parse_css(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr parse_css_declaration(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr parse_css_value(const CSS::DeprecatedParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); +Optional parse_selector(const CSS::DeprecatedParsingContext&, const StringView&); -RefPtr parse_line_width(const CSS::ParsingContext&, const StringView&); -RefPtr parse_color(const CSS::ParsingContext&, const StringView&); -RefPtr parse_line_style(const CSS::ParsingContext&, const StringView&); +RefPtr parse_line_width(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr parse_color(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr parse_line_style(const CSS::DeprecatedParsingContext&, const StringView&); RefPtr parse_html_length(const DOM::Document&, const StringView&); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 2833f12f0d..e12ad96892 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #define CSS_PARSER_TRACE 1 @@ -29,8 +30,33 @@ static void log_parse_error(const SourceLocation& location = SourceLocation::cur namespace Web::CSS { -Parser::Parser(const StringView& input, const String& encoding) - : m_tokenizer(input, encoding) +ParsingContext::ParsingContext() +{ +} + +ParsingContext::ParsingContext(DOM::Document const& document) + : m_document(&document) +{ +} + +ParsingContext::ParsingContext(DOM::ParentNode const& parent_node) + : m_document(&parent_node.document()) +{ +} + +bool ParsingContext::in_quirks_mode() const +{ + return m_document ? m_document->in_quirks_mode() : false; +} + +URL ParsingContext::complete_url(String const& addr) const +{ + return m_document ? m_document->url().complete_url(addr) : URL::create_with_url_or_path(addr); +} + +Parser::Parser(ParsingContext const& context, StringView const& input, String const& encoding) + : m_context(context) + , m_tokenizer(input, encoding) { m_tokens = m_tokenizer.parse(); } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 0ab9421f37..6d3844f9b2 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -26,9 +26,23 @@ class CSSRule; class CSSStyleRule; struct StyleProperty; +class ParsingContext { +public: + ParsingContext(); + explicit ParsingContext(DOM::Document const&); + explicit ParsingContext(DOM::ParentNode const&); + + bool in_quirks_mode() const; + + URL complete_url(String const&) const; + +private: + const DOM::Document* m_document { nullptr }; +}; + class Parser { public: - Parser(const StringView& input, const String& encoding = "utf-8"); + Parser(ParsingContext const&, StringView const& input, String const& encoding = "utf-8"); ~Parser(); // The normal parser entry point, for parsing stylesheets. @@ -87,6 +101,8 @@ private: RefPtr convert_rule(NonnullRefPtr); + ParsingContext m_context; + Tokenizer m_tokenizer; Vector m_tokens; int m_iterator_offset { -1 }; diff --git a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp index ccbaf1712d..141e426adf 100644 --- a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp +++ b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp @@ -109,7 +109,7 @@ static bool matches(const CSS::Selector::SimpleSelector& component, const DOM::E case CSS::Selector::SimpleSelector::PseudoClass::Not: { if (component.not_selector.is_empty()) return false; - auto not_selector = Web::parse_selector(CSS::ParsingContext(element), component.not_selector); + auto not_selector = Web::parse_selector(CSS::DeprecatedParsingContext(element), component.not_selector); if (!not_selector.has_value()) return false; auto not_matches = matches(not_selector.value(), element); diff --git a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp index 673f2e84df..235a884ac8 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp @@ -34,7 +34,7 @@ static StyleSheet& default_stylesheet() if (!sheet) { extern const char default_stylesheet_source[]; String css = default_stylesheet_source; - sheet = parse_css(CSS::ParsingContext(), css).leak_ref(); + sheet = parse_css(CSS::DeprecatedParsingContext(), css).leak_ref(); } return *sheet; } @@ -45,7 +45,7 @@ static StyleSheet& quirks_mode_stylesheet() if (!sheet) { extern const char quirks_mode_stylesheet_source[]; String css = quirks_mode_stylesheet_source; - sheet = parse_css(CSS::ParsingContext(), css).leak_ref(); + sheet = parse_css(CSS::DeprecatedParsingContext(), css).leak_ref(); } return *sheet; } @@ -229,7 +229,7 @@ static inline bool is_background_repeat_property(const StyleValue& value) static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value, DOM::Document& document, bool is_internally_generated_pseudo_property = false) { - CSS::ParsingContext context(document); + CSS::DeprecatedParsingContext context(document); if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) { dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id)); -- cgit v1.2.3