* \brief Parses inline CSS style attributes into a property-value map. * * Parses a CSS style string (e.g., "color: red; font-size: 14px") into individual * property-value pairs. Handles parentheses in values (e.g., rgb() functions), * quoted strings, and case normalization. Trims whitespace from values. * * \param style_string The inline style attribute value to parse. * \return An unorder
| 16 | * \return An unordered_map of CSS property names to their values. |
| 17 | */ |
| 18 | std::unordered_map<std::string, std::string> parse_inline_style(std::string_view style_string) |
| 19 | { |
| 20 | std::unordered_map<std::string, std::string> result; |
| 21 | if (style_string.empty()) |
| 22 | return result; |
| 23 | std::string name, value; |
| 24 | bool is_value = false; |
| 25 | int paren_depth = 0; |
| 26 | bool is_in_quote = false; |
| 27 | size_t pos = 0; |
| 28 | |
| 29 | while (pos < style_string.size()) |
| 30 | { |
| 31 | char current = style_string[pos]; |
| 32 | |
| 33 | if (current == ':' && !is_value) |
| 34 | { |
| 35 | is_value = true; |
| 36 | } |
| 37 | else if (current == ';' && paren_depth == 0 && !is_in_quote) |
| 38 | { |
| 39 | if (!name.empty() && !value.empty()) |
| 40 | { |
| 41 | result[name] = trim_copy(value); |
| 42 | } |
| 43 | name.clear(); |
| 44 | value.clear(); |
| 45 | is_value = false; |
| 46 | } |
| 47 | else if (std::isspace(current) && !is_value) |
| 48 | { |
| 49 | ++pos; |
| 50 | continue; |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | if (!is_value) |
| 55 | { |
| 56 | name += std::tolower(static_cast<unsigned char>(current)); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | if (current == '(') |
| 61 | { |
| 62 | ++paren_depth; |
| 63 | } |
| 64 | |
| 65 | else if (current == ')') |
| 66 | { |
| 67 | --paren_depth; |
| 68 | } |
| 69 | |
| 70 | if (current == '"' || current == '\'') |
| 71 | { |
| 72 | is_in_quote = !is_in_quote; |
| 73 | } |
| 74 | value += paren_depth > 0 || is_in_quote ? current : std::tolower(static_cast<unsigned char>(current)); |
| 75 | } |