diff options
author | Sam Atkins <atkinssj@gmail.com> | 2021-07-02 20:25:13 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-11 23:19:56 +0200 |
commit | 004ae453d17a5d1d215d3c719fb199bed8919cb5 (patch) | |
tree | a7cd01e6c05cb0b80b82b7952c55f0c18743049d /Userland/Libraries | |
parent | 390cc30a9707b36477b1042359249f7e448038c0 (diff) | |
download | serenity-004ae453d17a5d1d215d3c719fb199bed8919cb5.zip |
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.
Diffstat (limited to 'Userland/Libraries')
10 files changed, 83 insertions, 41 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp index c265cce00a..d99d124cb5 100644 --- a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp +++ b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp @@ -35,7 +35,7 @@ bool CSSStyleDeclarationWrapper::internal_set(const JS::PropertyName& name, JS:: if (vm().exception()) return false; - auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id); + auto new_value = parse_css_value(CSS::DeprecatedParsingContext {}, css_text, property_id); // FIXME: What are we supposed to do if we can't parse it? if (!new_value) return false; 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<Color> parse_css_color(const CSS::ParsingContext&, const StringView& view) +static Optional<Color> 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<float> 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<float> value; @@ -229,7 +229,7 @@ static StringView parse_custom_property_name(const StringView& value) return value.substring_view(4, substring_length); } -RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext& context, const StringView& string, CSS::PropertyID property_id) +RefPtr<CSS::StyleValue> parse_css_value(const CSS::DeprecatedParsingContext& context, const StringView& string, CSS::PropertyID property_id) { bool is_bad_length = false; @@ -269,7 +269,7 @@ RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext& context, cons return CSS::StringStyleValue::create(string); } -RefPtr<CSS::LengthStyleValue> parse_line_width(const CSS::ParsingContext& context, const StringView& part) +RefPtr<CSS::LengthStyleValue> 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<CSS::LengthStyleValue> parse_line_width(const CSS::ParsingContext& contex return nullptr; } -RefPtr<CSS::ColorStyleValue> parse_color(const CSS::ParsingContext& context, const StringView& part) +RefPtr<CSS::ColorStyleValue> 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<CSS::ColorStyleValue> parse_color(const CSS::ParsingContext& context, con return nullptr; } -RefPtr<CSS::IdentifierStyleValue> parse_line_style(const CSS::ParsingContext& context, const StringView& part) +RefPtr<CSS::IdentifierStyleValue> 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<CSS::IdentifierStyleValue> 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<CSS::CSSRule> rules; @@ -1045,13 +1045,13 @@ private: StringView css; }; -Optional<CSS::Selector> parse_selector(const CSS::ParsingContext& context, const StringView& selector_text) +Optional<CSS::Selector> parse_selector(const CSS::DeprecatedParsingContext& context, const StringView& selector_text) { CSSParser parser(context, selector_text); return parser.parse_individual_selector(); } -RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css) +RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::DeprecatedParsingContext& context, const StringView& css) { if (css.is_empty()) return CSS::CSSStyleSheet::create({}); @@ -1059,7 +1059,7 @@ RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext& context, const S return parser.parse_sheet(); } -RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::ParsingContext& context, const StringView& css) +RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::DeprecatedParsingContext& context, const StringView& css) { if (css.is_empty()) return CSS::CSSStyleDeclaration::create({}, {}); @@ -1072,6 +1072,6 @@ RefPtr<CSS::StyleValue> 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 <LibWeb/CSS/CSSStyleSheet.h> 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<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext&, const StringView&); -RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::ParsingContext&, const StringView&); -RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); -Optional<CSS::Selector> parse_selector(const CSS::ParsingContext&, const StringView&); +RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr<CSS::StyleValue> parse_css_value(const CSS::DeprecatedParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); +Optional<CSS::Selector> parse_selector(const CSS::DeprecatedParsingContext&, const StringView&); -RefPtr<CSS::LengthStyleValue> parse_line_width(const CSS::ParsingContext&, const StringView&); -RefPtr<CSS::ColorStyleValue> parse_color(const CSS::ParsingContext&, const StringView&); -RefPtr<CSS::IdentifierStyleValue> parse_line_style(const CSS::ParsingContext&, const StringView&); +RefPtr<CSS::LengthStyleValue> parse_line_width(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr<CSS::ColorStyleValue> parse_color(const CSS::DeprecatedParsingContext&, const StringView&); +RefPtr<CSS::IdentifierStyleValue> parse_line_style(const CSS::DeprecatedParsingContext&, const StringView&); RefPtr<CSS::StyleValue> 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 <LibWeb/CSS/Parser/StyleComponentValueRule.h> #include <LibWeb/CSS/Parser/StyleFunctionRule.h> #include <LibWeb/CSS/Selector.h> +#include <LibWeb/DOM/Document.h> #include <LibWeb/Dump.h> #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<CSSRule> convert_rule(NonnullRefPtr<QualifiedStyleRule>); + ParsingContext m_context; + Tokenizer m_tokenizer; Vector<Token> 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)); diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index b7ef8fba34..bc0506088d 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -159,7 +159,7 @@ void Element::parse_attribute(const FlyString& name, const String& value) m_classes.unchecked_append(new_class); } } else if (name == HTML::AttributeNames::style) { - m_inline_style = parse_css_declaration(CSS::ParsingContext(document()), value); + m_inline_style = parse_css_declaration(CSS::DeprecatedParsingContext(document()), value); set_needs_style_update(true); } } diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp index 924a36fdaf..904a1f4945 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp @@ -13,7 +13,7 @@ namespace Web::DOM { RefPtr<Element> ParentNode::query_selector(const StringView& selector_text) { - auto selector = parse_selector(CSS::ParsingContext(*this), selector_text); + auto selector = parse_selector(CSS::DeprecatedParsingContext(*this), selector_text); if (!selector.has_value()) return {}; @@ -33,7 +33,7 @@ RefPtr<Element> ParentNode::query_selector(const StringView& selector_text) NonnullRefPtrVector<Element> ParentNode::query_selector_all(const StringView& selector_text) { - auto selector = parse_selector(CSS::ParsingContext(*this), selector_text); + auto selector = parse_selector(CSS::DeprecatedParsingContext(*this), selector_text); if (!selector.has_value()) return {}; diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp index 27cb13f661..bfb9597e32 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp @@ -23,7 +23,7 @@ CSSLoader::CSSLoader(DOM::Element& owner_element) void CSSLoader::load_from_text(const String& text) { - m_style_sheet = parse_css(CSS::ParsingContext(m_owner_element.document()), text); + m_style_sheet = parse_css(CSS::DeprecatedParsingContext(m_owner_element.document()), text); if (!m_style_sheet) { m_style_sheet = CSS::CSSStyleSheet::create({}); m_style_sheet->set_owner_node(&m_owner_element); @@ -51,7 +51,7 @@ void CSSLoader::resource_did_load() dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, has encoded data. URL: {}", resource()->url()); } - auto sheet = parse_css(CSS::ParsingContext(m_owner_element.document()), resource()->encoded_data()); + auto sheet = parse_css(CSS::DeprecatedParsingContext(m_owner_element.document()), resource()->encoded_data()); if (!sheet) { dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url()); return; |