58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
package svg
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type (
|
|
// Element is a convenience for implementing SVG element models.
|
|
// Element implements MarshalerReader.
|
|
Element[T Tag, A Attribute, C MarshalerReader] struct {
|
|
Attributes []A
|
|
Children []C
|
|
}
|
|
)
|
|
|
|
func (e *Element[T, A, C]) Attr(as ...A) *Element[T, A, C] {
|
|
e.Attributes = append(e.Attributes, as...)
|
|
return e
|
|
}
|
|
|
|
func (e *Element[T, A, C]) Child(cs ...C) *Element[T, A, C] {
|
|
e.Children = append(e.Children, cs...)
|
|
return e
|
|
}
|
|
|
|
func (e Element[T, A, C]) GetMarkup() string {
|
|
attrs := make([]string, len(e.Attributes))
|
|
for i, a := range e.Attributes {
|
|
attrs[i] = PrintAttribute(a)
|
|
}
|
|
|
|
children := make([]string, len(e.Children))
|
|
for i, c := range e.Children {
|
|
children[i] = c.GetMarkup()
|
|
}
|
|
|
|
tag := e.PrintTag()
|
|
|
|
return fmt.Sprintf(
|
|
`<%s %s>%s</%s>`,
|
|
tag,
|
|
strings.Join(attrs, " "),
|
|
strings.Join(children, ""),
|
|
tag,
|
|
)
|
|
}
|
|
|
|
func (e Element[T, A, C]) GetMarkupReader() io.Reader {
|
|
return newElementReader(e)
|
|
}
|
|
|
|
func (e Element[T, _, _]) PrintTag() string {
|
|
var t T
|
|
return t.PrintTag()
|
|
}
|