servo/components/layout_2020/lists.rs
Martin Robinson b5c8164e99
layout: Simplify and generalize the usage of pseudo-elements (#36202)
- Remove the last remaining Servo-specific PseudoElement enum from
  layout. This was made to select `::before` and `::after` (both eager
  pseudo-elements), but now `traverse_pseudo_element` is called
  `traverse_eager_pseudo_element` and should work on any eager pseudo
  element.
- Expose a single way of getting psuedo-element variants of
  ThreadSafeLayoutElement in the Layout DOM, which returns `None` when
  the pseudo-element doesn't apply (not defined for eager
  pseudo-elements or when trying to get `<details>` related
  pseudo-elements on elements that they don't apply to).
- Ensure that NodeAndStyleInfo always refers to a node. This is done by
  making sure that anonymous boxes are all associated with their
  originating node.

These changes are prepatory work for implementation of the `::marker`
pseudo-element as well as ensuring that all anonymous boxes can be
cached into the box tree eventually.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-03-29 12:41:04 +00:00

100 lines
3.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use style::properties::longhands::list_style_type::computed_value::T as ListStyleType;
use style::properties::style_structs;
use style::values::computed::Image;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::{NodeAndStyleInfo, PseudoElementContentItem};
use crate::replaced::ReplacedContents;
/// <https://drafts.csswg.org/css-lists/#content-property>
pub(crate) fn make_marker<'dom, Node>(
context: &LayoutContext,
info: &NodeAndStyleInfo<Node>,
) -> Option<(NodeAndStyleInfo<Node>, Vec<PseudoElementContentItem>)>
where
Node: NodeExt<'dom>,
{
// TODO: use `PseudoElement::Marker` when we add it.
let marker_info = info.pseudo(
context,
style::selector_parser::PseudoElement::ServoLegacyText,
)?;
let style = &marker_info.style;
let list_style = style.get_list();
// https://drafts.csswg.org/css-lists/#marker-image
let marker_image = || match &list_style.list_style_image {
Image::Url(url) => Some(vec![
PseudoElementContentItem::Replaced(ReplacedContents::from_image_url(
marker_info.node,
context,
url,
)?),
PseudoElementContentItem::Text(" ".into()),
]),
// XXX: Non-None image types unimplemented.
Image::ImageSet(..) |
Image::Gradient(..) |
Image::CrossFade(..) |
Image::PaintWorklet(..) |
Image::None => None,
};
let content = marker_image().or_else(|| {
Some(vec![PseudoElementContentItem::Text(
marker_string(list_style)?.into(),
)])
})?;
Some((marker_info, content))
}
/// <https://drafts.csswg.org/css-lists/#marker-string>
fn marker_string(style: &style_structs::List) -> Option<&'static str> {
match style.list_style_type {
ListStyleType::None => None,
// TODO: Using non-breaking space here is a bit of a hack to give a bit of margin to outside
// markers, but really we should be setting `white-space: pre` on them instead.
// See https://github.com/w3c/csswg-drafts/issues/4891.
ListStyleType::Disc => Some("\u{00a0}"),
ListStyleType::Circle => Some("\u{00a0}"),
ListStyleType::Square => Some("\u{00a0}"),
ListStyleType::DisclosureOpen => Some("\u{00a0}"),
ListStyleType::DisclosureClosed => Some("\u{00a0}"),
ListStyleType::Decimal |
ListStyleType::LowerAlpha |
ListStyleType::UpperAlpha |
ListStyleType::ArabicIndic |
ListStyleType::Bengali |
ListStyleType::Cambodian |
ListStyleType::CjkDecimal |
ListStyleType::Devanagari |
ListStyleType::Gujarati |
ListStyleType::Gurmukhi |
ListStyleType::Kannada |
ListStyleType::Khmer |
ListStyleType::Lao |
ListStyleType::Malayalam |
ListStyleType::Mongolian |
ListStyleType::Myanmar |
ListStyleType::Oriya |
ListStyleType::Persian |
ListStyleType::Telugu |
ListStyleType::Thai |
ListStyleType::Tibetan |
ListStyleType::CjkEarthlyBranch |
ListStyleType::CjkHeavenlyStem |
ListStyleType::LowerGreek |
ListStyleType::Hiragana |
ListStyleType::HiraganaIroha |
ListStyleType::Katakana |
ListStyleType::KatakanaIroha => {
// TODO: Implement support for counters.
None
},
}
}