Skip to content

Commit

Permalink
web/core: Apply all properties to element in logical order
Browse files Browse the repository at this point in the history
Previously, we would iterate the list of matched properties
in reverse order (most important first) and keep track of which
properties were set (using a hashset) to avoid less-important
values overriding more-important ones.

This wasn't perfect because shorthand properties set multiple
properties at once and its not trivial to keep track of that in
a nice way. Also we constantly need to allocate for the hashset
(no benchmarks, but it *sounds* slow)

Instead, we now iterate the list in logical order, applying
less-important rules first and then overriding them later if necessary.
  • Loading branch information
simonwuelker committed Dec 11, 2023
1 parent d86490d commit 937420f
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions crates/web/core/src/css/stylecomputer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cmp, collections::HashSet, mem};
use std::cmp;

use crate::{
css::{
Expand Down Expand Up @@ -169,15 +169,11 @@ impl<'a> StyleComputer<'a> {
matched_properties.sort_unstable_by(MatchingProperty::compare_in_cascade_order);
let mut computed_style = parent_style.get_inherited();

// Add properties in reverse order (most important first)
let mut properties_added = HashSet::new();

for matched_property in matched_properties.into_iter().rev() {
// Add properties in logical order (least important first)
// That way, more important rules can override less important ones
for matched_property in matched_properties.into_iter() {
let property = matched_property.into_property();

if properties_added.insert(mem::discriminant(&property)) {
computed_style.set_property(property);
}
computed_style.set_property(property);
}

computed_style
Expand Down

0 comments on commit 937420f

Please sign in to comment.