diff options
Diffstat (limited to 'libs/litehtml/src/style.cpp')
-rw-r--r-- | libs/litehtml/src/style.cpp | 147 |
1 files changed, 100 insertions, 47 deletions
diff --git a/libs/litehtml/src/style.cpp b/libs/litehtml/src/style.cpp index 727f01aa10..05d366d867 100644 --- a/libs/litehtml/src/style.cpp +++ b/libs/litehtml/src/style.cpp @@ -87,12 +87,39 @@ void style::parse_property(const string& txt, const string& baseurl, document_co } } +void style::inherit_property(string_id name, bool important) +{ + switch (name) + { + case _font_: + add_parsed_property(_font_style_, property_value(inherit(), important)); + add_parsed_property(_font_variant_, property_value(inherit(), important)); + add_parsed_property(_font_weight_, property_value(inherit(), important)); + add_parsed_property(_font_size_, property_value(inherit(), important)); + add_parsed_property(_line_height_, property_value(inherit(), important)); + break; + case _background_: + add_parsed_property(_background_color_, property_value(inherit(), important)); + add_parsed_property(_background_position_x_, property_value(inherit(), important)); + add_parsed_property(_background_position_y_, property_value(inherit(), important)); + add_parsed_property(_background_repeat_, property_value(inherit(), important)); + add_parsed_property(_background_attachment_, property_value(inherit(), important)); + add_parsed_property(_background_image_, property_value(inherit(), important)); + add_parsed_property(_background_image_baseurl_, property_value(inherit(), important)); + add_parsed_property(_background_size_, property_value(inherit(), important)); + add_parsed_property(_background_origin_, property_value(inherit(), important)); + add_parsed_property(_background_clip_, property_value(inherit(), important)); + break; + default: + add_parsed_property(name, property_value(inherit(), important)); + } +} + void style::add_property(string_id name, const string& val, const string& baseurl, bool important, document_container* container) { - if (val.find("var(") != string::npos) return add_parsed_property(name, property_value(val, important, prop_type_var)); - if (val == "inherit" && name != _font_) return add_parsed_property(name, property_value(important, prop_type_inherit)); + if (val.find("var(") != string::npos) return add_parsed_property(name, property_value(val, important, true)); + if (val == "inherit") return inherit_property(name, important); - int idx; string url; css_length len[4], length; @@ -131,14 +158,14 @@ void style::add_property(string_id name, const string& val, const string& baseur case _align_content_: case _caption_side_: - - idx = value_index(val, m_valid_values[name]); + { + int idx = value_index(val, m_valid_values[name]); if (idx >= 0) { add_parsed_property(name, property_value(idx, important)); } break; - + } case _align_items_: case _align_self_: parse_align_self(name, val, important); @@ -195,7 +222,7 @@ void style::add_property(string_id name, const string& val, const string& baseur break; case _background_image_: - parse_background_image(val, baseurl, important); + parse_background_image(val, container, baseurl, important); break; case _background_attachment_: @@ -620,10 +647,11 @@ void style::parse_background(const string& val, const string& baseurl, bool impo if (tokens.empty()) return; web_color color; - string_vector images; + std::vector<background_image> images; int_vector repeats, origins, clips, attachments; length_vector x_positions, y_positions; size_vector sizes; + background_gradient grad; for (const auto& token : tokens) { @@ -657,7 +685,7 @@ void style::parse_background(const string& val, const string& baseurl, bool impo bool style::parse_one_background(const string& val, document_container* container, background& bg) { bg.m_color = web_color::transparent; - bg.m_image = {""}; + bg.m_image = { background_image() }; bg.m_repeat = { background_repeat_repeat }; bg.m_origin = { background_box_padding }; bg.m_clip = { background_box_border }; @@ -690,7 +718,10 @@ bool style::parse_one_background(const string& val, document_container* containe if (image_found) return false; string url; css::parse_css_url(token, url); - bg.m_image = { url }; + background_image img; + img.type = background_image::bg_image_type_url; + img.url = url; + bg.m_image = { img }; image_found = true; } else if( (idx = value_index(token, background_repeat_strings)) >= 0 ) { @@ -727,6 +758,17 @@ bool style::parse_one_background(const string& val, document_container* containe if (color_found) return false; bg.m_color = web_color::from_string(token, container); color_found = true; + } else if ( token.substr(0, 15) == "linear-gradient" || token.substr(0, 25) == "repeating-linear-gradient" || + token.substr(0, 15) == "radial-gradient" || token.substr(0, 25) == "repeating-radial-gradient" || + token.substr(0, 14) == "conic-gradient" || token.substr(0, 24) == "repeating-conic-gradient") + { + if (image_found) return false; + background_image img; + img.type = background_image::bg_image_type_gradient; + css::parse_gradient(token, container, img.gradient); + if(img.is_empty()) return false; + bg.m_image = { img }; + image_found = true; } else { @@ -734,9 +776,9 @@ bool style::parse_one_background(const string& val, document_container* containe } } - if (position != "") + if (!position.empty()) { - string_vector tokens; + tokens.clear(); split_string(position, tokens, "/"); if (tokens.size() > 2) return false; @@ -744,30 +786,51 @@ bool style::parse_one_background(const string& val, document_container* containe if (tokens.size() == 2 && !parse_one_background_size(tokens[1], bg.m_size[0])) return false; - if (tokens.size() > 0 && !parse_one_background_position(tokens[0], bg.m_position_x[0], bg.m_position_y[0])) + if (!tokens.empty() && !parse_one_background_position(tokens[0], bg.m_position_x[0], bg.m_position_y[0])) return false; } return true; } -void style::parse_background_image(const string& val, const string& baseurl, bool important) +void style::parse_background_image(const string& val, document_container* container, const string& baseurl, bool important) { string_vector tokens; split_string(val, tokens, ",", "", "("); if (tokens.empty()) return; - string_vector images; + std::vector<background_image> images; - for (const auto& token : tokens) + for (auto& token : tokens) { - string url; - css::parse_css_url(token, url); - images.push_back(url); + trim(token); + if(token.substr(0, 3) == "url") + { + string url; + css::parse_css_url(token, url); + background_image img; + img.type = background_image::bg_image_type_url; + img.url = url; + images.emplace_back(img); + } else if (token.substr(0, 15) == "linear-gradient" || token.substr(0, 25) == "repeating-linear-gradient" || + token.substr(0, 15) == "radial-gradient" || token.substr(0, 25) == "repeating-radial-gradient" || + token.substr(0, 14) == "conic-gradient" || token.substr(0, 24) == "repeating-conic-gradient") + { + background_image img; + img.type = background_image::bg_image_type_gradient; + css::parse_gradient(token, container, img.gradient); + if(!img.is_empty()) + { + images.emplace_back(img); + } + } } - add_parsed_property(_background_image_, property_value(images, important)); - add_parsed_property(_background_image_baseurl_, property_value(baseurl, important)); + if(!images.empty()) + { + add_parsed_property(_background_image_, property_value(images, important)); + add_parsed_property(_background_image_baseurl_, property_value(baseurl, important)); + } } void style::parse_keyword_comma_list(string_id name, const string& val, bool important) @@ -812,7 +875,7 @@ void style::parse_background_position(const string& val, bool important) bool style::parse_one_background_position(const string& val, css_length& x, css_length& y) { string_vector pos; - split_string(val, pos, " \t"); + split_string(val, pos, split_delims_spaces); if (pos.empty() || pos.size() > 2) { @@ -920,7 +983,7 @@ void style::parse_background_size(const string& val, bool important) bool style::parse_one_background_size(const string& val, css_size& size) { string_vector res; - split_string(val, res, " \t"); + split_string(val, res, split_delims_spaces); if (res.empty()) { return false; @@ -940,22 +1003,11 @@ bool style::parse_one_background_size(const string& val, css_size& size) void style::parse_font(const string& val, bool important) { - if (val == "inherit") - { - add_parsed_property(_font_style_, property_value(important, prop_type_inherit)); - add_parsed_property(_font_variant_, property_value(important, prop_type_inherit)); - add_parsed_property(_font_weight_, property_value(important, prop_type_inherit)); - add_parsed_property(_font_size_, property_value(important, prop_type_inherit)); - add_parsed_property(_line_height_, property_value(important, prop_type_inherit)); - return; - } else - { - add_parsed_property(_font_style_, property_value(font_style_normal, important)); - add_parsed_property(_font_variant_, property_value(font_variant_normal, important)); - add_parsed_property(_font_weight_, property_value(font_weight_normal, important)); - add_parsed_property(_font_size_, property_value(font_size_medium, important)); - add_parsed_property(_line_height_, property_value(line_height_normal, important)); - } + add_parsed_property(_font_style_, property_value(font_style_normal, important)); + add_parsed_property(_font_variant_, property_value(font_variant_normal, important)); + add_parsed_property(_font_weight_, property_value(font_weight_normal, important)); + add_parsed_property(_font_size_, property_value(font_size_medium, important)); + add_parsed_property(_line_height_, property_value(line_height_normal, important)); string_vector tokens; split_string(val, tokens, " ", "", "\""); @@ -1182,11 +1234,11 @@ const property_value& style::get_property(string_id name) const { return it->second; } - static property_value dummy; - return dummy; + static property_value _invalid(invalid(), false); + return _invalid; } -void style::subst_vars_(string& str, const element* el) +void style::subst_vars_(string& str, const html_tag* el) { while (true) { @@ -1202,17 +1254,18 @@ void style::subst_vars_(string& str, const element* el) } } -void style::subst_vars(const element* el) +void style::subst_vars(const html_tag* el) { for (auto& prop : m_properties) { - if (prop.second.m_type == prop_type_var) + if (prop.second.m_has_var) { - subst_vars_(prop.second.m_string, el); + string str = prop.second.get<string>(); + subst_vars_(str, el); // re-adding the same property - // if it is a custom property it will be readded as a string (currently it is prop_type_var) + // if it is a custom property it will be readded as a string // if it is a standard css property it will be parsed and properly added as typed property - add_property(prop.first, prop.second.m_string, "", prop.second.m_important, el->get_document()->container()); + add_property(prop.first, str, "", prop.second.m_important, el->get_document()->container()); } } } |