Skip to content

Better integration of getComputedStyle with PseudoElement #43095

Closed

Description

@Loirooriol

GetComputedStyle has some manual parsing for pseudo-elements:

// Step 3: If pseudoElt is provided, is not the empty string, and starts with a colon, then:
// Step 3.1: Parse pseudoElt as a <pseudo-element-selector>, and let type be the result.
// TODO(#43095): This is quite hacky and it would be better to have a parsing function that
// is integrated with stylo `PseudoElement` itself. Comparing with stylo, we are now currently
// missing `::backdrop`, `::color-swatch`, and `::details-content`.
let pseudo = pseudo.map(|mut s| {
s.make_ascii_lowercase();
s
});
let pseudo = match pseudo {
Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
Some(PseudoElement::Before)
},
Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
Some(PseudoElement::After)
},
Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
Some(ref pseudo) if pseudo == "::placeholder" => Some(PseudoElement::Placeholder),
Some(ref pseudo) if pseudo.starts_with(':') => {
// Step 3.2: If type is failure, or is a ::slotted() or ::part()
// pseudo-element, let obj be null.
is_null = true;
None
},
_ => None,
};

It would be better to have something like what Gecko does:

https://searchfox.org/firefox-main/rev/46fa95cd7f10222996ec267947ab94c5107b1475/servo/ports/geckolib/glue.rs#5195-5226

pub extern "C" fn Servo_ParsePseudoElement(
    data: &nsAString,
    ignore_enabled_state: bool,
    request: &mut structs::PseudoStyleRequest, /* output */
) -> bool {
    let string = data.to_string();
    let mut input = ParserInput::new(&string);
    let mut parser = Parser::new(&mut input);
    // This is unspecced, but we'd like to match other browsers' behavior, so we reject the
    // preceding whitespaces and trailing whitespaces.
    // FIXME: [Bug 1845712](https://bugzilla.mozilla.org/show_bug.cgi?id=1845712). Figure out if it is necessary to reject preceding and trailing
    // whitespaces.
    if parser.try_parse(|i| i.expect_whitespace()).is_ok() {
        return false;
    }
    let Ok(pseudo) = PseudoElement::parse_ignore_enabled_state(&mut parser) else {
        return false;
    };
    // The trailing tokens are not allowed, including whitespaces.
    if parser.next_including_whitespace().is_ok() {
        return false;
    }
    if !ignore_enabled_state && !pseudo.enabled_in_content() {
        return false;
    }
    let (pseudo_type, name) = pseudo.pseudo_type_and_argument();
    let name_ptr = name.map_or(std::ptr::null_mut(), |name| name.as_ptr());
    request.mType = pseudo_type;
    request.mIdentifier = unsafe { RefPtr::new(name_ptr).forget() };

    true
}

Activity

  1. added
    A-content/script
    on Mar 8, 2026
  2. added a commit that references this issue on Nov 11, 2026
    c685d98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/script

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions