2021-03-22 17:41:47 +01:00
/*
2021-04-28 22:46:44 +02:00
* Copyright ( c ) 2020 - 2021 , the SerenityOS developers .
2021-06-29 17:03:25 +01:00
* Copyright ( c ) 2021 , Sam Atkins < atkinssj @ gmail . com >
2021-03-22 17:41:47 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2021-03-22 17:41:47 +01:00
*/
# pragma once
# include <AK/NonnullOwnPtrVector.h>
2021-07-12 17:30:40 +01:00
# include <AK/NonnullRefPtrVector.h>
# include <AK/RefPtr.h>
2021-07-23 16:13:07 +01:00
# include <AK/Result.h>
2021-03-22 17:41:47 +01:00
# include <AK/Vector.h>
# include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
# include <LibWeb/CSS/Parser/StyleBlockRule.h>
# include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
# include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
# include <LibWeb/CSS/Parser/StyleFunctionRule.h>
2021-07-03 13:36:33 +01:00
# include <LibWeb/CSS/Parser/StyleRule.h>
2021-03-22 17:41:47 +01:00
# include <LibWeb/CSS/Parser/Tokenizer.h>
# include <LibWeb/CSS/Selector.h>
2021-07-18 17:12:23 +01:00
# include <LibWeb/CSS/StyleValue.h>
2021-03-22 17:41:47 +01:00
namespace Web : : CSS {
2021-07-09 16:34:29 +01:00
class CSSStyleSheet ;
class CSSRule ;
class CSSStyleRule ;
struct StyleProperty ;
2021-07-08 21:53:22 +01:00
enum class PropertyID ;
2021-07-09 16:34:29 +01:00
2021-07-02 20:25:13 +01:00
class ParsingContext {
public :
ParsingContext ( ) ;
2021-07-22 13:00:29 +01:00
explicit ParsingContext ( DOM : : Document & ) ;
explicit ParsingContext ( DOM : : ParentNode & ) ;
2021-07-02 20:25:13 +01:00
bool in_quirks_mode ( ) const ;
2021-07-22 13:00:29 +01:00
DOM : : Document * document ( ) const { return m_document ; }
2021-07-02 20:25:13 +01:00
URL complete_url ( String const & ) const ;
private :
2021-07-22 13:00:29 +01:00
DOM : : Document * m_document { nullptr } ;
2021-07-02 20:25:13 +01:00
} ;
2021-07-03 14:00:41 +01:00
template < typename T >
class TokenStream {
public :
explicit TokenStream ( Vector < T > const & ) ;
~ TokenStream ( ) ;
bool has_next_token ( ) ;
T const & next_token ( ) ;
2021-07-28 16:27:45 +01:00
T const & peek_token ( int offset = 0 ) ;
2021-07-03 14:00:41 +01:00
T const & current_token ( ) ;
void reconsume_current_input_token ( ) ;
void skip_whitespace ( ) ;
void dump_all_tokens ( ) ;
private :
Vector < T > const & m_tokens ;
int m_iterator_offset { - 1 } ;
T make_eof ( ) ;
T m_eof ;
} ;
2021-03-22 17:41:47 +01:00
class Parser {
public :
2021-07-02 20:25:13 +01:00
Parser ( ParsingContext const & , StringView const & input , String const & encoding = " utf-8 " ) ;
2021-03-22 17:41:47 +01:00
~ Parser ( ) ;
// The normal parser entry point, for parsing stylesheets.
2021-07-09 16:34:29 +01:00
NonnullRefPtr < CSSStyleSheet > parse_as_stylesheet ( ) ;
2021-03-22 17:41:47 +01:00
// For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
2021-07-09 16:34:29 +01:00
NonnullRefPtrVector < CSSRule > parse_as_list_of_rules ( ) ;
2021-03-22 17:41:47 +01:00
// For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
2021-07-09 16:34:29 +01:00
RefPtr < CSSRule > parse_as_rule ( ) ;
2021-03-22 17:41:47 +01:00
// Used in @supports conditions. [CSS3-CONDITIONAL]
2021-07-09 16:34:29 +01:00
Optional < StyleProperty > parse_as_declaration ( ) ;
2021-03-22 17:41:47 +01:00
// For the contents of a style attribute, which parses text into the contents of a single style rule.
2021-07-03 15:40:06 +01:00
RefPtr < CSSStyleDeclaration > parse_as_list_of_declarations ( ) ;
2021-03-22 17:41:47 +01:00
// For things that need to consume a single value, like the parsing rules for attr().
Optional < StyleComponentValueRule > parse_as_component_value ( ) ;
// For the contents of presentational attributes, which parse text into a single declaration’ s value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute.
Vector < StyleComponentValueRule > parse_as_list_of_component_values ( ) ;
2021-07-02 19:52:07 +01:00
Vector < Vector < StyleComponentValueRule > > parse_as_comma_separated_list_of_component_values ( ) ;
2021-07-08 13:44:01 +01:00
2021-07-03 12:34:25 +01:00
// Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
2021-07-30 17:06:48 +01:00
Optional < SelectorList > parse_as_selector ( ) ;
Optional < SelectorList > parse_as_relative_selector ( ) ;
2021-07-30 17:48:23 +01:00
RefPtr < StyleValue > parse_as_css_value ( PropertyID ) ;
2021-07-30 17:06:48 +01:00
// 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 & ) ;
private :
template < typename T >
NonnullRefPtr < CSSStyleSheet > parse_a_stylesheet ( TokenStream < T > & ) ;
template < typename T >
NonnullRefPtrVector < CSSRule > parse_a_list_of_rules ( TokenStream < T > & ) ;
template < typename T >
RefPtr < CSSRule > parse_a_rule ( TokenStream < T > & ) ;
template < typename T >
Optional < StyleProperty > parse_a_declaration ( TokenStream < T > & ) ;
template < typename T >
RefPtr < CSSStyleDeclaration > parse_a_list_of_declarations ( TokenStream < T > & ) ;
template < typename T >
Optional < StyleComponentValueRule > parse_a_component_value ( TokenStream < T > & ) ;
template < typename T >
Vector < StyleComponentValueRule > parse_a_list_of_component_values ( TokenStream < T > & ) ;
template < typename T >
Vector < Vector < StyleComponentValueRule > > parse_a_comma_separated_list_of_component_values ( TokenStream < T > & ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-23 16:13:07 +01:00
Optional < SelectorList > parse_a_selector ( TokenStream < T > & ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-23 16:13:07 +01:00
Optional < SelectorList > parse_a_relative_selector ( TokenStream < T > & ) ;
2021-07-30 17:06:48 +01:00
template < typename T >
Optional < SelectorList > parse_a_selector_list ( TokenStream < T > & ) ;
template < typename T >
Optional < SelectorList > parse_a_relative_selector_list ( TokenStream < T > & ) ;
2021-03-22 17:41:47 +01:00
2021-07-30 17:06:48 +01:00
Optional < Selector : : SimpleSelector : : ANPlusBPattern > parse_a_n_plus_b_pattern ( TokenStream < StyleComponentValueRule > & ) ;
2021-03-22 17:41:47 +01:00
2021-07-03 15:40:06 +01:00
[ [ nodiscard ] ] NonnullRefPtrVector < StyleRule > consume_a_list_of_rules ( bool top_level ) ;
template < typename T >
[ [ nodiscard ] ] NonnullRefPtrVector < StyleRule > consume_a_list_of_rules ( TokenStream < T > & , bool top_level ) ;
[ [ nodiscard ] ] NonnullRefPtr < StyleRule > consume_an_at_rule ( ) ;
template < typename T >
[ [ nodiscard ] ] NonnullRefPtr < StyleRule > consume_an_at_rule ( TokenStream < T > & ) ;
[ [ nodiscard ] ] RefPtr < StyleRule > consume_a_qualified_rule ( ) ;
template < typename T >
[ [ nodiscard ] ] RefPtr < StyleRule > consume_a_qualified_rule ( TokenStream < T > & ) ;
[ [ nodiscard ] ] Vector < DeclarationOrAtRule > consume_a_list_of_declarations ( ) ;
template < typename T >
[ [ nodiscard ] ] Vector < DeclarationOrAtRule > consume_a_list_of_declarations ( TokenStream < T > & ) ;
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] Optional < StyleDeclarationRule > consume_a_declaration ( ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] Optional < StyleDeclarationRule > consume_a_declaration ( TokenStream < T > & ) ;
2021-07-03 15:40:06 +01:00
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] StyleComponentValueRule consume_a_component_value ( ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] StyleComponentValueRule consume_a_component_value ( TokenStream < T > & ) ;
2021-07-03 15:40:06 +01:00
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] NonnullRefPtr < StyleBlockRule > consume_a_simple_block ( ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] NonnullRefPtr < StyleBlockRule > consume_a_simple_block ( TokenStream < T > & ) ;
2021-07-03 15:40:06 +01:00
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] NonnullRefPtr < StyleFunctionRule > consume_a_function ( ) ;
2021-07-03 15:40:06 +01:00
template < typename T >
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] NonnullRefPtr < StyleFunctionRule > consume_a_function ( TokenStream < T > & ) ;
2021-03-22 17:41:47 +01:00
2021-07-08 21:53:22 +01:00
[ [ nodiscard ] ] RefPtr < CSSRule > convert_to_rule ( NonnullRefPtr < StyleRule > ) ;
[ [ nodiscard ] ] RefPtr < CSSStyleDeclaration > convert_to_declaration ( NonnullRefPtr < StyleBlockRule > ) ;
2021-07-06 16:32:18 +01:00
[ [ nodiscard ] ] Optional < StyleProperty > convert_to_style_property ( StyleDeclarationRule & ) ;
2021-07-09 16:34:29 +01:00
2021-07-09 21:04:34 +01:00
static Optional < float > try_parse_float ( StringView string ) ;
2021-07-28 14:10:03 +01:00
static Optional < Color > parse_color ( ParsingContext const & , StyleComponentValueRule const & ) ;
static Optional < Length > parse_length ( ParsingContext const & , StyleComponentValueRule const & ) ;
2021-07-22 17:51:07 +01:00
static Optional < URL > parse_url_function ( ParsingContext const & , StyleComponentValueRule const & ) ;
2021-07-09 21:04:34 +01:00
2021-07-28 16:29:11 +01:00
static RefPtr < StyleValue > parse_builtin_or_dynamic_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
2021-07-18 17:12:23 +01:00
static RefPtr < StyleValue > parse_length_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
static RefPtr < StyleValue > parse_numeric_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
static RefPtr < StyleValue > parse_identifier_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
static RefPtr < StyleValue > parse_color_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
static RefPtr < StyleValue > parse_string_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
2021-07-22 13:00:29 +01:00
static RefPtr < StyleValue > parse_image_value ( ParsingContext const & , StyleComponentValueRule const & ) ;
2021-08-03 14:56:08 +01:00
static RefPtr < StyleValue > parse_background_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-05 21:11:38 +01:00
static RefPtr < StyleValue > parse_border_value ( ParsingContext const & , PropertyID , Vector < StyleComponentValueRule > const & ) ;
2021-08-06 16:55:08 +01:00
static RefPtr < StyleValue > parse_border_radius_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
static RefPtr < StyleValue > parse_border_radius_shorthand_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-07-28 14:12:28 +01:00
static RefPtr < StyleValue > parse_box_shadow_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-04 17:48:08 +01:00
static RefPtr < StyleValue > parse_flex_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-05 17:19:29 +01:00
static RefPtr < StyleValue > parse_flex_flow_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-03 11:37:24 +01:00
static RefPtr < StyleValue > parse_font_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-03 15:56:51 +01:00
static RefPtr < StyleValue > parse_list_style_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-09 14:54:40 +01:00
static RefPtr < StyleValue > parse_overflow_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-08-04 12:34:14 +01:00
static RefPtr < StyleValue > parse_text_decoration_value ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-07-14 16:20:06 +01:00
2021-07-28 16:30:59 +01:00
// calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
static OwnPtr < CalculatedStyleValue : : CalcSum > parse_calc_sum ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcProduct > parse_calc_product ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static Optional < CalculatedStyleValue : : CalcValue > parse_calc_value ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcNumberSum > parse_calc_number_sum ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcNumberProduct > parse_calc_number_product ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static Optional < CalculatedStyleValue : : CalcNumberValue > parse_calc_number_value ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcProductPartWithOperator > parse_calc_product_part_with_operator ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcSumPartWithOperator > parse_calc_sum_part_with_operator ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcNumberProductPartWithOperator > parse_calc_number_product_part_with_operator ( ParsingContext const & , TokenStream < StyleComponentValueRule > & tokens ) ;
static OwnPtr < CalculatedStyleValue : : CalcNumberSumPartWithOperator > parse_calc_number_sum_part_with_operator ( ParsingContext const & , TokenStream < StyleComponentValueRule > & ) ;
static OwnPtr < CalculatedStyleValue : : CalcSum > parse_calc_expression ( ParsingContext const & , Vector < StyleComponentValueRule > const & ) ;
2021-07-23 16:13:07 +01:00
enum class SelectorParsingResult {
Done ,
SyntaxError ,
} ;
RefPtr < Selector > parse_complex_selector ( TokenStream < StyleComponentValueRule > & , bool allow_starting_combinator ) ;
Result < Selector : : CompoundSelector , SelectorParsingResult > parse_compound_selector ( TokenStream < StyleComponentValueRule > & ) ;
Optional < Selector : : Combinator > parse_selector_combinator ( TokenStream < StyleComponentValueRule > & ) ;
Result < Selector : : SimpleSelector , SelectorParsingResult > parse_simple_selector ( TokenStream < StyleComponentValueRule > & ) ;
2021-07-02 20:25:13 +01:00
ParsingContext m_context ;
2021-03-22 17:41:47 +01:00
Tokenizer m_tokenizer ;
Vector < Token > m_tokens ;
2021-07-03 14:00:41 +01:00
TokenStream < Token > m_token_stream ;
2021-03-22 17:41:47 +01:00
} ;
}
2021-07-30 17:48:23 +01:00
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 & ) ;
}