[selectors] Anonymous Attribute Selector

Let’s assume I have a document language with usual named element types, but without predefined attributes. The only attribute for each element type is anonymous and its value can be any string or digit or, possibly, any other type:

  <element attribute>
  <element true>
  <element 123>
  <element 12px>
  <element 100%>

If HTML/SGML faces the first case, a boolean attribute is created, i.e. it’s defined as a shorthand of

  <element attribute="attribute">

which enables Selectors

  element[attribute]
  element[attribute="attribute"]

In SGML, the attribute name and its value keyword wouldn’t even have to match, but the other cases cannot be modeled and therefore are not possible in HTML and XML, maybe they are in SGML with some DTD trickery. 

Anyway, my (actually not so hypothetic) language isn’t *ML-based. There currently is no way in Selectors to match such anonymous attributes, but there’s kind of a way to match anonymous elements with the universal selector which is optional in ID, class, pseudo-class, pseudo-element and attribute selectors. 

  *
  *#id              =   #id
  *.class           =   .class
  *:pseudo-class    =   :pseudo-class
  *::pseudo-element =   ::pseudo-element
  *[attribute]      =   [attribute]

Therefore, I’d like to propose an anonymous attribute selector. Since ‘*’ is already used in the “contains” attribute selector ‘*=’, I suggest the at-sign ‘@’, which is often used to symbolize attributes anyway.

  [@*="a"]   {}
  [@="true"] {}
  [@^=1]     {}
  [@$="px"]  {}
  [@$="%"]   {}

The ‘attr()’ pseudo-function would of course also have to accept that symbol:

  [@*="a"]::before {content: attr(@);}
  [@$="px"] {width: attr(@ length);}

That’s also the reason why the attribute cannot just be left out:

  [*="a"]   {}
  [="true"] {}
  [^=1]     {}
  [$="px"]  {}
  [$="%"]   {}

  [*="a"]::before {content: attr();} /* might still work */
  [$="px"] {width: attr(length);}    /* fails */

In languages that do have named attributes, any attribute would match that selector, probably. This may make it useful for HTML, but probably not all that often.

 PS: Attribute selectors for detailed numeric matching, like ‘<=’, ‘>=’ or ‘!=’,
     have been proposed earlier, but sadly haven‘t made it into the ED yet.

Received on Friday, 30 May 2014 09:33:59 UTC