Commits
Diff
diff --git a/main.go b/main.go
index 6710144..d659ffb 100644
--- a/main.go
+++ b/main.go
@@ -3,0 +4,2 @@ import (
+ "bytes"
+ "crypto/sha256"
@@ -4,0 +7 @@ import (
+ "encoding/json"
@@ -18 +21 @@ import (
- marginblock "grim/cstyle/transformers/margin-block"
+ // marginblock "grim/cstyle/transformers/margin-block"
@@ -27,0 +31 @@ import (
+ "time"
@@ -74 +78 @@ func (window *Window) Path(path string) {
- open(window)
+ // window.Document = *window.Document
@@ -77 +81 @@ func (window *Window) Path(path string) {
-func New(adapterFunction *adapter.Adapter, width, height int) Window {
+func New(adapterFunction *adapter.Adapter) Window {
@@ -79,2 +83,2 @@ func New(adapterFunction *adapter.Adapter, width, height int) Window {
- Width: float32(width),
- Height: float32(height),
+ Width: 800,
+ Height: 450,
@@ -95 +99 @@ func New(adapterFunction *adapter.Adapter, width, height int) Window {
- css.AddTransformer(marginblock.Init())
+ // css.AddTransformer(marginblock.Init())
@@ -103,2 +107,2 @@ func New(adapterFunction *adapter.Adapter, width, height int) Window {
- document.CStyle["width"] = strconv.Itoa(width)+"px"
- document.CStyle["height"] = strconv.Itoa(height)+"px"
+ document.CStyle["width"] = "800px"
+ document.CStyle["height"] = "450px"
@@ -208 +212 @@ func flatten(n *element.Node) []*element.Node {
-func open(data *Window) {
+func Open(data *Window, width, height int) {
@@ -215,2 +219,2 @@ func open(data *Window) {
- data.document.CStyle["width"] = strconv.Itoa(int(data.CSS.Width)) + "px"
- data.document.CStyle["height"] = strconv.Itoa(int(data.CSS.Height)) + "px"
+ data.document.CStyle["width"] = strconv.Itoa(int(width)) + "px"
+ data.document.CStyle["height"] = strconv.Itoa(int(height)) + "px"
@@ -219 +223 @@ func open(data *Window) {
- data.CSS.Adapter.Init(int(data.CSS.Width), int(data.CSS.Height))
+ data.CSS.Adapter.Init(width, height)
@@ -223,2 +227,2 @@ func open(data *Window) {
- Width: float32(data.CSS.Width),
- Height: float32(data.CSS.Height),
+ Width: float32(width),
+ Height: float32(height),
@@ -228,0 +233 @@ func open(data *Window) {
+ var hash []byte
@@ -240,0 +246 @@ func open(data *Window) {
+ newWidth, newHeight := width, height
@@ -257,7 +263,2 @@ func open(data *Window) {
-
- data.CSS.Width = float32(wh["width"])
- data.CSS.Height = float32(wh["height"])
-
- data.document.CStyle["width"] = strconv.Itoa(wh["width"]) + "px"
- data.document.CStyle["height"] = strconv.Itoa(wh["height"]) + "px"
- rd = getRenderData(data, &state, &shelf, &monitor)
+ newWidth = wh["width"]
+ newHeight = wh["height"]
@@ -285,7 +286,2 @@ func open(data *Window) {
- if pos[0] > 0 && pos[1] > 0 {
- if pos[0] < int(data.CSS.Width) && pos[1] < int(data.CSS.Height) {
- currentEvent.Position = pos
- monitor.GetEvents(¤tEvent)
- rd = getRenderData(data, &state, &shelf, &monitor)
- }
- }
+ currentEvent.Position = pos
+ monitor.GetEvents(¤tEvent)
@@ -298 +293,0 @@ func open(data *Window) {
- rd = getRenderData(data, &state, &shelf, &monitor)
@@ -304 +298,0 @@ func open(data *Window) {
- rd = getRenderData(data, &state, &shelf, &monitor)
@@ -310 +303,0 @@ func open(data *Window) {
- rd = getRenderData(data, &state, &shelf, &monitor)
@@ -329,3 +321,0 @@ func open(data *Window) {
- rd = getRenderData(data, &state, &shelf, &monitor)
- // !TODO: Move to adapter
-
@@ -337,4 +327 @@ func open(data *Window) {
-
- data.CSS.Adapter.Render(rd)
- }
-}
+ resize := false
@@ -342,7 +329,14 @@ func open(data *Window) {
-// !TODO: This need to be better implemented but rn just testing
-func getRenderData(data *Window, state *map[string]element.State, shelf *library.Shelf, monitor *events.Monitor) []element.State {
- (*state)["ROOT"] = element.State{
- Width: float32(data.CSS.Width),
- Height: float32(data.CSS.Height),
- }
- fmt.Println("_______________________")
+ if newWidth != width || newHeight != height {
+ resize = true
+ // Window has been resized, handle the event
+ width = newWidth
+ height = newHeight
+
+ data.CSS.Width = float32(width)
+ data.CSS.Height = float32(height)
+
+ data.document.CStyle["width"] = strconv.Itoa(int(width)) + "px"
+ data.document.CStyle["height"] = strconv.Itoa(int(height)) + "px"
+ }
+
+ newHash, _ := hashStruct(&data.document.Children[0])
@@ -350,2 +344,5 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- // !ISSUE: Move this inside of ComputeNodeStyle
- // + This modify events to return effected nodes
+ if !bytes.Equal(hash, newHash) || resize {
+ hash = newHash
+ fmt.Println("----------------------------------")
+ s := time.Now()
+ newDoc := AddStyles(data.CSS, data.document.Children[0], &data.document)
@@ -353,8 +350 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- dc := data.document.Children[0]
- // start := time.Now()
- newDoc := AddStyles(data.CSS, dc, &data.document)
- // fmt.Println(time.Since(start))
-
- data.CSS.ComputeNodeStyle(newDoc, state)
-
- rd := data.Render(newDoc, state, shelf)
+ newDoc = data.CSS.Transform(newDoc)
@@ -362 +352,4 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- data.CSS.Adapter.Load(rd)
+ state["ROOT"] = element.State{
+ Width: float32(width),
+ Height: float32(height),
+ }
@@ -364,2 +357 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- AddHTMLAndAttrs(&data.document, state)
- // fmt.Println(data.document.InnerHTML)
+ data.CSS.ComputeNodeStyle(newDoc, &state)
@@ -367,2 +359 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- data.Scripts.Run(&data.document)
- shelf.Clean()
+ rd = data.Render(newDoc, &state, &shelf)
@@ -370,5 +361 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
- // !TODO: Should return effected node, then render those specific
- // + I think have node.ComputeNodeStyle would make this nice
- monitor.RunEvents(data.document.Children[0])
- return rd
-}
+ data.CSS.Adapter.Load(rd)
@@ -375,0 +363,13 @@ func getRenderData(data *Window, state *map[string]element.State, shelf *library
+ AddHTMLAndAttrs(&data.document, &state)
+ // AddHTMLAndAttrs(newDoc, &state)
+ // fmt.Println(newDoc.OuterHTML)
+ data.Scripts.Run(&data.document)
+ shelf.Clean()
+ elapsed := time.Since(s)
+ fmt.Printf("Execution time: %s\n", elapsed)
+ }
+
+ monitor.RunEvents(data.document.Children[0])
+ data.CSS.Adapter.Render(rd)
+ }
+}
@@ -380 +380 @@ func AddStyles(c cstyle.CSS, node *element.Node, parent *element.Node) *element.
- // !DEVMAN: Copying is done here, would like to remove this and add it to ComputeNodeStyle, so I can save a tree climb
+
@@ -434 +433,0 @@ func CreateNode(node *html.Node, parent *element.Node) {
-
@@ -455 +453,0 @@ func AddHTMLAndAttrs(n *element.Node, state *map[string]element.State) {
-
@@ -459 +456,0 @@ func AddHTMLAndAttrs(n *element.Node, state *map[string]element.State) {
-
@@ -578 +575 @@ func matchFactory(re *regexp.Regexp) func(string) string {
- return submatches[1] + "" + submatches[2] + "" + submatches[3]
+ return submatches[1] + "" + submatches[2] + "" + submatches[3]
@@ -607,0 +605,15 @@ func removeWhitespaceBetweenTags(html string) string {
+// Function to hash a struct using SHA-256
+func hashStruct(s interface{}) ([]byte, error) {
+ // Convert struct to JSON
+ jsonData, err := json.Marshal(s)
+ if err != nil {
+ return nil, err
+ }
+
+ // Hash the JSON data using SHA-256
+ hasher := sha256.New()
+ hasher.Write(jsonData)
+ hash := hasher.Sum(nil)
+
+ return hash, nil
+}
package grim
import (
_ "embed"
"fmt"
adapter "grim/adapters"
"grim/canvas"
"grim/cstyle"
"grim/cstyle/plugins/crop"
"grim/cstyle/plugins/flex"
"grim/cstyle/plugins/inline"
"grim/cstyle/plugins/textAlign"
"grim/cstyle/transformers/background"
"grim/cstyle/transformers/banda"
flexprep "grim/cstyle/transformers/flex"
img "grim/cstyle/transformers/image"
marginblock "grim/cstyle/transformers/margin-block"
"grim/cstyle/transformers/ol"
"grim/cstyle/transformers/scrollbar"
"grim/cstyle/transformers/text"
"grim/cstyle/transformers/ul"
"grim/font"
"grim/library"
"grim/scripts"
"grim/scripts/a"
"image"
"grim/element"
"grim/events"
"grim/utils"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
imgFont "golang.org/x/image/font"
"golang.org/x/net/html"
)
//go:embed master.css
var mastercss string
type Window struct {
CSS cstyle.CSS
document element.Node
Scripts scripts.Scripts
}
func (w *Window) Document() *element.Node {
return w.document.Children[0]
}
// !TODO: Add a Mux option to all a http server to map to the window
func (window *Window) HttpMux() {}
func (window *Window) Path(path string) {
styleSheets, styleTags, htmlNodes := parseHTMLFromFile(path, window.CSS.Adapter.FileSystem)
for _, v := range styleSheets {
window.CSS.StyleSheet(v)
}
for _, v := range styleTags {
window.CSS.StyleTag(v)
}
window.CSS.Path = filepath.Dir(path)
CreateNode(htmlNodes, &window.document)
open(window)
}
func New(adapterFunction *adapter.Adapter, width, height int) Window {
css := cstyle.CSS{
Width: float32(width),
Height: float32(height),
Adapter: adapterFunction,
}
css.StyleTag(mastercss)
// This is still apart of computestyle
css.AddPlugin(inline.Init())
css.AddPlugin(textAlign.Init())
css.AddPlugin(flex.Init())
css.AddPlugin(crop.Init())
css.AddTransformer(text.Init())
css.AddTransformer(banda.Init())
css.AddTransformer(scrollbar.Init())
css.AddTransformer(flexprep.Init())
css.AddTransformer(marginblock.Init())
css.AddTransformer(ul.Init())
css.AddTransformer(ol.Init())
css.AddTransformer(background.Init())
css.AddTransformer(img.Init())
el := element.Node{}
document := el.CreateElement("ROOT")
document.CStyle["width"] = strconv.Itoa(width)+"px"
document.CStyle["height"] = strconv.Itoa(height)+"px"
document.Properties.Id = "ROOT"
s := scripts.Scripts{}
s.Add(a.Init())
return Window{
CSS: css,
document: document,
Scripts: s,
}
}
func (w *Window) Render(doc *element.Node, state *map[string]element.State, shelf *library.Shelf) []element.State {
s := *state
flatDoc := flatten(doc)
store := []element.State{}
keys := []string{}
for _, v := range flatDoc {
store = append(store, s[v.Properties.Id])
keys = append(keys, v.Properties.Id)
}
// Create a set of keys to keep
keysSet := make(map[string]struct{}, len(keys))
for _, key := range keys {
keysSet[key] = struct{}{}
}
// Iterate over the map and delete keys not in the set
for k := range s {
if _, found := keysSet[k]; !found {
delete(s, k)
}
}
for k, self := range store {
// Option: Have Grim render all elements
wbw := int(self.Width + self.Border.Left.Width + self.Border.Right.Width)
hbw := int(self.Height + self.Border.Top.Width + self.Border.Bottom.Width)
key := strconv.Itoa(wbw) + strconv.Itoa(hbw) + utils.RGBAtoString(self.Background)
exists := shelf.Check(key)
bounds := shelf.Bounds(key)
// fmt.Println(n.Properties.Id, self.Width, self.Height, bounds)
if exists && bounds[0] == int(wbw) && bounds[1] == int(hbw) {
lookup := make(map[string]struct{}, len(self.Textures))
for _, v := range self.Textures {
lookup[v] = struct{}{}
}
if _, found := lookup[key]; !found {
self.Textures = append([]string{key}, self.Textures...)
store[k] = self
}
} else if self.Background.A > 0 {
lookup := make(map[string]struct{}, len(self.Textures))
for _, v := range self.Textures {
lookup[v] = struct{}{}
}
if _, found := lookup[key]; !found {
// Only make the drawing if it's not found
can := canvas.NewCanvas(wbw, hbw)
can.BeginPath()
can.SetFillStyle(self.Background.R, self.Background.G, self.Background.B, self.Background.A)
can.SetLineWidth(10)
can.RoundedRect(0, 0, float64(wbw), float64(hbw),
[]float64{float64(self.Border.Radius.TopLeft), float64(self.Border.Radius.TopRight), float64(self.Border.Radius.BottomRight), float64(self.Border.Radius.BottomLeft)})
can.Fill()
can.ClosePath()
shelf.Set(key, can.RGBA)
self.Textures = append([]string{key}, self.Textures...)
store[k] = self
}
}
}
return store
}
func flatten(n *element.Node) []*element.Node {
var nodes []*element.Node
nodes = append(nodes, n)
children := n.Children
if len(children) > 0 {
for _, ch := range children {
chNodes := flatten(ch)
nodes = append(nodes, chNodes...)
}
}
return nodes
}
// !ISSUE: Probally don't need this to be exposed to the outside, if using getter/setter, just render once content is loaded then everytime a event
// + or content update
func open(data *Window) {
shelf := library.Shelf{
Textures: map[string]*image.RGBA{},
References: map[string]bool{},
}
debug := false
data.document.CStyle["width"] = strconv.Itoa(int(data.CSS.Width)) + "px"
data.document.CStyle["height"] = strconv.Itoa(int(data.CSS.Height)) + "px"
data.CSS.Adapter.Library = &shelf
data.CSS.Adapter.Init(int(data.CSS.Width), int(data.CSS.Height))
state := map[string]element.State{}
state["ROOT"] = element.State{
Width: float32(data.CSS.Width),
Height: float32(data.CSS.Height),
}
shouldStop := false
var rd []element.State
// Load init font
if data.CSS.Fonts == nil {
data.CSS.Fonts = map[string]imgFont.Face{}
}
fid := "Georgia 16px false false"
if data.CSS.Fonts[fid] == nil {
f, _ := font.LoadFont("Georgia", 16, "", false, &data.CSS.Adapter.FileSystem)
data.CSS.Fonts[fid] = f
}
monitor := events.Monitor{
EventMap: make(map[string]element.Event),
Adapter: data.CSS.Adapter,
State: &state,
CSS: &data.CSS,
Focus: events.Focus{
Nodes: []string{},
Selected: -1,
SoftFocused: "",
LastClickWasFocused: false,
},
}
data.CSS.Adapter.AddEventListener("windowresize", func(e element.Event) {
wh := e.Data.(map[string]int)
data.CSS.Width = float32(wh["width"])
data.CSS.Height = float32(wh["height"])
data.document.CStyle["width"] = strconv.Itoa(wh["width"]) + "px"
data.document.CStyle["height"] = strconv.Itoa(wh["height"]) + "px"
rd = getRenderData(data, &state, &shelf, &monitor)
})
data.CSS.Adapter.AddEventListener("close", func(e element.Event) {
shouldStop = true
})
currentEvent := events.EventData{}
data.CSS.Adapter.AddEventListener("keydown", func(e element.Event) {
currentEvent.Key = e.Data.(int)
currentEvent.KeyState = true
monitor.GetEvents(¤tEvent)
})
data.CSS.Adapter.AddEventListener("keyup", func(e element.Event) {
currentEvent.Key = 0
currentEvent.KeyState = false
monitor.GetEvents(¤tEvent)
})
data.CSS.Adapter.AddEventListener("mousemove", func(e element.Event) {
pos := e.Data.([]int)
if pos[0] > 0 && pos[1] > 0 {
if pos[0] < int(data.CSS.Width) && pos[1] < int(data.CSS.Height) {
currentEvent.Position = pos
monitor.GetEvents(¤tEvent)
rd = getRenderData(data, &state, &shelf, &monitor)
}
}
})
data.CSS.Adapter.AddEventListener("scroll", func(e element.Event) {
currentEvent.ScrollY = e.Data.(int)
monitor.GetEvents(¤tEvent)
currentEvent.ScrollY = 0
rd = getRenderData(data, &state, &shelf, &monitor)
})
data.CSS.Adapter.AddEventListener("mousedown", func(e element.Event) {
currentEvent.Click = true
monitor.GetEvents(¤tEvent)
rd = getRenderData(data, &state, &shelf, &monitor)
})
data.CSS.Adapter.AddEventListener("mouseup", func(e element.Event) {
currentEvent.Click = false
monitor.GetEvents(¤tEvent)
rd = getRenderData(data, &state, &shelf, &monitor)
})
data.CSS.Adapter.AddEventListener("contextmenudown", func(e element.Event) {
currentEvent.Context = true
monitor.GetEvents(¤tEvent)
})
data.CSS.Adapter.AddEventListener("contextmenuup", func(e element.Event) {
currentEvent.Context = true
monitor.GetEvents(¤tEvent)
})
// !ISSUE: the loop should be moved to the adapter and the rerendering should only happen if a eventlistener goes off
// + also have a animation loop seperate but thats later
// + really should run runevents after get events and if there is any changes then rerender
// + ahh what about dom changes in the js api...
// + could swap to getters and setters but i don't like them
// Main game loop
rd = getRenderData(data, &state, &shelf, &monitor)
// !TODO: Move to adapter
for !shouldStop {
if !shouldStop && debug {
shouldStop = true
}
// Check if the window size has changed
data.CSS.Adapter.Render(rd)
}
}
// !TODO: This need to be better implemented but rn just testing
func getRenderData(data *Window, state *map[string]element.State, shelf *library.Shelf, monitor *events.Monitor) []element.State {
(*state)["ROOT"] = element.State{
Width: float32(data.CSS.Width),
Height: float32(data.CSS.Height),
}
fmt.Println("_______________________")
// !ISSUE: Move this inside of ComputeNodeStyle
// + This modify events to return effected nodes
dc := data.document.Children[0]
// start := time.Now()
newDoc := AddStyles(data.CSS, dc, &data.document)
// fmt.Println(time.Since(start))
data.CSS.ComputeNodeStyle(newDoc, state)
rd := data.Render(newDoc, state, shelf)
data.CSS.Adapter.Load(rd)
AddHTMLAndAttrs(&data.document, state)
// fmt.Println(data.document.InnerHTML)
data.Scripts.Run(&data.document)
shelf.Clean()
// !TODO: Should return effected node, then render those specific
// + I think have node.ComputeNodeStyle would make this nice
monitor.RunEvents(data.document.Children[0])
return rd
}
func AddStyles(c cstyle.CSS, node *element.Node, parent *element.Node) *element.Node {
n := *node
n.Parent = parent
// !DEVMAN: Copying is done here, would like to remove this and add it to ComputeNodeStyle, so I can save a tree climb
n.CStyle, n.PseudoElements = c.GetStyles(&n)
if len(node.Children) > 0 {
n.Children = make([]*element.Node, 0, len(node.Children))
// n.Children = append(n.Children, node.Children...)
// for _, v := range node.Children {
for i := 0; i < len(node.Children); i++ {
n.Children = append(n.Children, node.Children[i])
}
for i := 0; i < len(node.Children); i++ {
n.Children[i] = AddStyles(c, node.Children[i], &n)
}
}
return &n
}
func CreateNode(node *html.Node, parent *element.Node) {
if node.Type == html.ElementNode {
newNode := parent.CreateElement(node.Data)
for _, attr := range node.Attr {
switch attr.Key {
case "class":
classes := strings.Split(attr.Val, " ")
for _, class := range classes {
newNode.ClassList.Add(class)
}
case "id":
newNode.Id = attr.Val
case "contenteditable":
if attr.Val == "" || attr.Val == "true" {
newNode.ContentEditable = true
}
case "href":
newNode.Href = attr.Val
case "src":
newNode.Src = attr.Val
case "title":
newNode.Title = attr.Val
case "tabindex":
newNode.TabIndex, _ = strconv.Atoi(attr.Val)
case "disabled":
newNode.Disabled = true
case "required":
newNode.Required = true
case "checked":
newNode.Checked = true
default:
newNode.SetAttribute(attr.Key, attr.Val)
}
}
newNode.InnerText = strings.TrimSpace(utils.GetInnerText(node))
// Recursively traverse child nodes
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type == html.ElementNode {
CreateNode(child, &newNode)
}
}
parent.AppendChild(&newNode)
} else {
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type == html.ElementNode {
CreateNode(child, parent)
}
}
}
}
func AddHTMLAndAttrs(n *element.Node, state *map[string]element.State) {
// Head is not renderable
s := (*state)
n.InnerHTML = utils.InnerHTML(n)
tag, closing := utils.NodeToHTML(n)
n.OuterHTML = tag + n.InnerHTML + closing
// !NOTE: This is the only spot you can pierce the vale
n.ScrollHeight = s[n.Properties.Id].ScrollHeight
n.ScrollWidth = s[n.Properties.Id].ScrollWidth
for i := range n.Children {
AddHTMLAndAttrs(n.Children[i], state)
}
}
func parseHTMLFromFile(path string, fs adapter.FileSystem) ([]string, []string, *html.Node) {
file, _ := fs.ReadFile(path)
htmlContent := removeHTMLComments(string(file))
doc, _ := html.Parse(strings.NewReader(encapsulateText(removeWhitespaceBetweenTags(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
for _, attr := range node.Attr {
if attr.Key == "rel" && attr.Val == "stylesheet" {
isStylesheet = true
} else if attr.Key == "href" {
href = attr.Val
}
}
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)
// If the absolute path is the same as the original path, return it
if absPath == filePath {
return filePath
}
return "./" + absPath
}
func encapsulateText(htmlString string) string {
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
}
func matchFactory(re *regexp.Regexp) func(string) string {
return func(match string) string {
submatches := re.FindStringSubmatch(match)
if len(submatches) != 4 {
return match
}
// 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
}
func removeHTMLComments(htmlString string) string {
re := regexp.MustCompile(``)
return re.ReplaceAllString(htmlString, "")
}
// important to allow the notspans to be injected, the spaces after removing the comments cause the regexp to fail
func removeWhitespaceBetweenTags(html string) string {
// Create a regular expression to match spaces between angle brackets
re := regexp.MustCompile(`>\s+<`)
// Replace all matches of spaces between angle brackets with "><"
return re.ReplaceAllString(html, "><")
}