Fix css rule declaration splitting

develop
Nimetu 3 years ago
parent 2e816ddf6e
commit 487f9f060e

@ -41,25 +41,57 @@ namespace NLGUI
TStyleVec CCssParser::parseDecls(const std::string &styleString) TStyleVec CCssParser::parseDecls(const std::string &styleString)
{ {
TStyleVec styles; TStyleVec styles;
std::vector<std::string> elements; size_t pos = 0;
NLMISC::splitString(styleString, ";", elements); size_t end = styleString.size();
while(pos < end)
for(uint i = 0; i < elements.size(); ++i)
{ {
std::string::size_type pos; size_t sep = styleString.find(':', pos);
pos = elements[i].find_first_of(':'); if (sep == std::string::npos)
if (pos != std::string::npos) break;
size_t keyIndex = pos;
size_t keyLength = sep - pos;
sep++;
pos = sep;
while(sep < end)
{ {
// css properties are case-insensitive, but sep = styleString.find_first_of(";'\"(", sep);
// custom properties (--name; ...;) are case sensitive if (sep == std::string::npos || styleString[sep] == ';')
std::string key = trim(elements[i].substr(0, pos)); break;
if (key.size() < 2 || (key[0] != '-' && key[1] != '-'))
key = toLowerAscii(key); if (styleString[sep] == '\'' || styleString[sep] == '"')
std::string value = trim(elements[i].substr(pos+1)); {
styles.push_back(TStylePair(key, value)); char ch = styleString[sep];
// skip open quote
sep++;
while(sep < end && styleString[sep] != ch)
{
if (styleString[sep] == '\\')
sep++;
sep++;
}
// skip close quote
sep++;
}
else if (styleString[sep] == '(')
{
while(sep < end && styleString[sep] != ')')
{
sep++;
}
// skip close parenthesis
sep++;
}
} }
}
styles.push_back(TStylePair(trim(styleString.substr(keyIndex, keyLength)), trim(styleString.substr(pos, sep - pos))));
if (sep >= end)
break;
pos = sep + 1;
}
return styles; return styles;
} }

Loading…
Cancel
Save