Skip to content

Commit a529eb7

Browse files
committed
CSS: add space before \!important when KeepCSS2 is set, fixes #818
1 parent 4f2a6e6 commit a529eb7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

css/css.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (c *cssMinifier) minifyGrammar() {
180180

181181
// write out the offending declaration (but save the semicolon)
182182
vals := c.p.Values()
183-
if len(vals) > 0 && vals[len(vals)-1].TokenType == css.SemicolonToken {
183+
if 0 < len(vals) && vals[len(vals)-1].TokenType == css.SemicolonToken {
184184
vals = vals[:len(vals)-1]
185185
semicolonQueued = true
186186
}
@@ -383,7 +383,7 @@ func (c *cssMinifier) minifyDeclaration(property []byte, components []css.Token)
383383

384384
// Strip !important from the component list, this will be added later separately
385385
important := false
386-
if len(components) > 2 && components[len(components)-2].TokenType == css.DelimToken && components[len(components)-2].Data[0] == '!' && ToHash(components[len(components)-1].Data) == Important {
386+
if 2 < len(components) && components[len(components)-2].TokenType == css.DelimToken && components[len(components)-2].Data[0] == '!' && ToHash(components[len(components)-1].Data) == Important {
387387
components = components[:len(components)-2]
388388
important = true
389389
}
@@ -413,6 +413,9 @@ func (c *cssMinifier) minifyDeclaration(property []byte, components []css.Token)
413413
c.w.Write(component.Data)
414414
}
415415
if important {
416+
if c.o.KeepCSS2 {
417+
c.w.Write(spaceBytes)
418+
}
416419
c.w.Write(importantBytes)
417420
}
418421
return
@@ -456,6 +459,9 @@ func (c *cssMinifier) writeDeclaration(values []Token, important bool) {
456459
}
457460

458461
if important {
462+
if c.o.KeepCSS2 {
463+
c.w.Write(spaceBytes)
464+
}
459465
c.w.Write(importantBytes)
460466
}
461467
}

css/css_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ func TestCSSKeepCSS2(t *testing.T) {
450450
{`margin:5000em`, `margin:5000em`},
451451
{`color:transparent`, `color:transparent`},
452452
{`background-color:transparent`, `background-color:transparent`},
453+
//{`background-color:black!important`, `background-color:#000!important`}, // TODO: fails! may make result bigger
454+
{`background-color:black !important`, `background-color:#000 !important`},
453455
}
454456

455457
m := minify.New()

0 commit comments

Comments
 (0)