Add css :lang selector

feature/develop-atys
Nimetu 4 years ago
parent 92eb1dcdc7
commit 95cacf75a6

@ -96,6 +96,8 @@ namespace NLGUI
// match An+B rule to child index (1 based) // match An+B rule to child index (1 based)
bool matchNth(sint childNr, sint a, sint b) const; 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 // parse nth-child string to 'a' and 'b' components
// :nth-child(odd) // :nth-child(odd)

@ -235,6 +235,11 @@ namespace NLGUI
// 1st child should be '1' and not '0' // 1st child should be '1' and not '0'
if (!matchNth(elm.childIndex+1, a, b)) return false; 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 else
{ {
return false; 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 = toLowerAscii(elm.getInheritedLanguage());
if (lang.empty() || pseudo.empty())
return false;
// lang = 'en', pseudo = 'en-US'
if (lang.size() < pseudo.size())
return false;
std::string selector = toLowerAscii(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 CCssSelector::toString() const
{ {
std::string ret; std::string ret;

Loading…
Cancel
Save