Skip to content

Commit

Permalink
Update at_rule.go
Browse files Browse the repository at this point in the history
  • Loading branch information
yosssi committed Sep 17, 2014
1 parent a71ff9a commit 47b2e26
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 21 deletions.
19 changes: 14 additions & 5 deletions at_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,33 @@ func (ar *atRule) WriteTo(w io.Writer) (int64, error) {

bf.WriteString(strings.TrimSpace(ar.ln.s))

if len(ar.sels) == 0 && len(ar.decs) == 0 {
if len(ar.sels) == 0 && len(ar.decs) == 0 && !ar.hasMixinDecs() && !ar.hasMixinSels() {
bf.WriteString(semicolon)
n, err := w.Write(bf.Bytes())
return int64(n), err
}

bf.WriteString(openBrace)

for _, dec := range ar.decs {
// Writing to the bytes.Buffer never returns an error.
dec.WriteTo(bf)
}
// Writing to the bytes.Buffer never returns an error.
ar.writeDecsTo(bf, nil)

for _, sel := range ar.sels {
// Writing to the bytes.Buffer never returns an error.
sel.WriteTo(bf)
}

// Write the mixin's selectors.
for _, mi := range ar.mixins {
sels, prms := mi.selsParams()

for _, sl := range sels {
sl.parent = ar
// Writing to the bytes.Buffer never returns an error.
sl.writeTo(bf, prms)
}
}

bf.WriteString(closeBrace)

n, err := w.Write(bf.Bytes())
Expand Down
2 changes: 1 addition & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Test_e2e(t *testing.T) {
var wg sync.WaitGroup

for i := 1; i <= 10; i++ {
for i := 1; i <= 11; i++ {
idx := strconv.Itoa(i)

gcssPath := "test/e2e/actual/" + strings.Repeat("0", 4-len(idx)) + idx + ".gcss"
Expand Down
44 changes: 43 additions & 1 deletion element_base.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package gcss

import (
"bytes"
"io"
)

// elementBase holds the common fields of an element.
type elementBase struct {
ln *line
Expand Down Expand Up @@ -41,7 +46,7 @@ func (eBase *elementBase) Context() *context {
return eBase.ctx
}

// hasMixinDecs returns true if the selector has a mixin
// hasMixinDecs returns true if the element has a mixin
// which has declarations.
func (eBase *elementBase) hasMixinDecs() bool {
for _, mi := range eBase.mixins {
Expand All @@ -53,6 +58,43 @@ func (eBase *elementBase) hasMixinDecs() bool {
return false
}

// hasMixinSels returns true if the element has a mixin
// which has selectors.
func (eBase *elementBase) hasMixinSels() bool {
for _, mi := range eBase.mixins {
if sels, _ := mi.selsParams(); len(sels) > 0 {
return true
}
}

return false
}

// writeDecsTo writes the element's declarations to w.
func (eBase *elementBase) writeDecsTo(w io.Writer, params map[string]string) (int64, error) {
bf := new(bytes.Buffer)

// Write the declarations.
for _, dec := range eBase.decs {
// Writing to the bytes.Buffer never returns an error.
dec.writeTo(bf, params)
}

// Write the mixin's declarations.
for _, mi := range eBase.mixins {
decs, prms := mi.decsParams()

for _, dec := range decs {
// Writing to the bytes.Buffer never returns an error.
dec.writeTo(bf, prms)
}
}

n, err := w.Write(bf.Bytes())

return int64(n), err
}

// newElementBase creates and returns an element base.
func newElementBase(ln *line, parent element) elementBase {
return elementBase{
Expand Down
16 changes: 2 additions & 14 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,8 @@ func (sel *selector) writeTo(w io.Writer, params map[string]string) (int64, erro
bf.WriteString(sel.names())
bf.WriteString(openBrace)

for _, dec := range sel.decs {
// Writing to the bytes.Buffer never returns an error.
dec.writeTo(bf, params)
}

// Write the mixin's declarations.
for _, mi := range sel.mixins {
decs, prms := mi.decsParams()

for _, dec := range decs {
// Writing to the bytes.Buffer never returns an error.
dec.writeTo(bf, prms)
}
}
// Writing to the bytes.Buffer never returns an error.
sel.writeDecsTo(bf, params)

bf.WriteString(closeBrace)
}
Expand Down
55 changes: 55 additions & 0 deletions test/e2e/actual/0011.gcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// At-rules which include mixins
$color: black

$mixin-for-media-1($font-size)
font-size: $font-size

$mixin-for-media-2($background)
html
background: $background
color: $color

$mixin-for-media-3()
h1
color: red

$mixin-for-page($margin)
margin: $margin

$mixin-for-font-face()
font-family: "Example Font"
src: url("http://www.example.com/fonts/example")

@media screen, projection
$mixin-for-media-1(13px)
$mixin-for-media-2(red)
body
max-width: 35em
margin: 0 auto
$mixin-for-media-3()

@media screen, projection
$mixin-for-media-2(red)

@media print
$mixin-for-media-1(13px)
$mixin-for-media-2(red)
body
max-width: 35em
margin: 0 auto
$mixin-for-media-3()

@page
$mixin-for-page(2.5cm)

@page :left
$mixin-for-page(2.5cm)

@page :right
$mixin-for-page(2.5cm)

@page :first
$mixin-for-page(2.5cm)

@font-face
$mixin-for-font-face()
1 change: 1 addition & 0 deletions test/e2e/expected/0011.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47b2e26

Please sign in to comment.