Could CSSOM provide constructors and factory methods?

The CSSOM seems to have a preponderance of parsing for creating objects. For
example, to create a MediaList, I bounce a string off a stylesheet:

s.media = 'print';
s.media  // MediaList appears!

What do you think about separating parsing, creation, and setting? For
example, applied to rules:

Specific CSS*Rule subtypes have Constructor(…) metadata.
insertRule is overloaded to accept CSSRule instances as well as DOMString.
Static CSSRule.parse(DOMString) can parse strings to produce CSSRules, so
you don’t have to abuse setters to do it.

Since there is not a detailed hierarchy for CSS values, maybe factory
methods instead of constructors make sense, so:

Static CSSPrimitiveValue.parse(DOMString)
Static CSSPrimitiveValue.px(unsigned int)
etc.

Having specific constructors and methods seems better than relying on
strings alone, because it is easier to find syntax errors (because mistyping
these result in JavaScript errors, which have stacktraces and tools support)
and it is easier to discern what the possible uses of a value are, whereas a
string could be anything.

Dominic

Received on Wednesday, 10 August 2011 02:58:56 UTC