Diff
diff --git a/main.go b/main.go
index b34db76..6e88272 100644
--- a/main.go
+++ b/main.go
@@ -1 +1 @@
-package gui
+package main
@@ -4,2 +4,2 @@ import (
- "bufio"
- "gui/cstyle"
+ "fmt"
+ "gui/document"
@@ -7,8 +7 @@ import (
- "net/url"
- "os"
- "path/filepath"
- "regexp"
- "strconv"
- "strings"
-
- "golang.org/x/net/html"
+ // _ "net/http/pprof"
@@ -17,72 +10,5 @@ import (
-// _ "net/http/pprof"
-
-type Window struct {
- CSS cstyle.CSS
- Document element.Node
-}
-
-func Open(path string) Window {
- window := New()
-
- styleSheets, styleTags, html := parseHTMLFromFile(path)
-
- for _, v := range styleSheets {
- window.CSS.StyleSheet(v)
- }
-
- for _, v := range styleTags {
- window.CSS.StyleTag(v)
- }
-
- return window
-}
-
-func New() Window {
- el := element.Node{}
- document := el.CreateElement("ROOT")
- document.Style["width"] = "800px"
- document.Style["height"] = "450px"
- document.Properties.Id = "ROOT"
- return Window{
- CSS: cstyle.CSS{
- Width: 800,
- Height: 450,
- },
- Document: document,
- }
-}
-
-func Display(window Window, width, height int) {
- window.Document.Style["width"] = strconv.Itoa(width) + "px"
- window.Document.Style["height"] = strconv.Itoa(height) + "px"
-}
-
-func parseHTMLFromFile(path string) ([]string, []string, *html.Node) {
- file, _ := os.Open(path)
- defer file.Close()
-
- scanner := bufio.NewScanner(file)
- var htmlContent string
-
- for scanner.Scan() {
- htmlContent += scanner.Text() + "\n"
- }
-
- // println(encapsulateText(htmlContent))
- doc, _ := html.Parse(strings.NewReader(encapsulateText(htmlContent)))
-
- // Extract stylesheet link tags and style tags
- stylesheets := extractStylesheets(doc, filepath.Dir(path))
- styleTags := extractStyleTags(doc)
-
- return stylesheets, styleTags, doc
-}
-
-func extractStylesheets(n *html.Node, baseDir string) []string {
- var stylesheets []string
-
- var dfs func(*html.Node)
- dfs = func(node *html.Node) {
- if node.Type == html.ElementNode && node.Data == "link" {
- var href string
- isStylesheet := false
+func main() {
+ // Server for pprof
+ // go func() {
+ // fmt.Println(http.ListenAndServe("localhost:6060", nil))
+ // }()
@@ -90,7 +16 @@ func extractStylesheets(n *html.Node, baseDir string) []string {
- for _, attr := range node.Attr {
- if attr.Key == "rel" && attr.Val == "stylesheet" {
- isStylesheet = true
- } else if attr.Key == "href" {
- href = attr.Val
- }
- }
+ document := document.Document{}
@@ -98,48 +18 @@ func extractStylesheets(n *html.Node, baseDir string) []string {
- if isStylesheet {
- resolvedHref := localizePath(baseDir, href)
- stylesheets = append(stylesheets, resolvedHref)
- }
- }
-
- for c := node.FirstChild; c != nil; c = c.NextSibling {
- dfs(c)
- }
- }
-
- dfs(n)
- return stylesheets
-}
-
-func extractStyleTags(n *html.Node) []string {
- var styleTags []string
-
- var dfs func(*html.Node)
- dfs = func(node *html.Node) {
- if node.Type == html.ElementNode && node.Data == "style" {
- var styleContent strings.Builder
- for c := node.FirstChild; c != nil; c = c.NextSibling {
- if c.Type == html.TextNode {
- styleContent.WriteString(c.Data)
- }
- }
- styleTags = append(styleTags, styleContent.String())
- }
-
- for c := node.FirstChild; c != nil; c = c.NextSibling {
- dfs(c)
- }
- }
-
- dfs(n)
- return styleTags
-}
-
-func localizePath(rootPath, filePath string) string {
- // Check if the file path has a scheme, indicating it's a URL
- u, err := url.Parse(filePath)
- if err == nil && u.Scheme != "" {
- return filePath
- }
-
- // Join the root path and the file path to create an absolute path
- absPath := filepath.Join(rootPath, filePath)
+ // html := document.CreateElement("html")
@@ -147,4 +20 @@ func localizePath(rootPath, filePath string) string {
- // If the absolute path is the same as the original path, return it
- if absPath == filePath {
- return filePath
- }
+ // document := window.New()
@@ -152,16 +22,3 @@ func localizePath(rootPath, filePath string) string {
- return "./" + absPath
-}
-
-func encapsulateText(htmlString string) string {
- htmlString = removeHTMLComments(htmlString)
- openOpen := regexp.MustCompile(`(<\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
- closeOpen := regexp.MustCompile(`(\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
- closeClose := regexp.MustCompile(`(\w+[^>]*>)([^<]+)(\w+[^>]*>)`)
- a := matchFactory(openOpen)
- t := openOpen.ReplaceAllStringFunc(htmlString, a)
- b := matchFactory(closeOpen)
- u := closeOpen.ReplaceAllStringFunc(t, b)
- c := matchFactory(closeClose)
- v := closeClose.ReplaceAllStringFunc(u, c)
- return v
-}
+ // html.OnLoad(func() {
+ // fmt.Println("loaded")
+ // })
@@ -169,5 +26,9 @@ func encapsulateText(htmlString string) string {
-func matchFactory(re *regexp.Regexp) func(string) string {
- return func(match string) string {
- submatches := re.FindStringSubmatch(match)
- if len(submatches) != 4 {
- return match
+ document.Open("./src/app.html", func(doc *element.Node) {
+ row := doc.QuerySelector(".row")
+ buttons := row.QuerySelectorAll(".button")
+ b := *buttons
+ for i := range b {
+ b[i].AddEventListener("click", func(e element.Event) {
+ fmt.Println("Click: ", e.Target.InnerText)
+ e.Target.InnerText = "mason"
+ })
@@ -176,18 +37,10 @@ func matchFactory(re *regexp.Regexp) func(string) string {
- // Process submatches
- if len(removeWhitespace(submatches[2])) > 0 {
- return submatches[1] + "" + submatches[2] + "" + submatches[3]
- } else {
- return match
- }
- }
-}
-func removeWhitespace(htmlString string) string {
- // Remove extra white space
- reSpaces := regexp.MustCompile(`\s+`)
- htmlString = reSpaces.ReplaceAllString(htmlString, " ")
-
- // Trim leading and trailing white space
- htmlString = strings.TrimSpace(htmlString)
-
- return htmlString
-}
+ editor := doc.QuerySelector("#editor")
+ editor.AddEventListener("keypress", func(e element.Event) {
+ fmt.Println("key", editor.Value, editor.Properties.Id)
+ })
+ // div := document.CreateElement("div")
+ // div.InnerText = "test"
+ // div.ClassList.Add("button")
+ // row.Children = []element.Node{}
+ // row.AppendChild(div)
+ })
@@ -195,3 +47,0 @@ func removeWhitespace(htmlString string) string {
-func removeHTMLComments(htmlString string) string {
- re := regexp.MustCompile(``)
- return re.ReplaceAllString(htmlString, "")