diff options
Diffstat (limited to 'libs/litehtml/include/litehtml/style.h')
-rw-r--r-- | libs/litehtml/include/litehtml/style.h | 91 |
1 files changed, 56 insertions, 35 deletions
diff --git a/libs/litehtml/include/litehtml/style.h b/libs/litehtml/include/litehtml/style.h index 6014f02dd0..256fd8f3ca 100644 --- a/libs/litehtml/include/litehtml/style.h +++ b/libs/litehtml/include/litehtml/style.h @@ -1,12 +1,14 @@ #ifndef LH_STYLE_H #define LH_STYLE_H -#include <variant> + +#include "css_tokenizer.h" namespace litehtml { struct invalid {}; // indicates "not found" condition in style::get_property struct inherit {}; // "inherit" was specified as the value of this property - using property_value_base = std::variant< + + struct property_value : variant< invalid, inherit, int, @@ -15,28 +17,25 @@ namespace litehtml length_vector, float, web_color, - std::vector<background_image>, + vector<image>, string, string_vector, - size_vector - >; - - struct property_value : property_value_base + size_vector, + css_token_vector + > { bool m_important = false; - bool m_has_var = false; // string; parsing is delayed because of var() + bool m_has_var = false; // css_token_vector, parsing is delayed because of var() property_value() {} template<class T> property_value(const T& val, bool important, bool has_var = false) - : property_value_base(val), m_important(important), m_has_var(has_var) {} - - template<class T> bool is() const { return std::holds_alternative<T>(*this); } - template<class T> const T& get() const { return std::get<T>(*this); } + : base(val), m_important(important), m_has_var(has_var) {} }; class html_tag; typedef std::map<string_id, property_value> props_map; + // represents a style block, eg. "color: black; display: inline" class style { public: @@ -46,12 +45,11 @@ namespace litehtml props_map m_properties; static std::map<string_id, string> m_valid_values; public: - void add(const string& txt, const string& baseurl = "", document_container* container = nullptr) - { - parse(txt, baseurl, container); - } + void add(const css_token_vector& tokens, const string& baseurl = "", document_container* container = nullptr); + void add(const string& txt, const string& baseurl = "", document_container* container = nullptr); - void add_property(string_id name, const string& val, const string& baseurl = "", bool important = false, document_container* container = nullptr); + void add_property(string_id name, const css_token_vector& tokens, const string& baseurl = "", bool important = false, document_container* container = nullptr); + void add_property(string_id name, const string& val, const string& baseurl = "", bool important = false, document_container* container = nullptr); const property_value& get_property(string_id name) const; @@ -65,28 +63,51 @@ namespace litehtml private: void inherit_property(string_id name, bool important); - void parse_property(const string& txt, const string& baseurl, document_container* container); - void parse(const string& txt, const string& baseurl, document_container* container); - void parse_background(const string& val, const string& baseurl, bool important, document_container* container); - bool parse_one_background(const string& val, document_container* container, background& bg); - void parse_background_image(const string& val, document_container* container, const string& baseurl, bool important); + + void parse_background(const css_token_vector& tokens, const string& baseurl, bool important, document_container* container); + bool parse_bg_layer(const css_token_vector& tokens, document_container* container, background& bg, bool final_layer); + // parse the value of background-image property, which is comma-separated list of <bg-image>s + void parse_background_image(const css_token_vector& tokens, const string& baseurl, bool important, document_container* container); + // parse comma-separated list of keywords - void parse_keyword_comma_list(string_id name, const string& val, bool important); - void parse_background_position(const string& val, bool important); - bool parse_one_background_position(const string& val, css_length& x, css_length& y); - void parse_background_size(const string& val, bool important); - bool parse_one_background_size(const string& val, css_size& size); - void parse_font(const string& val, bool important); - void parse_flex(const string& val, bool important); - void parse_align_self(string_id name, const string& val, bool important); - static css_length parse_border_width(const string& str); - static void parse_two_lengths(const string& str, css_length len[2]); - static int parse_four_lengths(const string& str, css_length len[4]); - static void subst_vars_(string& str, const html_tag* el); + void parse_keyword_comma_list(string_id name, const css_token_vector& tokens, bool important); + void parse_background_position(const css_token_vector& tokens, bool important); + void parse_background_size(const css_token_vector& tokens, bool important); + + void parse_border(const css_token_vector& tokens, bool important, document_container* container); + void parse_border_side(string_id name, const css_token_vector& tokens, bool important, document_container* container); + void parse_border_radius(const css_token_vector& tokens, bool important); + + bool parse_list_style_image(const css_token& tok, string& url); + void parse_list_style(const css_token_vector& tokens, string baseurl, bool important); + void parse_font(css_token_vector tokens, bool important); + + void parse_flex_flow(const css_token_vector& tokens, bool important); + void parse_flex(const css_token_vector& tokens, bool important); + void parse_align_self(string_id name, const css_token_vector& tokens, bool important); + void add_parsed_property(string_id name, const property_value& propval); + void add_length_property(string_id name, css_token val, string keywords, int options, bool important); + template<class T> void add_four_properties(string_id top_name, T val[4], int n, bool important); void remove_property(string_id name, bool important); }; -} + + bool parse_url(const css_token& token, string& url); + bool parse_length(const css_token& tok, css_length& length, int options, string keywords = ""); + bool parse_angle(const css_token& tok, float& angle, bool percents_allowed = false); + bool parse_bg_position(const css_token_vector& tokens, int& index, css_length& x, css_length& y, bool convert_keywords_to_percents); + + template<typename Enum> + bool parse_keyword(const css_token& tok, Enum& val, string keywords, int first_keyword_value = 0) + { + int value_index(const string& val, const string& strings, int defValue = -1, char delim = ';'); + int idx = value_index(tok.ident(), keywords); + if (idx == -1) return false; + val = (Enum)(first_keyword_value + idx); + return true; + } + +} // namespace litehtml #endif // LH_STYLE_H |