diff --git a/element/parser.go b/element/parser.go
index 19d14d7..8645ece 100644
--- a/element/parser.go
+++ b/element/parser.go
@@ -4 +4 @@ import (
- "bytes"
+ "regexp"
@@ -19,3 +19,3 @@ func ParseCSS(css string) map[string][]*StyleMap {
- // Split into rule blocks
- blocks := splitBlocks(css)
-
+ // Parse regular selectors and styles
+ selectorRegex := regexp.MustCompile(`([^{]+){([^}]+)}`)
+ matches := selectorRegex.FindAllStringSubmatch(css, -1)
@@ -22,0 +23,3 @@ func ParseCSS(css string) map[string][]*StyleMap {
+ for _, match := range matches {
+ selectorBlock := strings.TrimSpace(match[1])
+ styleBlock := match[2]
@@ -24,11 +26,0 @@ func ParseCSS(css string) map[string][]*StyleMap {
- for _, block := range blocks {
- // Split selector from style declarations
- parts := strings.SplitN(block, "{", 2)
- if len(parts) != 2 {
- continue // Invalid block
- }
-
- selectorBlock := strings.TrimSpace(parts[0])
- styleBlock := strings.TrimSpace(strings.TrimSuffix(parts[1], "}"))
-
- // Parse selectors and styles
@@ -36,3 +27,0 @@ func ParseCSS(css string) map[string][]*StyleMap {
- styles := parseStylesSimple(styleBlock)
-
- // Add to style maps
@@ -39,0 +29,2 @@ func ParseCSS(css string) map[string][]*StyleMap {
+ styles := parseStyles(styleBlock)
+
@@ -40,0 +32 @@ func ParseCSS(css string) map[string][]*StyleMap {
+ smm := map[string]*StyleMap{}
@@ -43,5 +35,3 @@ func ParseCSS(css string) map[string][]*StyleMap {
- styleMap := &StyleMap{
- Selector: s,
- Styles: &styles,
- }
-
+ smm[v] = &StyleMap{}
+ smm[v].Styles = &styles
+ smm[v].Selector = s
@@ -51 +41 @@ func ParseCSS(css string) map[string][]*StyleMap {
- styleMaps[v] = append(styleMaps[v], styleMap)
+ styleMaps[v] = append(styleMaps[v], smm[v])
@@ -53,3 +42,0 @@ func ParseCSS(css string) map[string][]*StyleMap {
- }
- }
- }
@@ -57,21 +43,0 @@ func ParseCSS(css string) map[string][]*StyleMap {
- return styleMaps
-}
-
-// splitBlocks splits CSS into rule blocks without using regex
-func splitBlocks(css string) []string {
- var blocks []string
- var currentBlock bytes.Buffer
- var braceDepth int
-
- for i := 0; i < len(css); i++ {
- ch := css[i]
-
- if ch == '{' {
- braceDepth++
- } else if ch == '}' {
- braceDepth--
- if braceDepth == 0 {
- currentBlock.WriteByte(ch)
- blocks = append(blocks, currentBlock.String())
- currentBlock.Reset()
- continue
@@ -80,4 +45,0 @@ func splitBlocks(css string) []string {
-
- if braceDepth > 0 || !isWhitespace(ch) {
- currentBlock.WriteByte(ch)
- }
@@ -86,28 +48 @@ func splitBlocks(css string) []string {
- return blocks
-}
-
-// parseStylesSimple parses CSS style declarations without using regex
-func parseStylesSimple(styleBlock string) map[string]string {
- styles := make(map[string]string)
- declarations := strings.Split(styleBlock, ";")
-
- for _, declaration := range declarations {
- declaration = strings.TrimSpace(declaration)
- if declaration == "" {
- continue
- }
-
- parts := strings.SplitN(declaration, ":", 2)
- if len(parts) != 2 {
- continue
- }
-
- property := strings.TrimSpace(parts[0])
- value := strings.TrimSpace(parts[1])
-
- if property != "" && value != "" {
- styles[property] = value
- }
- }
-
- return styles
+ return styleMaps
@@ -152,18 +87,3 @@ func parseSelectors(selectorBlock string) []string {
-// removeComments removes CSS comments without using regex
-func removeComments(css string) string {
- var result bytes.Buffer
- i := 0
-
- for i < len(css) {
- if i+1 < len(css) && css[i] == '/' && css[i+1] == '*' {
- // Skip until end of comment
- i += 2
- for i+1 < len(css) && !(css[i] == '*' && css[i+1] == '/') {
- i++
- }
- i += 2 // Skip */
- } else {
- result.WriteByte(css[i])
- i++
- }
- }
+func parseStyles(styleBlock string) map[string]string {
+ styleRegex := regexp.MustCompile(`([a-zA-Z-]+)\s*:\s*([^;]+);`)
+ matches := styleRegex.FindAllStringSubmatch(styleBlock, -1)
@@ -171,2 +91,6 @@ func removeComments(css string) string {
- return result.String()
-}
+ styleMap := make(map[string]string)
+ for _, match := range matches {
+ propName := strings.TrimSpace(match[1])
+ propValue := strings.TrimSpace(match[2])
+ styleMap[propName] = propValue
+ }
@@ -174,2 +98 @@ func removeComments(css string) string {
-func isWhitespace(ch byte) bool {
- return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
+ return styleMap
@@ -216,0 +140,5 @@ func parseKeyValue(style string) (string, string) {
+
+func removeComments(css string) string {
+ commentRegex := regexp.MustCompile(`(?s)/\*.*?\*/`)
+ return commentRegex.ReplaceAllString(css, "")
+}
package element
import (
"bytes"
"strings"
)
type StyleMap struct {
Selector string
Styles *map[string]string
Sheet int
PsuedoStyles map[string]map[string]map[string]string
}
func ParseCSS(css string) map[string][]*StyleMap {
// Remove comments
css = removeComments(css)
// Split into rule blocks
blocks := splitBlocks(css)
styleMaps := map[string][]*StyleMap{}
for _, block := range blocks {
// Split selector from style declarations
parts := strings.SplitN(block, "{", 2)
if len(parts) != 2 {
continue // Invalid block
}
selectorBlock := strings.TrimSpace(parts[0])
styleBlock := strings.TrimSpace(strings.TrimSuffix(parts[1], "}"))
// Parse selectors and styles
selectors := parseSelectors(selectorBlock)
styles := parseStylesSimple(styleBlock)
// Add to style maps
for _, s := range selectors {
sel := ExtractBaseElements(s)
for _, is := range sel {
for _, v := range is {
styleMap := &StyleMap{
Selector: s,
Styles: &styles,
}
if styleMaps[v] == nil {
styleMaps[v] = []*StyleMap{}
}
styleMaps[v] = append(styleMaps[v], styleMap)
}
}
}
}
return styleMaps
}
// splitBlocks splits CSS into rule blocks without using regex
func splitBlocks(css string) []string {
var blocks []string
var currentBlock bytes.Buffer
var braceDepth int
for i := 0; i < len(css); i++ {
ch := css[i]
if ch == '{' {
braceDepth++
} else if ch == '}' {
braceDepth--
if braceDepth == 0 {
currentBlock.WriteByte(ch)
blocks = append(blocks, currentBlock.String())
currentBlock.Reset()
continue
}
}
if braceDepth > 0 || !isWhitespace(ch) {
currentBlock.WriteByte(ch)
}
}
return blocks
}
// parseStylesSimple parses CSS style declarations without using regex
func parseStylesSimple(styleBlock string) map[string]string {
styles := make(map[string]string)
declarations := strings.Split(styleBlock, ";")
for _, declaration := range declarations {
declaration = strings.TrimSpace(declaration)
if declaration == "" {
continue
}
parts := strings.SplitN(declaration, ":", 2)
if len(parts) != 2 {
continue
}
property := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if property != "" && value != "" {
styles[property] = value
}
}
return styles
}
func parseSelectors(selectorBlock string) []string {
var selectors []string
var current strings.Builder
var parenthesesDepth int
for _, char := range selectorBlock {
switch char {
case '(':
// Enter parentheses
parenthesesDepth++
current.WriteRune(char)
case ')':
// Exit parentheses
parenthesesDepth--
current.WriteRune(char)
case ',':
// Split only if not inside parentheses
if parenthesesDepth == 0 {
selectors = append(selectors, strings.TrimSpace(current.String()))
current.Reset()
} else {
current.WriteRune(char)
}
default:
current.WriteRune(char)
}
}
// Add the last selector (if any)
if current.Len() > 0 {
selectors = append(selectors, strings.TrimSpace(current.String()))
}
return selectors
}
// removeComments removes CSS comments without using regex
func removeComments(css string) string {
var result bytes.Buffer
i := 0
for i < len(css) {
if i+1 < len(css) && css[i] == '/' && css[i+1] == '*' {
// Skip until end of comment
i += 2
for i+1 < len(css) && !(css[i] == '*' && css[i+1] == '/') {
i++
}
i += 2 // Skip */
} else {
result.WriteByte(css[i])
i++
}
}
return result.String()
}
func isWhitespace(ch byte) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}
func ParseStyleAttribute(styleValue string) map[string]string {
styleMap := make(map[string]string)
start := 0
for i := 0; i < len(styleValue); i++ {
if styleValue[i] == ';' {
part := styleValue[start:i]
if len(part) > 0 {
key, value := parseKeyValue(part)
if key != "" && value != "" {
styleMap[key] = value
}
}
start = i + 1
}
}
// Handle the last part if there's no trailing semicolon
if start < len(styleValue) {
part := styleValue[start:]
key, value := parseKeyValue(part)
if key != "" && value != "" {
styleMap[key] = value
}
}
return styleMap
}
func parseKeyValue(style string) (string, string) {
for i := 0; i < len(style); i++ {
if style[i] == ':' {
key := strings.TrimSpace(style[:i])
value := strings.TrimSpace(style[i+1:])
return key, value
}
}
return "", ""
}