v160 · javascript · cssom

Round-trip into string APIs

The practical reason selectorText exists. The rest of the platform names pseudo-elements by string: getComputedStyle(el, "::x"), el.animate(kf, {pseudoElement: "::x"}), el.pseudo("::x"). A reusable helper that is handed only a CSSPseudoElement object (from pseudoTarget, from a framework, from a previous lookup) needs a string that selects exactly that pseudo-element. Run the same helper with selectorText and with the pre-160 workaround, type, and compare.

Probing this browser…

The page

Inbox
  • Ship the release
Hand the helper this CSSPseudoElement
helper(pseudo) results — key = pseudo.selectorText vs pseudo.type
keygetComputedStyleanimate()pseudo(key) === p
not run yet
Pick a pseudo-element and run the helper.

the helper

function pulse(pseudo) {
  const key = pseudo.selectorText;                 // Chrome 160+
  const from = getComputedStyle(pseudo.element, key).opacity;
  pseudo.element.animate([{ opacity: from }, { opacity: 0.3 }, { opacity: from }],
                         { duration: 400, pseudoElement: key });
}

// Pre-160 workaround: pseudo.type — fine for "::before", but
// getComputedStyle(html, "::view-transition-group") → empty declaration
// html.animate(kf, { pseudoElement: "::view-transition-group" }) → SyntaxError

Verified on Chromium 153 in this container: with key "::view-transition-group", animate() throws SyntaxError: ::view-transition-group is a syntactically invalid pseudo-element and getComputedStyle returns empty values, while the full "::view-transition-group(panel)" works during a transition.

see also