Skip to content

Commit

Permalink
Revert "optimize html convert"
Browse files Browse the repository at this point in the history
This reverts commit 77cb2b3.
  • Loading branch information
maxence-charriere committed Jun 17, 2022
1 parent a357ee8 commit ec0bfc4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 88 deletions.
69 changes: 31 additions & 38 deletions pkg/app/html.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"bufio"
"context"
"io"
"net/url"
Expand Down Expand Up @@ -330,95 +329,89 @@ func (e *htmlElement) preRender(p Page) {
}

func (e *htmlElement) html(w io.Writer) {
bw := bufio.NewWriter(w)
defer bw.Flush()

bw.WriteString("<")
bw.WriteString(e.tag)
w.Write([]byte("<"))
w.Write([]byte(e.tag))

for k, v := range e.attributes {
bw.WriteString(" ")
bw.WriteString(k)
w.Write([]byte(" "))
w.Write([]byte(k))

if v != "" {
bw.WriteString(`="`)
bw.WriteString(resolveAttributeURLValue(k, v, func(s string) string {
w.Write([]byte(`="`))
w.Write([]byte(resolveAttributeURLValue(k, v, func(s string) string {
if e.dispatcher != nil {
return e.dispatcher.resolveStaticResource(v)
}
return v
}))
bw.WriteString(`"`)
})))
w.Write([]byte(`"`))
}
}

bw.WriteString(">")
w.Write([]byte(">"))

if e.isSelfClosing {
return
}

for _, c := range e.children {
bw.WriteString("n")
w.Write(ln())
if c.self() == nil {
c.setSelf(c)
}
c.html(bw)
c.html(w)
}

if len(e.children) != 0 {
bw.WriteString("\n")
w.Write(ln())
}

bw.WriteString("</")
bw.WriteString(e.tag)
bw.WriteString(">")
w.Write([]byte("</"))
w.Write([]byte(e.tag))
w.Write([]byte(">"))
}

func (e *htmlElement) htmlWithIndent(w io.Writer, indent int) {
bw := bufio.NewWriter(w)
defer bw.Flush()

writeIndent(w, indent)
bw.WriteString("<")
bw.WriteString(e.tag)
w.Write([]byte("<"))
w.Write([]byte(e.tag))

for k, v := range e.attributes {
bw.WriteString(" ")
bw.WriteString(k)
w.Write([]byte(" "))
w.Write([]byte(k))

if v != "" {
bw.WriteString(`="`)
bw.WriteString(resolveAttributeURLValue(k, v, func(s string) string {
w.Write([]byte(`="`))
w.Write([]byte(resolveAttributeURLValue(k, v, func(s string) string {
if e.dispatcher != nil {
return e.dispatcher.resolveStaticResource(v)
}
return v
}))
bw.WriteString(`"`)
})))
w.Write([]byte(`"`))
}
}

bw.WriteString(">")
w.Write([]byte(">"))

if e.isSelfClosing {
return
}

for _, c := range e.children {
bw.WriteString("\n")
w.Write(ln())
if c.self() == nil {
c.setSelf(c)
}
c.htmlWithIndent(bw, indent+1)
c.htmlWithIndent(w, indent+1)
}

if len(e.children) != 0 {
bw.WriteString("\n")
writeIndent(bw, indent)
w.Write(ln())
writeIndent(w, indent)
}

bw.WriteString("</")
bw.WriteString(e.tag)
bw.WriteString(">")
w.Write([]byte("</"))
w.Write([]byte(e.tag))
w.Write([]byte(">"))
}
45 changes: 0 additions & 45 deletions pkg/app/html_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"bytes"
"fmt"
"testing"
)
Expand All @@ -25,47 +24,3 @@ func BenchmarkMountHTMLElement(b *testing.B) {
client.Close()
}
}

func BenchmarkHTMLElementHTML(b *testing.B) {
div := Div().
Class("shell").
Body(
H1().Class("title").
Text("Hello"),
Input().
Type("text").
Class("in").
Value("World").
Placeholder("Type a name.").
OnChange(func(ctx Context, e Event) {
fmt.Println("Yo!")
}),
)

for n := 0; n < b.N; n++ {
var bytes bytes.Buffer
div.html(&bytes)
}
}

func BenchmarkHTMLElementHTMLIndent(b *testing.B) {
div := Div().
Class("shell").
Body(
H1().Class("title").
Text("Hello"),
Input().
Type("text").
Class("in").
Value("World").
Placeholder("Type a name.").
OnChange(func(ctx Context, e Event) {
fmt.Println("Yo!")
}),
)

for n := 0; n < b.N; n++ {
var bytes bytes.Buffer
div.htmlWithIndent(&bytes, 0)
}
}
6 changes: 1 addition & 5 deletions pkg/app/strings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"bufio"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -53,11 +52,8 @@ func toPath(v ...any) string {
}

func writeIndent(w io.Writer, indent int) {
bw := bufio.NewWriter(w)
defer bw.Flush()

for i := 0; i < indent*2; i++ {
bw.WriteString(" ")
w.Write([]byte(" "))
}
}

Expand Down

0 comments on commit ec0bfc4

Please sign in to comment.