Mercurial > yakumo_izuru > aya
comparison vendor/github.com/yosssi/gcss/element_base.go @ 66:787b5ee0289d draft
Use vendored modules
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
| author | yakumo.izuru |
|---|---|
| date | Sun, 23 Jul 2023 13:18:53 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 65:6d985efa0f7a | 66:787b5ee0289d |
|---|---|
| 1 package gcss | |
| 2 | |
| 3 import ( | |
| 4 "bytes" | |
| 5 "io" | |
| 6 ) | |
| 7 | |
| 8 // elementBase holds the common fields of an element. | |
| 9 type elementBase struct { | |
| 10 ln *line | |
| 11 parent element | |
| 12 sels []*selector | |
| 13 decs []*declaration | |
| 14 mixins []*mixinInvocation | |
| 15 ctx *context | |
| 16 } | |
| 17 | |
| 18 // AppendChild appends a child element to the element. | |
| 19 func (eBase *elementBase) AppendChild(child element) { | |
| 20 switch c := child.(type) { | |
| 21 case *mixinInvocation: | |
| 22 eBase.mixins = append(eBase.mixins, c) | |
| 23 case *declaration: | |
| 24 eBase.decs = append(eBase.decs, c) | |
| 25 case *selector: | |
| 26 eBase.sels = append(eBase.sels, c) | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Base returns the element base. | |
| 31 func (eBase *elementBase) Base() *elementBase { | |
| 32 return eBase | |
| 33 } | |
| 34 | |
| 35 // SetContext sets the context to the element. | |
| 36 func (eBase *elementBase) SetContext(ctx *context) { | |
| 37 eBase.ctx = ctx | |
| 38 } | |
| 39 | |
| 40 // Context returns the top element's context. | |
| 41 func (eBase *elementBase) Context() *context { | |
| 42 if eBase.parent != nil { | |
| 43 return eBase.parent.Context() | |
| 44 } | |
| 45 | |
| 46 return eBase.ctx | |
| 47 } | |
| 48 | |
| 49 // hasMixinDecs returns true if the element has a mixin | |
| 50 // which has declarations. | |
| 51 func (eBase *elementBase) hasMixinDecs() bool { | |
| 52 for _, mi := range eBase.mixins { | |
| 53 if decs, _ := mi.decsParams(); len(decs) > 0 { | |
| 54 return true | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 return false | |
| 59 } | |
| 60 | |
| 61 // hasMixinSels returns true if the element has a mixin | |
| 62 // which has selectors. | |
| 63 func (eBase *elementBase) hasMixinSels() bool { | |
| 64 for _, mi := range eBase.mixins { | |
| 65 if sels, _ := mi.selsParams(); len(sels) > 0 { | |
| 66 return true | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 return false | |
| 71 } | |
| 72 | |
| 73 // writeDecsTo writes the element's declarations to w. | |
| 74 func (eBase *elementBase) writeDecsTo(w io.Writer, params map[string]string) (int64, error) { | |
| 75 bf := new(bytes.Buffer) | |
| 76 | |
| 77 // Write the declarations. | |
| 78 for _, dec := range eBase.decs { | |
| 79 // Writing to the bytes.Buffer never returns an error. | |
| 80 dec.writeTo(bf, params) | |
| 81 } | |
| 82 | |
| 83 // Write the mixin's declarations. | |
| 84 for _, mi := range eBase.mixins { | |
| 85 decs, prms := mi.decsParams() | |
| 86 | |
| 87 for _, dec := range decs { | |
| 88 // Writing to the bytes.Buffer never returns an error. | |
| 89 dec.writeTo(bf, prms) | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 n, err := w.Write(bf.Bytes()) | |
| 94 | |
| 95 return int64(n), err | |
| 96 } | |
| 97 | |
| 98 // newElementBase creates and returns an element base. | |
| 99 func newElementBase(ln *line, parent element) elementBase { | |
| 100 return elementBase{ | |
| 101 ln: ln, | |
| 102 parent: parent, | |
| 103 } | |
| 104 } |
