Skip to content
Draft
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions src/css/css_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,10 @@ fn parse_at_rule<P: AtRuleParser>(
Ok(v) => v,
Err(_) => break 'out,
};
if !matches!(*tok, Token::OpenCurly | Token::Semicolon) {
unreachable!("Should have consumed these delimiters");
}
debug_assert!(
matches!(*tok, Token::OpenCurly | Token::Semicolon),
"parse_until_before(SEMICOLON|CURLY) contract"
);
}
return Err(e);
}
Expand All @@ -438,7 +439,7 @@ fn parse_at_rule<P: AtRuleParser>(
Token::OpenCurly => parse_nested_block(input, |input2: &mut Parser| {
P::parse_block(parser, prelude, start, input2)
}),
_ => unreachable!(),
t => Err(input.new_unexpected_token_error(t)),
}
}

Expand Down Expand Up @@ -982,7 +983,6 @@ impl<'a> CustomAtRuleParser for BundlerAtRuleParser<'a> {

pub enum AtRulePrelude<T> {
FontFace,
FontFeatureValues,
FontPaletteValues(DashedIdent),
CounterStyle(CustomIdent),
Import {
Expand Down Expand Up @@ -1538,16 +1538,11 @@ mod rule_parsers {
) -> CssResult<(DeclarationBlock<'static>, CssRuleList<T::AtRule>)> {
// TODO: think about memory management in error cases
let mut rules = CssRuleList::<T::AtRule>::default();
let composes_state = if self.is_in_style_rule
&& matches!(self.composes_state, ComposesState::Allow(_))
{
let ComposesState::Allow(l) = self.composes_state else {
unreachable!()
};
ComposesState::DisallowNested(l)
} else {
// ComposesState is Copy.
self.composes_state
let composes_state = match self.composes_state {
ComposesState::Allow(l) if self.is_in_style_rule => {
ComposesState::DisallowNested(l)
}
other => other,
};
// SAFETY: see `TopLevelRuleParser::nested` — `'static` erasure of the
// parser arena.
Expand Down Expand Up @@ -2008,7 +2003,6 @@ mod rule_parsers {
}));
Ok(())
}
AtRulePrelude::FontFeatureValues => unreachable!(),
AtRulePrelude::Unknown { name, tokens } => {
this.rules.v.push(CssRule::Unknown(UnknownAtRule {
name,
Expand Down Expand Up @@ -3605,7 +3599,8 @@ impl<'a> Parser<'a> {
Err(_) => return Ok(values),
};
if !matches!(tok, Token::Comma) {
unreachable!();
debug_assert!(false, "parse_until_before(COMMA) contract");
return Ok(values);
}
}
}
Expand Down Expand Up @@ -4011,7 +4006,8 @@ impl<'a> Parser<'a> {
) {
Ok(())
} else {
unreachable!("Unexpected error encountered: {}", e.kind);
debug_assert!(false, "expect_exhausted: unexpected error {}", e.kind);
Ok(())
}
}
};
Expand Down Expand Up @@ -5718,14 +5714,6 @@ impl Token {
self.kind().to_string()
}

pub fn raw(&self) -> &[u8] {
match self {
Token::Ident(v) => v,
// .function => ...
_ => unreachable!(),
}
}

pub fn to_css_generic<W: WriteAll + ?Sized>(&self, writer: &mut W) -> bun_io::Result<()> {
match self {
Token::Ident(v) => serializer::serialize_identifier(v, writer),
Expand Down
14 changes: 0 additions & 14 deletions src/css/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,6 @@ impl VendorPrefix {
VendorPrefix::NONE,
];

// NOTE: bitflags 2.x already generates `from_name(&str) -> Option<Self>`;
// the panicking variant is exposed as `from_name_str`.
#[inline]
pub fn from_name_str(name: &str) -> VendorPrefix {
match name {
"none" => VendorPrefix::NONE,
"webkit" => VendorPrefix::WEBKIT,
"moz" => VendorPrefix::MOZ,
"ms" => VendorPrefix::MS,
"o" => VendorPrefix::O,
_ => unreachable!(),
}
}

/// Returns VendorPrefix::None if empty.
#[inline]
pub fn or_none(self) -> VendorPrefix {
Expand Down
16 changes: 7 additions & 9 deletions src/css/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ pub trait QueryCondition: Sized + ToCss {

/// Serialize a variant that isn't `Feature`/`Not`/`Operation`
/// (e.g. `ContainerCondition::Style`). Implementors with no extra
/// variants leave the default.
fn extra_to_css(&self, _dest: &mut Printer) -> core::result::Result<(), PrintErr> {
unreachable!("QueryCondition: no extra variants")
}
/// variants return `Ok(())`.
fn extra_to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr>;

/// Provided: shared `ToCss` body for the `Feature`/`Not`/`Operation`
/// lattice (+ `extra_to_css` fallback). Mirrors the three hand-rolled
Expand Down Expand Up @@ -826,6 +824,9 @@ impl QueryCondition for MediaCondition {
fn feature_to_css(f: &MediaFeature, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
f.to_css(dest)
}
fn extra_to_css(&self, _dest: &mut Printer) -> core::result::Result<(), PrintErr> {
Ok(())
}

fn parse_feature(input: &mut Parser) -> Result<Self> {
let feature = MediaFeature::parse(input)?;
Expand Down Expand Up @@ -1441,11 +1442,8 @@ impl MediaList {

match input.next() {
Ok(tok) => {
if !matches!(tok, css::Token::Comma) {
unreachable!(
"expected a comma after parsing a MediaQuery — bug in CSS parser"
);
}
debug_assert!(matches!(tok, css::Token::Comma));
let _ = tok;
}
Err(_) => break,
}
Expand Down
84 changes: 1 addition & 83 deletions src/css/properties/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate as css;
use crate::CSSString;
use crate::css_values::easing::EasingFunction;
use crate::css_values::ident::{CustomIdent, DashedIdent, is_reserved_custom_ident};
use crate::css_values::length::{LengthPercentage, LengthPercentageOrAuto};
use crate::css_values::length::LengthPercentage;
use crate::css_values::number::{CSSNumber, CSSNumberFns};
use crate::css_values::size::Size2D;
use crate::css_values::time::Time;
use crate::generics::CssEql;
use crate::{Parser, PrintErr, Printer, SmallList};
Expand Down Expand Up @@ -513,17 +512,9 @@ pub enum AnimationTimeline {
None,
/// A timeline referenced by name.
DashedIdent(DashedIdent),
/// The scroll() function.
Scroll(ScrollTimeline),
/// The view() function.
View(ViewTimeline),
}

impl AnimationTimeline {
// Void variants (`auto`, `none`) are tried first via ident match;
// payloads follow in declaration order. We stop at `DashedIdent`; if
// scroll()/view() ever become live they need real function-syntax
// parsing, not a derived field-sequence fallback.
pub fn parse(input: &mut Parser) -> css::Result<Self> {
let state = input.state();
if let Ok(ident) = input.expect_ident() {
Expand All @@ -545,12 +536,6 @@ impl AnimationTimeline {
AnimationTimeline::Auto => dest.write_str(b"auto"),
AnimationTimeline::None => dest.write_str(b"none"),
AnimationTimeline::DashedIdent(d) => d.to_css(dest),
// These variants are currently unconstructible via `parse()`, and
// emitting bare space-separated fields here would be wrong CSS
// (spec syntax is `scroll(...)` / `view(...)`).
AnimationTimeline::Scroll(_) | AnimationTimeline::View(_) => {
unreachable!("ScrollTimeline / ViewTimeline have no toCss in spec (uninstantiated)")
}
}
}

Expand All @@ -568,9 +553,6 @@ impl AnimationTimeline {
AnimationTimeline::None => AnimationTimeline::None,
// `DashedIdent` is a `Copy` arena-slice pointer.
AnimationTimeline::DashedIdent(d) => AnimationTimeline::DashedIdent(*d),
AnimationTimeline::Scroll(_) | AnimationTimeline::View(_) => {
unreachable!("ScrollTimeline / ViewTimeline are uninstantiated (no parse path)")
}
}
}
}
Expand All @@ -584,75 +566,11 @@ impl PartialEq for AnimationTimeline {
(AnimationTimeline::Auto, AnimationTimeline::Auto) => true,
(AnimationTimeline::None, AnimationTimeline::None) => true,
(AnimationTimeline::DashedIdent(a), AnimationTimeline::DashedIdent(b)) => a.eql(b),
(AnimationTimeline::Scroll(a), AnimationTimeline::Scroll(b)) => a == b,
(AnimationTimeline::View(a), AnimationTimeline::View(b)) => {
a.axis == b.axis && Size2D::eql(&a.inset, &b.inset)
}
_ => false,
}
}
}

/// The [scroll()](https://drafts.csswg.org/scroll-animations-1/#scroll-notation) function.
#[derive(PartialEq, Eq)]
pub struct ScrollTimeline {
/// Specifies which element to use as the scroll container.
pub scroller: Scroller,
/// Specifies which axis of the scroll container to use as the progress for the timeline.
pub axis: ScrollAxis,
}

/// The [view()](https://drafts.csswg.org/scroll-animations-1/#view-notation) function.
pub struct ViewTimeline {
/// Specifies which axis of the scroll container to use as the progress for the timeline.
pub axis: ScrollAxis,
/// Provides an adjustment of the view progress visibility range.
pub inset: Size2D<LengthPercentageOrAuto>,
}

/// A scroller, used in the `scroll()` function.
#[derive(Clone, Copy, PartialEq, Eq, Hash, css::DefineEnumProperty)]
pub enum Scroller {
/// Specifies to use the document viewport as the scroll container.
Root,
/// Specifies to use the nearest ancestor scroll container.
Nearest,
/// Specifies to use the element's own principal box as the scroll container.
#[css("self")]
Self_,
}

impl Scroller {
pub fn deep_clone(self) -> Self {
self
}
pub fn default() -> Scroller {
Scroller::Nearest
}
}

/// A scroll axis, used in the `scroll()` function.
#[derive(Clone, Copy, PartialEq, Eq, Hash, css::DefineEnumProperty)]
pub enum ScrollAxis {
/// Specifies to use the measure of progress along the block axis of the scroll container.
Block,
/// Specifies to use the measure of progress along the inline axis of the scroll container.
Inline,
/// Specifies to use the measure of progress along the horizontal axis of the scroll container.
X,
/// Specifies to use the measure of progress along the vertical axis of the scroll container.
Y,
}

impl ScrollAxis {
pub fn deep_clone(self) -> Self {
self
}
pub fn default() -> ScrollAxis {
ScrollAxis::Block
}
}

/// A value for the animation-range shorthand property.
pub struct AnimationRange {
/// The start of the animation's attachment range.
Expand Down
6 changes: 3 additions & 3 deletions src/css/properties/background.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![warn(unused_must_use)]
use crate as css;
use crate::css_values::color::ColorFallbackKind;
use crate::css_values::color::CssColor;
use crate::css_values::color::{ColorFallback, ColorFallbackKind};
use crate::css_values::image::Image;
use crate::css_values::length::LengthPercentageOrAuto;
use crate::css_values::position::{HorizontalPosition, Position, VerticalPosition};
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Background {
ret
}

pub(crate) fn get_fallback(&self, arena: &Bump, kind: ColorFallbackKind) -> Background {
pub(crate) fn get_fallback(&self, arena: &Bump, kind: ColorFallback) -> Background {
let mut ret = self.deep_clone(arena);
ret.color = self.color.get_fallback(arena, kind);
ret.image = self.image.get_fallback(arena, kind);
Expand Down Expand Up @@ -1170,7 +1170,7 @@ impl crate::small_list::ImageFallback for Background {
Background::with_image(self, arena, image)
}
#[inline]
fn get_fallback(&self, arena: &Bump, kind: ColorFallbackKind) -> Self {
fn get_fallback(&self, arena: &Bump, kind: ColorFallback) -> Self {
Background::get_fallback(self, arena, kind)
}
#[inline]
Expand Down
8 changes: 4 additions & 4 deletions src/css/properties/border.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![warn(unused_must_use)]
use crate as css;
use crate::css_properties::custom::UnparsedProperty;
use crate::css_values::color::{ColorFallbackKind, CssColor};
use crate::css_values::color::{ColorFallback, ColorFallbackKind, CssColor};
use crate::css_values::length::Length;
use crate::properties::{Property, PropertyId, PropertyIdTag};
use crate::targets::Browsers;
Expand Down Expand Up @@ -270,22 +270,22 @@ macro_rules! impl_fallbacks {
if fallbacks.contains(ColorFallbackKind::RGB) {
res.append(Self {
$(
$field: self.$field.get_fallback(arena, ColorFallbackKind::RGB),
$field: self.$field.get_fallback(arena, ColorFallback::Rgb),
)+
});
}

if fallbacks.contains(ColorFallbackKind::P3) {
res.append(Self {
$(
$field: self.$field.get_fallback(arena, ColorFallbackKind::P3),
$field: self.$field.get_fallback(arena, ColorFallback::P3),
)+
});
}

if fallbacks.contains(ColorFallbackKind::LAB) {
$(
self.$field = self.$field.get_fallback(arena, ColorFallbackKind::LAB);
self.$field = self.$field.get_fallback(arena, ColorFallback::Lab);
)+
}

Expand Down
Loading
Loading