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>
# include <AK/Vector.h>
# include <LibWeb/CSS/Parser/AtStyleRule.h>
# include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
# include <LibWeb/CSS/Parser/QualifiedStyleRule.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>
# include <LibWeb/CSS/Parser/Tokenizer.h>
# include <LibWeb/CSS/Selector.h>
namespace Web : : CSS {
2021-07-09 16:34:29 +01:00
class CSSStyleSheet ;
class CSSRule ;
class CSSStyleRule ;
struct StyleProperty ;
2021-07-02 20:25:13 +01:00
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 } ;
} ;
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-09 16:34:29 +01:00
Vector < StyleProperty > 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-03-22 17:41:47 +01:00
2021-07-03 12:34:25 +01:00
Optional < Selector > parse_single_selector ( Vector < StyleComponentValueRule > parts , bool is_relative = false ) ;
2021-03-22 17:41:47 +01:00
// FIXME: https://www.w3.org/TR/selectors-4/
2021-07-03 12:34:25 +01:00
// Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
Vector < Selector > parse_a_selector ( ) ;
Vector < Selector > parse_a_selector ( Vector < Vector < StyleComponentValueRule > > & ) ;
Vector < Selector > parse_a_relative_selector ( ) ;
2021-03-22 17:41:47 +01:00
bool match_a_selector_against_an_element ( ) { return false ; }
bool match_a_selector_against_a_pseudo_element ( ) { return false ; }
bool match_a_selector_against_a_tree ( ) { return false ; }
// FIXME: https://drafts.csswg.org/css-backgrounds-3/
static Optional < String > as_valid_background_repeat ( String input ) { return input ; }
static Optional < String > as_valid_background_attachment ( String input ) { return input ; }
static Optional < String > as_valid_background_position ( String input ) { return input ; }
static Optional < String > as_valid_background_clip ( String input ) { return input ; }
static Optional < String > as_valid_background_origin ( String input ) { return input ; }
static Optional < String > as_valid_background_size ( String input ) { return input ; }
static Optional < String > as_valid_border_style ( String input ) { return input ; }
static Optional < String > as_valid_border_image_repeat ( String input ) { return input ; }
void dump_all_tokens ( ) ;
private :
Token next_token ( ) ;
Token peek_token ( ) ;
Token current_token ( ) ;
void reconsume_current_input_token ( ) ;
2021-07-01 16:49:33 +01:00
NonnullRefPtrVector < QualifiedStyleRule > consume_a_list_of_rules ( bool top_level ) ;
NonnullRefPtr < AtStyleRule > consume_an_at_rule ( ) ;
RefPtr < QualifiedStyleRule > consume_a_qualified_rule ( ) ;
2021-03-22 17:41:47 +01:00
Vector < DeclarationOrAtRule > consume_a_list_of_declarations ( ) ;
Optional < StyleDeclarationRule > consume_a_declaration ( Vector < StyleComponentValueRule > ) ;
Optional < StyleDeclarationRule > consume_a_declaration ( ) ;
StyleComponentValueRule consume_a_component_value ( ) ;
2021-07-01 14:13:48 +01:00
NonnullRefPtr < StyleBlockRule > consume_a_simple_block ( ) ;
NonnullRefPtr < StyleFunctionRule > consume_a_function ( ) ;
2021-03-22 17:41:47 +01:00
2021-07-09 16:34:29 +01:00
RefPtr < CSSRule > convert_rule ( NonnullRefPtr < QualifiedStyleRule > ) ;
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 ;
int m_iterator_offset { - 1 } ;
} ;
}