* \brief Parses HTML attribute strings into a name-value map. * * Extracts HTML attributes from a string (e.g., "class=\"button\" id=\"submit\"") * into individual name-value pairs. Expects attributes in the format name="value". * Skips whitespace and stops at the end of the string. * * \param to_parse The attribute string to parse. * \return A map of attribute names to their values. */
| 12 | * \return A map of attribute names to their values. |
| 13 | */ |
| 14 | std::map<std::string, std::string> parse_attribute(const std::string &to_parse) |
| 15 | { |
| 16 | std::map<std::string, std::string> attrs; |
| 17 | size_t pos = 0; |
| 18 | while (pos < to_parse.size()) |
| 19 | { |
| 20 | skip_space(pos, to_parse); |
| 21 | |
| 22 | size_t equal_sign_pos = to_parse.find("=", pos); |
| 23 | |
| 24 | std::string attribute_name = to_parse.substr(pos, equal_sign_pos - pos); |
| 25 | |
| 26 | pos = equal_sign_pos + 1; |
| 27 | |
| 28 | skip_space(pos, to_parse); |
| 29 | |
| 30 | char quote = to_parse[pos]; |
| 31 | pos++; |
| 32 | size_t closing_quote_pos = to_parse.find(quote, pos); |
| 33 | std::string attribute_value = to_parse.substr(pos, closing_quote_pos - pos); |
| 34 | pos = closing_quote_pos + 1; |
| 35 | |
| 36 | if (!attribute_value.empty()) |
| 37 | { |
| 38 | attrs[attribute_name] = attribute_value; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return attrs; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * \brief Tokenizes HTML source code into a sequence of tokens. |
no test coverage detected