diff options
author | Sam Atkins <atkinssj@gmail.com> | 2021-07-30 17:48:23 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-08-02 19:01:25 +0430 |
commit | 4065eb169c9d1e1ab4ec1f42291360b493e73c29 (patch) | |
tree | 1eb82ab4b056fa1b675ca524da1047aed2e3719b /Userland/Libraries | |
parent | eadcdd21e3367f4a3c0a4376099fca69d856b37c (diff) | |
download | serenity-4065eb169c9d1e1ab4ec1f42291360b493e73c29.zip |
LibWeb: Implement CSS parsing convenience functions
These mostly match the API in `DeprecatedCSSParser.h`. The exception is
that `parse_selector()` returns a `SelectorList` instead of just one
`Selector`. The only uses of that are in
`ParentNode::query_selector[_all]()` which should be matching against a
list, according to the spec.
`parse_html_length()` is an odd case. It's used for `width="200"` cases
in HTML, so is not really CSS related, but does produce a StyleValue.
The values allowed in `width/height` in HTML vary per element, but they
are a lot more restricted than in CSS, so it's slightly inappropriate to
use the CSS parser for them, even though it's convenient.
We also ignore a few functions:
- `parse_line_width()`
- `parse_line_style()`
- `parse_color()`
These are all only used in `StyleResolver`, when it is given a property
value as a String. That won't happen once the old parser is removed.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 50 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 13 |
2 files changed, 63 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index c367dd1b95..509c5594b0 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1777,6 +1777,13 @@ RefPtr<StyleValue> Parser::parse_box_shadow_value(ParsingContext const& context, return BoxShadowStyleValue::create(offset_x, offset_y, blur_radius, color); } +RefPtr<StyleValue> Parser::parse_as_css_value(PropertyID property_id) +{ + auto component_values = parse_as_list_of_component_values(); + auto tokens = TokenStream(component_values); + return parse_css_value(property_id, tokens); +} + RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<StyleComponentValueRule>& tokens) { Vector<StyleComponentValueRule> component_values; @@ -2405,3 +2412,46 @@ OwnPtr<CalculatedStyleValue::CalcSum> Parser::parse_calc_sum(ParsingContext cons } } + +namespace Web { + +RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const& context, StringView const& css) +{ + if (css.is_empty()) + return CSS::CSSStyleSheet::create({}); + CSS::Parser parser(context, css); + return parser.parse_as_stylesheet(); +} + +RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const& context, StringView const& css) +{ + if (css.is_empty()) + return CSS::CSSStyleDeclaration::create({}, {}); + CSS::Parser parser(context, css); + return parser.parse_as_list_of_declarations(); +} + +RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const& context, StringView const& string, CSS::PropertyID property_id) +{ + if (string.is_empty()) + return {}; + CSS::Parser parser(context, string); + return parser.parse_as_css_value(property_id); +} + +Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const& context, StringView const& selector_text) +{ + CSS::Parser parser(context, selector_text); + return parser.parse_as_selector(); +} + +RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const& document, StringView const& string) +{ + auto integer = string.to_int(); + if (integer.has_value()) + return CSS::LengthStyleValue::create(CSS::Length::make_px(integer.value())); + // FIXME: The const_cast is a hack. + return parse_css_value(CSS::ParsingContext(const_cast<DOM::Document&>(document)), string); +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 2e2f35a34d..ffe7d7c223 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -93,6 +93,8 @@ public: Optional<SelectorList> parse_as_selector(); Optional<SelectorList> parse_as_relative_selector(); + RefPtr<StyleValue> parse_as_css_value(PropertyID); + // FIXME: These want to be private, but StyleResolver still uses them for now. RefPtr<StyleValue> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&); static RefPtr<StyleValue> parse_css_value(ParsingContext const&, PropertyID, StyleComponentValueRule const&); @@ -206,3 +208,14 @@ private: }; } + +namespace Web { + +RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView const&); +RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const&, StringView const&); +RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView const&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); +Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView const&); + +RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const&, StringView const&); + +} |