Skip to content

Commit a56438c

Browse files
committed
Handle removing shorthand subsets from another shorthand
1 parent 088b6e3 commit a56438c

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/declaration.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ impl<'i> DeclarationBlock<'i> {
280280
/// the shorthand will be split apart into its component longhand properties, minus the property
281281
/// to remove. When removing a shorthand, all included longhand properties are also removed.
282282
pub fn remove(&mut self, property_id: &PropertyId) {
283-
fn remove<'i, 'a>(declarations: &mut Vec<Property<'i>>, property_id: &PropertyId<'a>) {
283+
fn remove<'i, 'a>(declarations: &mut Vec<Property<'i>>, property_id: &PropertyId<'a>) -> bool {
284284
let longhands = property_id.longhands().unwrap_or(vec![]);
285+
let mut needs_minify = false;
285286
let mut i = 0;
286287
while i < declarations.len() {
287288
let replacement = {
@@ -291,15 +292,21 @@ impl<'i> DeclarationBlock<'i> {
291292
// If the property matches the requested property id, or is a longhand
292293
// property that is included in the requested shorthand, remove it.
293294
None
294-
} else if longhands.is_empty() && id.longhands().unwrap_or(vec![]).contains(&property_id) {
295+
} else if id
296+
.longhands()
297+
.unwrap_or(vec![])
298+
.iter()
299+
.any(|longhand| *longhand == *property_id || longhands.contains(longhand))
300+
{
295301
// If this is a shorthand property that includes the requested longhand,
296302
// split it apart into its component longhands, excluding the requested one.
303+
needs_minify = !longhands.is_empty();
297304
Some(
298305
id.longhands()
299306
.unwrap()
300307
.iter()
301308
.filter_map(|longhand| {
302-
if *longhand == *property_id {
309+
if *longhand == *property_id || longhands.contains(longhand) {
303310
None
304311
} else {
305312
property.longhand(longhand)
@@ -324,10 +331,23 @@ impl<'i> DeclarationBlock<'i> {
324331
}
325332
}
326333
}
334+
335+
needs_minify
327336
}
328337

329-
remove(&mut self.declarations, property_id);
330-
remove(&mut self.important_declarations, property_id);
338+
let needs_minify = remove(&mut self.declarations, property_id);
339+
let needs_minify2 = remove(&mut self.important_declarations, property_id);
340+
341+
// If we split apart a shorthand property into longhands for another shorthand
342+
// (e.g. removing border-top from border), then re-minify to combine the remaining
343+
// properties into shorthands where possible.
344+
if needs_minify || needs_minify2 {
345+
self.minify(
346+
&mut DeclarationHandler::new(None),
347+
&mut DeclarationHandler::new(None),
348+
&mut PropertyHandlerContext::new(None),
349+
)
350+
}
331351
}
332352
}
333353

tests/test_cssom.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,9 @@ fn test_remove() {
505505
PropertyId::FlexDirection(VendorPrefix::WebKit),
506506
"-webkit-flex-wrap: wrap",
507507
);
508+
remove_test(
509+
"border: 1px solid #000",
510+
PropertyId::BorderTop,
511+
"border-bottom: 1px solid #000; border-left: 1px solid #000; border-right: 1px solid #000",
512+
);
508513
}

0 commit comments

Comments
 (0)