Another easy win.

This commit is contained in:
2025-04-09 04:10:04 -04:00
parent 228da913c4
commit 36d04ad1c4
2 changed files with 14 additions and 18 deletions

View File

@ -232,14 +232,9 @@ void CssSelector::print () {
CssSimpleSelector::CssSimpleSelector () {
element = ELEMENT_ANY;
id = NULL;
pseudo = NULL;
}
CssSimpleSelector::~CssSimpleSelector () {
dFree (id);
dFree (pseudo);
}
CssSimpleSelector::~CssSimpleSelector ()= default;
void CssSimpleSelector::setSelect (SelectType t, const char *v) {
switch (t) {
@ -247,12 +242,12 @@ void CssSimpleSelector::setSelect (SelectType t, const char *v) {
klass.push_back(v);
break;
case SELECT_PSEUDO_CLASS:
if (pseudo == NULL)
pseudo = dStrdup (v);
if (not pseudo.has_value())
pseudo = v;
break;
case SELECT_ID:
if (id == NULL)
id = dStrdup (v);
if (not id.has_value())
id = v;
break;
default:
break;
@ -267,10 +262,10 @@ bool CssSimpleSelector::match (const DoctreeNode *n) {
assert (n);
if (element != ELEMENT_ANY && element != n->element)
return false;
if (pseudo != NULL &&
(n->pseudo == NULL || dStrAsciiCasecmp (pseudo, n->pseudo) != 0))
if (pseudo.has_value() &&
(n->pseudo == NULL || dStrAsciiCasecmp (pseudo.value().c_str(), n->pseudo) != 0))
return false;
if (id != NULL && (n->id == NULL || dStrAsciiCasecmp (id, n->id) != 0))
if (id.has_value() && (n->id == NULL || dStrAsciiCasecmp (id.value().c_str(), n->id) != 0))
return false;
for (int i = 0; i < klass.size (); i++) {
bool found = false;
@ -310,7 +305,7 @@ int CssSimpleSelector::specificity () {
void CssSimpleSelector::print () {
fprintf (stderr, "Element %d, pseudo %s, id %s ",
element, pseudo, id);
element, pseudo.value().c_str(), id.value().c_str());
fprintf (stderr, "class ");
for (int i = 0; i < klass.size (); i++)
fprintf (stderr, ".%s", klass.at (i).c_str());
@ -369,7 +364,7 @@ void CssStyleSheet::addRule (std::shared_ptr< CssRule > rule) {
std::string string;
if (top->getId ()) {
string = top->getId ();
string = top->getId ().value();
if (not idTable.contains(string)) {
ruleList= std::make_shared< RuleList > ();
idTable.emplace (string, ruleList);

View File

@ -363,7 +363,8 @@ class CssPropertyList : public lout::misc::SimpleVector <CssProperty> {
class CssSimpleSelector {
private:
int element;
char *pseudo, *id;
std::optional< std::string > pseudo;
std::optional< std::string > id;
std::vector< std::string > klass;
public:
@ -384,8 +385,8 @@ class CssSimpleSelector {
inline void setElement (int e) { element = e; };
void setSelect (SelectType t, const char *v);
inline std::vector< std::string > &getClass () { return klass; };
inline const char *getPseudoClass () { return pseudo; };
inline const char *getId () { return id; };
inline const auto &getPseudoClass () { return pseudo; };
inline const auto &getId () { return id; };
inline int getElement () { return element; };
bool match (const DoctreeNode *node);
int specificity ();