remote-darkness/internal/css/token.go

72 lines
1.4 KiB
Go
Raw Normal View History

2018-06-06 18:34:27 +00:00
package css
import (
"fmt"
"strings"
"github.com/gorilla/css/scanner"
)
type CSSToken struct {
Type tokenType
NativeToken *scanner.Token `json:"NativeToken,omitempty"`
SubTokens []*CSSToken `json:"SubTokens,omitempty"`
}
func (token *CSSToken) Value() string {
if token.NativeToken != nil {
return token.NativeToken.Value
}
value := ""
for _, token := range token.SubTokens {
value += token.Value()
}
return value
}
func (token *CSSToken) NonMachineContent() bool {
switch token.Type {
case TokenType_Native:
switch {
case isNativeNonMachineContent(token.NativeToken):
return true
}
}
return false
}
func (token *CSSToken) MachineSubTokens() (result []*CSSToken) {
result = make([]*CSSToken, 0)
for _, token := range token.SubTokens {
if token.NonMachineContent() {
continue
}
result = append(result, token)
}
return
}
func (token *CSSToken) Find(tokenTypes ...tokenType) *CSSToken {
for _, token := range token.SubTokens {
for _, tokenType := range tokenTypes {
if token.Type == tokenType {
return token
}
}
}
return nil
}
func (token *CSSToken) String() string {
retval := fmt.Sprintf("type=%s native=%s", tokenTypeNames[token.Type], token.NativeToken)
if token.SubTokens != nil {
retval += " {\n"
for _, token := range token.SubTokens {
retval += "\t" + strings.Replace(token.String(), "\n", "\n\t", -1) + "\n"
}
retval += "}"
}
return retval
}