Add css :lang selector

feature/css-lang-var
Nimetu 4 years ago
parent 86f797cdbd
commit 8b43e65b95

@ -96,6 +96,8 @@ namespace NLGUI
// match An+B rule to child index (1 based)
bool matchNth(sint childNr, sint a, sint b) const;
// match :lang(xx)
bool matchLang(const CHtmlElement &elm, const std::string &pseudo) const;
// parse nth-child string to 'a' and 'b' components
// :nth-child(odd)

@ -235,6 +235,11 @@ namespace NLGUI
// 1st child should be '1' and not '0'
if (!matchNth(elm.childIndex+1, a, b)) return false;
}
else if (startsWith(PseudoClass[i], "lang("))
{
std::string lang = PseudoClass[i].substr(5, PseudoClass[i].size() - 6);
if (lang.empty() || !matchLang(elm, lang)) return false;
}
else
{
return false;
@ -324,6 +329,29 @@ namespace NLGUI
}
}
bool CCssSelector::matchLang(const CHtmlElement &elm, const std::string &pseudo) const
{
// TODO: does not support comma separated, or escaped/quoted/wildcard tags
std::string lang = toLower(elm.getInheritedLanguage());
if (lang.empty() || pseudo.empty())
return false;
// lang = 'en', pseudo = 'en-US'
if (lang.size() < pseudo.size())
return false;
std::string selector = toLower(pseudo);
bool selectorHasRegion = selector.find("-") != std::string::npos;
bool langHasRegion = lang.find("-") != std::string::npos;
// both are 'en', or 'en-US' type
if (langHasRegion == selectorHasRegion)
return lang == selector;
// lang = 'en-US', selector = 'en'
return lang[selector.size()] == '-' && startsWith(lang, selector);
}
std::string CCssSelector::toString() const
{
std::string ret;

Loading…
Cancel
Save