News for hackers. Stuff that matters.
Re:Everything that uses configuration files should report wh... by Shrezzing on Sunday June 25, 2023 @05:09AM (#36466267)
It's a little bit away from the domain of config files, but I think CSS is the most graceful example of handling this as a generalised problem. Style configurations cascade, and the last one read is the one that gets applied. There are overrides to that flow, but you have to very overtly add a piece of syntax to the overriding line like `!important`. Two overlapping overrides revert back to the "last read" approach. The approach is so fundamental to CSS that the entire language is named after it, so there's no ambiguity or forwards compatibility concerns in this regard whatsoever.
Re:Everything that uses configuration files should report wh... by masklinn on Sunday June 25, 2023 @05:20AM (#36466325)
You might want to read on specificity because it’s a huge spanner in your assumptions.
Re:Everything that uses configuration files should report wh... by lioeters on Sunday June 25, 2023 @11:33AM (#36469280)
> Specificity is the algorithm used by browsers to determine the CSS declaration that is the most relevant to an element, which in turn, determines the property value to apply to the element. The specificity algorithm calculates the weight of a CSS selector to determine which rule from competing CSS declarations gets applied to an element.

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity...

Re:Everything that uses configuration files should report wh... by lavela on Sunday June 25, 2023 @05:22AM (#36466332)
The rules of CSS specificity are a bit more involved than '!important' or not, and I frequently see beginners trip there, but apart from that I agree that the CSS rule set might be worth incorporating.
Re:Everything that uses configuration files should report wh... by yard2010 on Sunday June 25, 2023 @05:53AM (#36466499)
Fun trick to use if you use a css post processing (less, sass, etc.) is "&&&", it will resolve to a very specific rule which overwrites the other rules
Re:Everything that uses configuration files should report wh... by blowski on Sunday June 25, 2023 @06:50AM (#36466864)
I’m sure you know this, but for readers who don’t, it’s much more complex than “last defined wins with a few override options”. Based on “specificity”, rules based on the selector used.

Some examples:

1. #mydiv overrides .mydiv - IDs are more specific than classes

2. .outerdiv .innerdiv overrides .innerdiv - nested selectors are more specific

3. .mydiv overrides div - classes are more specific than elements

And a lot of other rules.

I can’t imagine I’d want this level of complexity in config files.

Re:Everything that uses configuration files should report wh... by masklinn on Sunday June 25, 2023 @07:36AM (#36467155)
Nesting is not actually more specific.

Rather specificity is a triple of (ids sub-selectors, class sub-selectors, element sub-selectors). When attributes conflict, the one whose rule has the more specific selector wins.

The specificities of the rules you wrote out are (1,0,0), (0,1,0), (0,2,0), (0,1,0), (0,1,0), and (0,0,1).

In-line attributes (@style) beat out out-out-of line ones except if they’re !important. Specificity also disambiguates between !important attributes.

Re:Everything that uses configuration files should report wh... by blowski on Sunday June 25, 2023 @07:57AM (#36467282)
Thank you for the clarification, I hadn't realised that.
Re:Everything that uses configuration files should report wh... by tobr on Sunday June 25, 2023 @08:29AM (#36467513)
> I can’t imagine I’d want this level of complexity in config files.

Lots of people don’t want it in their CSS either. It’s generally considered good practice to make everything the same specificity and avoid !important.

Re:Everything that uses configuration files should report wh... by lavela on Sunday June 25, 2023 @09:35AM (#36468047)
> avoid !important

Sure, haven't seen that in a while in well structured code bases.

> make everything the same specificity

How would you achieve same specificity on everything? Only use either ids or classes? No type selectors / pseudo-classes / pseudo-elements / multiple selectors? I can't imagine how that would be feasible and am not aware of anyone recommending this.

Re:Everything that uses configuration files should report wh... by Supermancho on Sunday June 25, 2023 @10:45AM (#36468692)
There are a large number of companies that require ids as the basis for every selector, by policy. This may not be interesting to you, but I think this is what they meant. The fact they may or may not be familiar to you is incidental.
Re:Everything that uses configuration files should report wh... by lavela on Sunday June 25, 2023 @12:06PM (#36469689)
I'm familiar with policies that generally strive for specificity of (0, 1, 0) like BEM or (1, 0, 0), but I'd be very interested in any policy that rules out pseudo-classes like `:hover`, as prohibiting them would leave you to handle any interactivity you'd get for free from CSS via JavaScript.
Re:Everything that uses configuration files should report wh... by blowski on Sunday June 25, 2023 @01:13PM (#36470464)
This is effectively how Tailwind works. You end up with classes like `hover:text-black`.
Re:Everything that uses configuration files should report wh... by afiori on Sunday June 25, 2023 @02:59PM (#36471471)
With :is and :where it is trivial to trick selectors

:is(selector_1, :where(selector_2))

Has always the specificity of selector_2, and if you choose an impossible selector for selector_1 (the easiest approach is to give multiple ids or use non existent classes/elements) it only select by selector_2

Re:Everything that uses configuration files should report wh... by oneeyedpigeon on Monday June 26, 2023 @03:55PM (#36484946)
That looks far more complex than just using specificity properly as intended.
Re:Everything that uses configuration files should report wh... by cerved on Monday June 26, 2023 @01:32AM (#36476052)
Perhaps you mean the same kind of specificity.

Specificity is at the heart of CSS, you're not supposed to design rules with equal specificity

Re:Everything that uses configuration files should report wh... by wildrhythms on Sunday June 25, 2023 @07:52AM (#36467254)
Not true at all. There is a very much non-intuitive weighting system applied to CSS selectors which is what ultimately determines which gets applied: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Re:Everything that uses configuration files should report wh... by cesarb on Sunday June 25, 2023 @10:12AM (#36468354)
> Two overlapping overrides revert back to the "last read" approach.

IIRC, there's an important (heh) exception to that: user stylesheets. Without the "!important" override on the user stylesheet, the site stylesheet wins; with the "!important" override on the user stylesheet, the user stylesheet wins, even if the site stylesheet also had an "!important" for that rule.

Re:Everything that uses configuration files should report wh... by cerved on Monday June 26, 2023 @01:35AM (#36476065)
The domain of style and configuration are very, very different. I'm not sure what works okay for displaying a website in different layouts on vastly different unknown devices share a lot of the same concerns of configuring programs on a machine