srctree

Robin Linden parent 605248ec 271beeba
style: Wrap the border width properties in UnresolvedValue

layout/layout.cpp added: 32, removed: 24, total 8
@@ -24,7 +24,6 @@
#include <cstdlib>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <ranges>
@@ -519,41 +518,25 @@ void Layouter::calculate_padding(LayoutBox &box, int const font_size) const {
padding.bottom = box.get_property<css::PropertyId::PaddingBottom>().resolve(font_size, root_font_size_);
}
 
// https://drafts.csswg.org/css-backgrounds/#the-border-width
// NOLINTNEXTLINE(cert-err58-cpp)
std::map<std::string_view, int> const border_width_keywords{
{"thin", 3},
{"medium", 5},
{"thick", 7},
};
 
void Layouter::calculate_border(LayoutBox &box, int const font_size) const {
auto as_px = [&](std::string_view border_width_property) {
if (auto it = border_width_keywords.find(border_width_property); it != border_width_keywords.end()) {
return it->second;
}
 
return style::to_px(border_width_property, font_size, root_font_size_);
};
 
if (box.get_property<css::PropertyId::BorderLeftStyle>() != style::BorderStyle::None) {
auto border_width = box.get_property<css::PropertyId::BorderLeftWidth>();
box.dimensions.border.left = as_px(border_width);
box.dimensions.border.left = border_width.resolve(font_size, root_font_size_);
}
 
if (box.get_property<css::PropertyId::BorderRightStyle>() != style::BorderStyle::None) {
auto border_width = box.get_property<css::PropertyId::BorderRightWidth>();
box.dimensions.border.right = as_px(border_width);
box.dimensions.border.right = border_width.resolve(font_size, root_font_size_);
}
 
if (box.get_property<css::PropertyId::BorderTopStyle>() != style::BorderStyle::None) {
auto border_width = box.get_property<css::PropertyId::BorderTopWidth>();
box.dimensions.border.top = as_px(border_width);
box.dimensions.border.top = border_width.resolve(font_size, root_font_size_);
}
 
if (box.get_property<css::PropertyId::BorderBottomStyle>() != style::BorderStyle::None) {
auto border_width = box.get_property<css::PropertyId::BorderBottomWidth>();
box.dimensions.border.bottom = as_px(border_width);
box.dimensions.border.bottom = border_width.resolve(font_size, root_font_size_);
}
}
 
 
style/styled_node.cpp added: 32, removed: 24, total 8
@@ -198,8 +198,24 @@ std::optional<std::pair<float, std::string_view>> split_into_value_and_unit(std:
return std::pair{res, unit};
}
 
// https://drafts.csswg.org/css-backgrounds/#the-border-width
// NOLINTNEXTLINE(cert-err58-cpp)
std::map<std::string_view, int> const border_width_keywords{
{"thin", 3},
{"medium", 5},
{"thick", 7},
};
 
} // namespace
 
int UnresolvedBorderWidth::resolve(int font_size, int root_font_size, std::optional<int> percent_relative_to) const {
if (auto it = border_width_keywords.find(width.raw); it != border_width_keywords.end()) {
return it->second;
}
 
return width.resolve(font_size, root_font_size, percent_relative_to);
}
 
std::string_view StyledNode::get_raw_property(css::PropertyId property) const {
// We don't support selector specificity yet, so the last property is found
// in order to allow website style to override the browser built-in style.
 
style/styled_node.h added: 32, removed: 24, total 8
@@ -105,6 +105,12 @@ enum class WhiteSpace : std::uint8_t {
PreLine,
};
 
struct UnresolvedBorderWidth {
UnresolvedValue width{};
 
int resolve(int font_size, int root_font_size, std::optional<int> percent_relative_to = std::nullopt) const;
};
 
struct StyledNode {
dom::Node const &node;
std::vector<std::pair<css::PropertyId, std::string>> properties;
@@ -161,6 +167,9 @@ struct StyledNode {
|| T == css::PropertyId::PaddingBottom || T == css::PropertyId::MinHeight
|| T == css::PropertyId::Height || T == css::PropertyId::MaxHeight) {
return UnresolvedValue{get_raw_property(T)};
} else if constexpr (T == css::PropertyId::BorderBottomWidth || T == css::PropertyId::BorderLeftWidth
|| T == css::PropertyId::BorderRightWidth || T == css::PropertyId::BorderTopWidth) {
return UnresolvedBorderWidth{{get_raw_property(T)}};
} else {
return get_raw_property(T);
}