-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-pseudo-5] ::text / ::text-node pseudoelement #2208
Comments
I’ve also been thinking about pseudo-elements for anonymous boxes, first to the WICG to be eventually moved here. I talked about it at bit at #2084 (comment) – and I’ve have been writing a strawman when I can. I could find barely any prior mention of this idea before, when I searched on the web and in the old mailing lists.
My own biggest use case would be uniformly styling the boxes of paragraph text in I don’t know if I should keep working on my WICG strawman if there’s going to be discussion here. I would be happy with any discussion: I’m certainly still surprised that there’s been barely any discussion about this idea over the past thirty years. |
I'm definitely supportive of the idea in general. A more detailed proposal with examples would probably be helpful in gaining more supporters, so if you're already working on something @js-choi, please don't stop just because this has now been formally raised as a working group issues. Some things to think of:
Which are all probably good prompts for a WICG discussion if you want to start one. |
|
It's unclear about what most of the CSS properties would mean on a text-node, though, right? Specially non-inheriting ones. |
In my opinion all styles including absolute positioning. But ::text::before forbidden. |
What does an absolutely positioned text-node mean? Can you set |
There's two ways to go about this. One is to style the text node itself - only a subset of the CSS properties would apply, those that work on text. (More or less just the properties that are inherited, but there are some inherited properties that wouldn't apply.) We'd have to define the precedence of this vs ::first-line and ::first-letter. The second is to create an anonymous box around the text node and style that. In this case all of CSS applies. There is a minor problem here, tho - one of the use-cases for this is to solve issue 2 in the Scoping spec, and it's not totally clear where in the flat tree the anonymous box would be. |
@tabatkins What about selecting anonymous inline blocks? They’re not exactly the same as text nodes (especially regarding positioning), but they overlap quite a bit. My impression had been that those are already being generated by current agents and thus would not necessitate creating new boxes. |
Works it like ::after and ::before.
will be shown identically like:
I think it is the easiest solution.
The text is removed and not shown.
Need clarification and any idea. It can be by exception treated as display:inline
Yes, in my opinion the anonymous inline box will be the solution. Only exception to styles that cannot be applied like display:contents. Please note, if the element |
I have desired something like this several times when I have Always wrapping text runs inside new boxes may be a breaking change, e.g. boxes inside a flex container become flex items, so this would prevent sequences of text runs to be wrapped inside a single anonymous flex item. CSS Flexbox could discriminate boxes generated by It has also been suggested to inject the styles into anonymous boxes, but I think this is a bad idea because the cascade is supposed to happen in the element tree, and I prefer to avoid another fiasco like I think what makes more sense would be:
|
What is formal initial BTW |
@Nadya678 |
OK. Thus here one exception: |
I wasn't aware of this ticket existing when I wrote this: #2406 (comment) The basic take-away is that I don't think a text node selector should be a pseudo element prefixed with colon(s) - that creates all kind of problems with specificity imho. Instead it should be like an invisible p-tag with slightly less specificity than tags that auto opens on text and auto closes before any new block context / when the parent element closes. |
I believe some of the above thinking too tightly tied to an artifact in the DOM (that never results from parsing) which is the possibility of having empty or adjacent text nodes. Let's have CSS, if in a DOM-based implementation of the host language, pretend that |
@ByteEater-pl It could be tied to the concept of text run:
|
A pseudo element like HTML: <p class="message">
Hi there
<span class="emoji-native" id="1">😋</span>
this is cool!
<span class="emoji-native" id="2">😍</span>
<span class="emoji-native" id="3">😍</span>
<p> CSS: .emoji-native {
margin: 0 3px;
}
.emoji-native + .emoji-native {
margin-left: 0px;
} Basically in this example I want to consider To be honest I don't see a syntax that would make this work, this feels out of context. I would vouch though for a new kind of element that would represent displaced text written directly into the source code, or even a simple "invisible" |
@mauriciogior No, adding <p class="message"> <!-- p -->
Hi there <!-- p::text -->
<span class="emoji-native" id="1"> <!-- p > :nth-child(1) -->
😋 <!-- p > :nth-child(1)::text -->
</span>
this is cool! <!-- p::text -->
<span class="emoji-native" id="2"> <!-- p > :nth-child(2) -->
😍 <!-- p > :nth-child(2)::text -->
</span>
<span class="emoji-native" id="3"> <!-- p > :nth-child(3) -->
😍 <!-- p > :nth-child(3)::text -->
</span>
</p> If what you want is selecting a |
This selector would have been useful to me when targeting slotted text nodes, created either from implicit I would also like to point out some prior art. At the time of this writing, other fully or partially shipping CSS text selectors or text-capable selectors include As a related aside, |
This is an interesting idea, especially since the closing tag for @mauriciogior mentions a new Another downside of an implied element is that it would get confusing when using positional pseudo-classes, like |
I think this selector will enable pretty cool features in combination with .button {
display: flex;
height: 40px;
padding: 8px 16px;
column-gap: 8px;
align-items: center;
}
.button:has(svg):not(:has(::text)) {
width: 40px;
padding: 0;
justify-content: center;
} This will make one class for regular buttons with icon and text and for square buttons if it contains only icon. |
While that may be possible, pseudo-elements are currently invalid inside Also note that Also note that <button>
<svg>...</svg>
</button> |
Imo, in case of button On the topic of whitespaces: I think |
That comment refers to empty text nodes like |
Is somehing like |
Seems possible as long as the |
I also want to consider This will be useful while using the streaming mode calling the ChatGPT's completion api with an asynchronous function call. When ChatGPT is outputting content in Streaming mode, it usually involves text nodes. At this time, if it needs to draw an image, it will pause to execute a function call. During this, a Loading sign should be displayed in the UI. The simplest way to do this is to add a p .loading:has(+::text) {
display: none;
} |
Having
Currently, one must do tricks with |
Looks like |
@contentfree So with this proposal you would actually need |
content: none ought to work for this, right? But when we implemented it wasn't web compatible, we'd need a separate keyword |
That's why I qualified it with "as a real node". Jumping back to the comment at #2208 (comment) and its example: It seems like some mechanism (maybe not |
Correct, but you could still target the text node next to the icon via If we introduce a I also want to point out that this could be a potential performance footgun, because as @Loirooriol pointed out several times, Sebastian |
Just wanted to add another example where this would be useful. I'm pretty sure there's currently no (CSS-only) way to distinguish between these to only add right-margin to the
|
@contentfree That seems a different feature, maybe But anyways, |
Related on Stack Overflow: Is there a CSS selector for text nodes? |
https://drafts.csswg.org/selectors-4/
Please add
::text
or::text-node
selector to select all text nodes inside any elementfor example for
the
p::text
will select #text1 and #text3 and the pseudoelement can be styled like::after
and::before
(without need to set "content").The text was updated successfully, but these errors were encountered: