From 482fcb36de1e7c79bfbfd4162b82f4ad3f2e7f80 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 17:37:48 +0000 Subject: [PATCH 1/2] css: return RGBA from parse_hash_color instead of a tuple parse_hash_color returned (u8, u8, u8, f32) and both callers unpacked it as (r, g, b, a) only to build an RGBA from it, converting the byte channels to floats and back on the way. Build the RGBA in the parser and hand it out directly. The alpha byte was already exact, so the resulting RGBA values are unchanged. Drops the matching tuple_wants_struct entry for css_parser.rs from the mordant baseline. --- mordant-baseline.toml | 1 - src/css/css_parser.rs | 48 ++++++++++++++++++++++-------------- src/css/properties/custom.rs | 11 +++------ src/css/values/color.rs | 4 +-- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/mordant-baseline.toml b/mordant-baseline.toml index 109483f7b029..c231ac55bb70 100644 --- a/mordant-baseline.toml +++ b/mordant-baseline.toml @@ -14,7 +14,6 @@ [bun_css] "arg_named_like_other_param:src/css/rules/mod.rs" = 1 "bare_bool_args:src/css/selectors/builder.rs" = 1 -"tuple_wants_struct:src/css/css_parser.rs" = 1 "tuple_wants_struct:src/css/values/color.rs" = 5 [bun_install] diff --git a/src/css/css_parser.rs b/src/css/css_parser.rs index 71d84c513cf2..b2cfade3f66e 100644 --- a/src/css/css_parser.rs +++ b/src/css/css_parser.rs @@ -5537,8 +5537,10 @@ impl<'a> CopyOnWriteStr<'a> { // ───────────────────────────── color ───────────────────────────── pub mod color { - /// The opaque alpha value of 1.0. - pub(crate) const OPAQUE: f32 = 1.0; + use crate::values::color::RGBA; + + /// The alpha channel of a fully opaque color. + pub(crate) const OPAQUE: u8 = 255; #[derive(Debug, strum::IntoStaticStr)] pub enum ColorError { @@ -5727,29 +5729,39 @@ pub mod color { } /// Parse a color hash, without the leading '#' character. - pub(crate) fn parse_hash_color(value: &[u8]) -> Option<(u8, u8, u8, f32)> { + pub(crate) fn parse_hash_color(value: &[u8]) -> Option { parse_hash_color_impl(value).ok() } - pub(crate) fn parse_hash_color_impl(value: &[u8]) -> Result<(u8, u8, u8, f32), ColorError> { + pub(crate) fn parse_hash_color_impl(value: &[u8]) -> Result { let pair = |i: usize| { bun_core::fmt::hex_pair_value(value[i], value[i + 1]).ok_or(ColorError::Parse) }; match value.len() { - 8 => Ok((pair(0)?, pair(2)?, pair(4)?, pair(6)? as f32 / 255.0)), - 6 => Ok((pair(0)?, pair(2)?, pair(4)?, OPAQUE)), - 4 => Ok(( - from_hex(value[0])? * 17, - from_hex(value[1])? * 17, - from_hex(value[2])? * 17, - (from_hex(value[3])? * 17) as f32 / 255.0, - )), - 3 => Ok(( - from_hex(value[0])? * 17, - from_hex(value[1])? * 17, - from_hex(value[2])? * 17, - OPAQUE, - )), + 8 => Ok(RGBA { + red: pair(0)?, + green: pair(2)?, + blue: pair(4)?, + alpha: pair(6)?, + }), + 6 => Ok(RGBA { + red: pair(0)?, + green: pair(2)?, + blue: pair(4)?, + alpha: OPAQUE, + }), + 4 => Ok(RGBA { + red: from_hex(value[0])? * 17, + green: from_hex(value[1])? * 17, + blue: from_hex(value[2])? * 17, + alpha: from_hex(value[3])? * 17, + }), + 3 => Ok(RGBA { + red: from_hex(value[0])? * 17, + green: from_hex(value[1])? * 17, + blue: from_hex(value[2])? * 17, + alpha: OPAQUE, + }), _ => Err(ColorError::Parse), } } diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index ddc8252a7c2c..e7bf8788871b 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -15,7 +15,7 @@ use crate::printer::Printer; use crate::values as css_values; use css_values::angle::Angle; -use css_values::color::{ColorFallbackKind, CssColor, RGBA}; +use css_values::color::{ColorFallbackKind, CssColor}; use css_values::ident::{ CustomIdent, CustomIdentFns, DashedIdent, DashedIdentReference, Ident, IdentFns, }; @@ -650,16 +650,11 @@ impl TokenList { } Token::UnrestrictedHash(h) | Token::IdHash(h) => { 'brk: { - let Some((r, g, b, a)) = css_parser::color::parse_hash_color(h) else { + let Some(rgba) = css_parser::color::parse_hash_color(h) else { tokens.push(TokenOrValue::Token(Token::UnrestrictedHash(*h))); break 'brk; }; - tokens.push(TokenOrValue::Color(CssColor::Rgba(RGBA::from_floats( - r as f32 / 255.0, - g as f32 / 255.0, - b as f32 / 255.0, - a, - )))); + tokens.push(TokenOrValue::Color(CssColor::Rgba(rgba))); } last_is_delim = false; last_is_whitespace = false; diff --git a/src/css/values/color.rs b/src/css/values/color.rs index f9a77b6a06c4..1cca1cc554b5 100644 --- a/src/css/values/color.rs +++ b/src/css/values/color.rs @@ -405,10 +405,10 @@ impl CssColor { match token { css::Token::UnrestrictedHash(v) | css::Token::IdHash(v) => { - let Some((r, g, b, a)) = css::color::parse_hash_color(v) else { + let Some(rgba) = css::color::parse_hash_color(v) else { return Err(location.new_unexpected_token_error(token)); }; - Ok(CssColor::Rgba(RGBA::new(r, g, b, a))) + Ok(CssColor::Rgba(rgba)) } css::Token::Ident(value) => crate::match_ignore_ascii_case! { value, { b"currentcolor" => Ok(CssColor::CurrentColor), From d4cf979d0247cb088a3104c1026a1fb668c7f553 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 19:14:40 +0000 Subject: [PATCH 2/2] ci: retrigger