w3c/css-houdini-drafts

[css-typed-om] Something about !important

Opened this issue · 2 comments

I was just reminded today that we somehow forgot to handle !important at all in TypedOM. You can't tell whether a declaration is !important with TypedOM APIs, or set/unset the flag.

We should do something about that.

Oh, this is a tricky one. Made a little trickier because we're adding it in after much of the API is stable.

Important is a characteristic of a declaration, not a value. But, neither old CSSOM nor Typed OM have an object that represents an individual declaration, only a declaration block (CSSStyleDeclaration and StylePropertyMap).

CSSStyleDeclaration has a separate getter for retrieving the priority versus the value. So, easiest solution for retrieving the flag would be to mimic that and add a getPriority(property) method on StylePropertyMapReadOnly.

We can't just copy existing CSSOM approach for setting important flags, though. That's done through a third parameter to CSSStyleDeclaration.setProperty(). But the TypedOM equivalent, StylePropertyMap.set() is defined to coalesce all subsequent parameters into a values array.

I'd lean towards having a separate setPriority(priorityFlag) method. It's not great that it is inconsistent with base CSSOM, but it would be more consistent internally to have matching getter & setters. And it makes it possible to toggle the important state without re-setting the value. Next questions:

  • Should we continue to require a literal "!important" string as the parameter? That's kind of annoying for devs, but leaves the API open to other !something flags getting added to CSS in the future.

  • On the other hand, if other flags may be added in the future, is "priority" really the best name for the flag(s)?

  • What should happen if you set a priority value for a property that doesn't yet exist in the style map? Options: ignore it silently, throw, or generate a new declaration with the value set to unset.

Yeah, you summed up my thoughts pretty much exactly.

My plan is indeed to go with a get/set method pair that adjusts the importance.

There is a question of whether we want to allow for the possibility of other bang-structures in the future. There's no plans for this yet, but I've considered it in the past; on the other hand, we can just not worry about that until it happens, and make this API easy to use for !important.

Given that we have given serious consideration to a !default priority in the past, we should make the method open-ended, not boolean. So probably:

style.getPropertyPriority("foo") // "important" or ""
style.setPropertyPriority("foo", "important"); (or "")

What should happen if you set a priority value for a property that doesn't yet exist in the style map? Options: ignore it silently, throw, or generate a new declaration with the value set to unset.

I don't really want to set up a fake value for the property. I'm inclined to silently ignore.