1

What I want to do is write a selector that matches an arbitrary value in one place, then later requires a different value be equal to it. If [attr="value"] parsed "value" as a regex, then this would solve my problem:

*[class="(.+)"] *[class="if_\1"] {/* styles */}

Obviously I could just list each possible class individually, but that takes a great deal of convenience out of it.

Is this possible?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
twhb
  • 4,294
  • 2
  • 20
  • 23
  • Is your question about CSS or about Javascript? The title says CSS, but the only tag is for Javascript. – Boaz May 11 '14 at 13:20
  • Maybe you should redesign your code to not require this kind of magic. – John Dvorak May 11 '14 at 13:29
  • @Boaz my mistake, tags are now fixed. – twhb May 11 '14 at 15:01
  • @JanDvorak I already did. I just wanted to see if anybody else can make this approach work, because it would be a terser approach, and doubtlessly useful elsewhere. – twhb May 11 '14 at 15:04
  • _“because it would be a terser approach, and doubtlessly useful elsewhere”_ – it would first and foremost require browsers to do _a lot_ more work then they are doing already to see which elements match a selector … and is therefor totally _not_ practical. – CBroe May 12 '14 at 02:44

1 Answers1

1

No, it's not possible. Attribute selectors are almost completely static and provide almost no dynamic matching functionality (beyond that of substring matches, which are still static and not dynamic pattern-based). They do not support anything like what you see in everyday regular expressions.

A stylesheet preprocessor such as Sass or LESS will allow you to generate the static CSS rules needed, but all that does is automate the manual task of listing all possible values individually, which proves my first point.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • I don't think Sass, LESS, or any CSS-only preprocessor could help with this, not without a list of possible values, because you have to end up with plain old CSS. – twhb May 11 '14 at 15:16
  • @twberger in every approach I can think of, you need to have this list or build it as the first step. – John Dvorak May 11 '14 at 15:17
  • @twberger: That's the whole point, but I felt compelled to add a separate paragraph about preprocessors lest someone else go on about it like I wasn't aware they existed. – BoltClock May 11 '14 at 15:18
  • @JanDvorak same. I'm hoping one of us thinks of something different. – twhb May 11 '14 at 15:29