Not per se a TW question, but it is TW causing this difficulty with the targeting here:
As you know, paragraphs in the tiddler body automatically get wrapped in <p>. Thus, in an arbitrary text I get an arbitrary number of p’s. In my case each paragraph can in turn contain an arbitrary number of <span>s.
Question: How do I target the nth span in this?
Here’s a copy-pasteable tiddler text for easy try out. As you see, it half-works so the variables etc do work but the problem is the selector:
Lorem ipsum dolor sit amet, <span>ONE</span> consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
Pragrafus sin dividum.
Ut enim ad <span>TWO</span> minim veniam, quis nostrud
exercitation ullamco <span>THREE</span> laboris nisi ut aliquip
ex ea commodo consequat.
<style>
<$list filter="red blue green" counter=counter variable=color>
[data-tiddler-title="<<currentTiddler>>"] .tc-tiddler-body span:nth-of-type(<<counter>>) { background:<<color>>; }
</list>
</style>
Yeah, thanks, but this question is a bit “beyond” that. I know how to use :nth-of-type but my difficulty here is the preceding selector, i.e either of these:
This works too. The first line has all linebreaks removed
<style><$list filter="red blue green" counter=counter variable=color>[data-tiddler-title=<<currentTiddler>>] .tc-tiddler-body span:nth-of-type(<<counter>>) { background:<<color>>; }</$list></style>
Lorem ipsum dolor sit amet, <span>ONE</span> consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
Pragrafus sin dividum.
Ut enim ad <span>TWO</span> minim veniam, quis nostrud
exercitation ullamco <span>THREE</span> laboris nisi ut aliquip
ex ea commodo consequat.
That’s closer but it still doesn’t target how I want. The idea is to apply different styles to all the spans, i.e the result should show all three colors not:
You’ll probably read through: :has() - CSS: Cascading Style Sheets | MDN which isn’t supported by all browsers yet. But :has, :is and :where are very new pseudo selectors which may make that possible. … I don’t know
Thanks for looking into it! I am actually just looking at :has but did not think of :is and :where. But, yeah, I’m not sure it is possible. I’ll fiddle on
Have you considered simply adding a named parent? eg wrap the whole text field <section class="sectionclass"> body </section> now you have a parent under which other html tags can either inherit or use the sectionclass to help with specificity.
I am most likely out of my depth here, but I have a suspicion I may be correct.
If this works it could be added into the view body template, and or use an existing class found in the view body template to get the specificity.
The nth-... selector doesn’t work because the elements (1st and 2nd) aren’t siblings, they are “cousins”.
I think you will need to identify/differenciate the span elements, it can be manually or by via automatizated.
You could automatizate it with a macro that creates your identified span elements, but then you will have to solve its related issues.
Automatic: A more complicated macro that used something as counter that would be used to change the class. <<span "Lorem">> ipsum <<span "2nd text">> → <span class="class_1">Lorem</span> ipsum <span class="class_2">2nd text</span>