| 1 | // Small CSS helpers shared by the shadow tree, the animation engine and the canvas. None of them |
| 2 | // is a CSS parser: they read the narrow, already sanitised text the validator forwards. |
| 3 | #pragma once |
| 4 | |
| 5 | #include "display_list.h" |
| 6 | |
| 7 | #include <optional> |
| 8 | #include <string> |
| 9 | #include <string_view> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace twlite::css { |
| 13 | |
| 14 | std::string trim(std::string_view s); |
| 15 | std::string lower(std::string s); |
| 16 | bool iequals(std::string_view a, std::string_view b); |
| 17 | bool starts_with(std::string_view s, std::string_view prefix); |
| 18 | bool ends_with(std::string_view s, std::string_view suffix); |
| 19 | /// A finite double from the whole of `s`, or nullopt. |
| 20 | std::optional<double> number(std::string_view s); |
| 21 | |
| 22 | /// Splits at `separator` outside parentheses and quotes (`a, rgb(1,2,3)` is two parts). |
| 23 | std::vector<std::string> split_top_level(std::string_view text, char separator); |
| 24 | |
| 25 | struct Declaration { |
| 26 | std::string name, value; |
| 27 | bool important = false; |
| 28 | }; |
| 29 | /// An inline `style` text as declarations; invalid names are dropped, a later duplicate wins. |
| 30 | std::vector<Declaration> parse_declarations(std::string_view text); |
| 31 | |
| 32 | /// What may be written into the HTML the renderer generates. The validator already guarantees |
| 33 | /// these; a name holding `;`, `"` or `>` would otherwise break out of its attribute or declaration. |
| 34 | bool is_tag_name(std::string_view name); |
| 35 | bool is_attribute_name(std::string_view name); |
| 36 | bool is_property_name(std::string_view name); |
| 37 | |
| 38 | /// Replaces every `light-dark(a, b)` with the side the theme picks: litehtml knows no such function. |
| 39 | std::string resolve_light_dark(std::string_view css, bool dark); |
| 40 | |
| 41 | /// `1.5s`, `200ms` or a `calc()` of times, in seconds. |
| 42 | std::optional<double> parse_time(std::string_view text); |
| 43 | |
| 44 | /// A CSS colour: hex, rgb[a](), hsl[a](), a named colour or `transparent`. |
| 45 | bool parse_color(std::string_view text, Color& out); |
| 46 | /// Rewrites every colour in `text` as `rgba(r, g, b, a)`, so two values interpolate number by number. |
| 47 | std::string normalize_colors(std::string_view text); |
| 48 | |
| 49 | /// Shortest decimal form: integers without a point, at most `digits` fractional digits. |
| 50 | std::string format_number(double value, int digits = 4); |
| 51 | |
| 52 | /// The `font` shorthand as the canvas and `measureText` read it. |
| 53 | struct Font { |
| 54 | std::string family = "sans-serif"; |
| 55 | float size = 10; |
| 56 | int weight = 400; |
| 57 | bool italic = false; |
| 58 | }; |
| 59 | Font parse_font(std::string_view font); |
| 60 | |
| 61 | /// A `transform` list as one matrix for a box of `w` × `h` (percentages), about the box's top left. |
| 62 | /// Only 2D functions in px, % and angle units; nullopt for anything else (3D, em, var(), calc()). |
| 63 | std::optional<Affine> parse_transform(std::string_view text, float w, float h); |
| 64 | /// The individual `translate`, `rotate` and `scale` properties, likewise. |
| 65 | std::optional<Affine> parse_translate(std::string_view text, float w, float h); |
| 66 | std::optional<Affine> parse_rotate(std::string_view text); |
| 67 | std::optional<Affine> parse_scale(std::string_view text); |
| 68 | /// `transform-origin` within a `w` × `h` box; nullopt when it is not 2D px, % or keywords. |
| 69 | std::optional<std::pair<float, float>> parse_origin(std::string_view text, float w, float h); |
| 70 | /// `opacity`: a number or a percentage, clamped to 0..1. |
| 71 | std::optional<float> parse_opacity(std::string_view text); |
| 72 | |
| 73 | } // namespace twlite::css |
| 74 | |