Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions src/css/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ impl VendorPrefix {
self.bits()
}

/// The prefix a selector component with these prefixes is written with: none
/// if it has an unprefixed variant, else the last of them in `FIELDS` order.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[inline]
pub(crate) fn canonical(self) -> VendorPrefix {
if self.is_empty() || self.contains(VendorPrefix::NONE) {
return VendorPrefix::NONE;
}
VendorPrefix::from_bits_retain(1 << (u8::BITS - 1 - self.bits().leading_zeros()))
}

/// Detects a leading vendor prefix on `name` (case-insensitive, ASCII) and
/// returns it together with the slice that follows the prefix.
///
Expand Down
25 changes: 9 additions & 16 deletions src/css/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,6 @@ fn merge_style_rules<R>(
src_compat: &mut Option<bool>,
dst_compat: &mut Option<bool>,
) -> bool {
use css::VendorPrefix;
// Merge declarations if the selectors are equivalent, and both are compatible with all targets.
// Does not apply if css modules are enabled.
if src.selectors.eql(&dst.selectors)
Expand Down Expand Up @@ -1137,18 +1136,18 @@ fn merge_style_rules<R>(

if src.declarations.eql(&dst.declarations) && src.rules.v.is_empty() && dst.rules.v.is_empty() {
// If both selectors are potentially vendor prefixable, and they are
// equivalent minus prefixes, add the prefix to the last rule.
// equivalent minus prefixes, add the prefixes to the last rule.
if !src.vendor_prefix.is_empty()
&& !dst.vendor_prefix.is_empty()
&& css::selector::is_equivalent(src.selectors.v.slice(), dst.selectors.v.slice())
&& css::selector::merge_prefixes(
dst.selectors.v.slice_mut(),
src.selectors.v.slice(),
context.targets,
)
{
if src.vendor_prefix.contains(VendorPrefix::NONE)
&& context.targets.should_compile_selectors()
{
dst.vendor_prefix = src.vendor_prefix;
} else {
dst.vendor_prefix.insert(src.vendor_prefix);
}
dst.vendor_prefix = css::selector::prefix_passes(dst.selectors.v.slice());
*dst_compat = None;
return true;
}

Expand All @@ -1167,13 +1166,7 @@ fn merge_style_rules<R>(
// Both sides were just proven compatible, so the combined selector
// list is too.
*dst_compat = Some(true);
if src.vendor_prefix.contains(VendorPrefix::NONE)
&& context.targets.should_compile_selectors()
{
dst.vendor_prefix = src.vendor_prefix;
} else {
dst.vendor_prefix.insert(src.vendor_prefix);
}
dst.vendor_prefix.insert(src.vendor_prefix);
return true;
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/css/rules/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{PrintErr, Printer, VendorPrefix};
pub struct StyleRule<R> {
/// The selectors for the style rule.
pub(crate) selectors: selector::parser::SelectorList,
/// A vendor prefix override, used during selector printing.
/// The vendor prefix passes to print this rule with (see
/// `selector::prefix_passes`), or empty to print it once as written.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) vendor_prefix: VendorPrefix,
/// The declarations within the style rule.
pub(crate) declarations: DeclarationBlock<'static>,
Expand Down Expand Up @@ -63,6 +64,9 @@ impl<R> StyleRule<R> {
context.targets,
);
}
if !self.vendor_prefix.is_empty() {
self.vendor_prefix = selector::prefix_passes(self.selectors.v.slice());
}
}

pub(crate) fn is_compatible(&self, targets: &css::targets::Targets) -> bool {
Expand Down Expand Up @@ -96,14 +100,22 @@ impl<R> StyleRule<R> {
if self.vendor_prefix.is_empty() {
self.to_css_base(dest, true)?;
} else {
// With nesting compiled away, the parent selectors substituted for
// `&` print in this rule's passes too.
Comment thread
robobun marked this conversation as resolved.
Resolved
let passes = match dest.ctx {
Some(parents) => {
selector::nested_prefix_passes(self.selectors.v.slice(), Some(parents))
}
None => self.vendor_prefix,
};
let mut first_rule = true;
let mut emitted_first_pass = false;
let mut remaining_prefixes = self.vendor_prefix;
let mut remaining_prefixes = passes;
// `inline for (css.VendorPrefix.FIELDS) |field|` — iterate the bool fields of the
// packed struct in declared order. In Rust the bitflags type exposes the same
// ordered single-bit table directly.
for &prefix in VendorPrefix::FIELDS {
if self.vendor_prefix.contains(prefix) {
if passes.contains(prefix) {
remaining_prefixes.remove(prefix);
if !first_rule {
if !dest.minify {
Expand Down
40 changes: 30 additions & 10 deletions src/css/selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,20 +1072,30 @@ impl PseudoClass {
}
}

pub(crate) fn get_necessary_prefixes(
/// The vendor prefixes of a prefixable pseudo class, with the feature that
/// decides which prefixes the browser targets need.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn prefix_mut(
&mut self,
targets: &css::targets::Targets,
) -> css::VendorPrefix {
) -> Option<(&mut css::VendorPrefix, css::prefixes::Feature)> {
use PseudoClass as P;
use css::prefixes::Feature as F;
let (p, feature): (&mut css::VendorPrefix, F) = match self {
Some(match self {
P::Fullscreen(p) => (p, F::PseudoClassFullscreen),
P::AnyLink(p) => (p, F::PseudoClassAnyLink),
P::ReadOnly(p) => (p, F::PseudoClassReadOnly),
P::ReadWrite(p) => (p, F::PseudoClassReadWrite),
P::PlaceholderShown(p) => (p, F::PseudoClassPlaceholderShown),
P::Autofill(p) => (p, F::PseudoClassAutofill),
_ => return css::VendorPrefix::empty(),
_ => return None,
})
}

pub(crate) fn get_necessary_prefixes(
&mut self,
targets: &css::targets::Targets,
) -> css::VendorPrefix {
let Some((p, feature)) = self.prefix_mut() else {
return css::VendorPrefix::empty();
};
*p = targets.prefixes(*p, feature);
*p
Expand Down Expand Up @@ -3015,18 +3025,28 @@ impl PseudoElement {
self.clone()
}

pub(crate) fn get_necessary_prefixes(
/// The vendor prefixes of a prefixable pseudo element, with the feature
/// that decides which prefixes the browser targets need.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn prefix_mut(
&mut self,
targets: &css::targets::Targets,
) -> css::VendorPrefix {
) -> Option<(&mut css::VendorPrefix, css::prefixes::Feature)> {
use PseudoElement as PE;
use css::prefixes::Feature as F;
let (p, feature): (&mut css::VendorPrefix, F) = match self {
Some(match self {
PE::Selection(p) => (p, F::PseudoElementSelection),
PE::Placeholder(p) => (p, F::PseudoElementPlaceholder),
PE::Backdrop(p) => (p, F::PseudoElementBackdrop),
PE::FileSelectorButton(p) => (p, F::PseudoElementFileSelectorButton),
_ => return css::VendorPrefix::empty(),
_ => return None,
})
}

pub(crate) fn get_necessary_prefixes(
&mut self,
targets: &css::targets::Targets,
) -> css::VendorPrefix {
let Some((p, feature)) = self.prefix_mut() else {
return css::VendorPrefix::empty();
};
*p = targets.prefixes(*p, feature);
*p
Expand Down
Loading
Loading