[css-houdini-drafts] [css-typed-om] How to deal with author-extensibility (#1038)

tabatkins has just created a new issue for https://github.com/w3c/css-houdini-drafts:

== [css-typed-om] How to deal with author-extensibility ==
Currently, the Typed OM is not meant to be author-extensible. The style-setting functions only accept Typed OM classes, and tho theoretically you could subclass one of them, there's nothing defined that would give you any special behavior for doing so - you'd just be treated as the class you derived from.

There are several obvious places where author-extensibility would be really cool, however - custom colorspaces are a big one. We obviously don't want to harm our ability to add new stuff, but there should be ways to interact with the Typed OM that are still safe.

My first thought is to define that the functions that consume style objects can check for the presence of a converter function, and if it's defined, call that to convert it to a "real" Typed OM object. Like:

```
class XYZColor : CSSColorValue {
 constructor(x,y,z,alpha=1) { ... }
 toTypedOM() {
  const [l,a,b] = labFromXyz(this);
  return new CSSLab(l,a,b,this.alpha);
 }
```

This could also allow for defining new units in author space:

```
class Km : CSSUnitValue {
 constructor(value) { 
  this.value = value; 
  Object.defineProperty(this, "unit", {value:"km", writable:false});
 }
 toTypedOM() {
  return CSS.cm(this.value*100_000);
 }
}
```

The class would turn into a "real" Typed OM value fairly eagerly - a new length unit, for example, needs to be turned into a real CSS value *immediately* upon setting, so we know what it's CSS type is and can do math on it properly. But it would at least allow for custom stuff to be constructed and manipulated before it's merged into a "real" css structure.

Please view or discuss this issue at https://github.com/w3c/css-houdini-drafts/issues/1038 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Thursday, 13 May 2021 15:27:58 UTC