Browse Source

update element

fancl 2 years ago
parent
commit
1c3db6db2c
5 changed files with 49 additions and 46 deletions
  1. 3 3
      element/button.go
  2. 42 0
      element/element.go
  3. 0 40
      element/interface.go
  4. 2 2
      element/text.go
  5. 2 1
      render_test.go

+ 3 - 3
element/button.go

@@ -27,9 +27,9 @@ func (element *Button) String() string {
 		element.Style["color"] = element.Color
 	}
 	return NewElement(atom.A, element.Text,
-		WithTagAttribute("class", "button"),
-		WithTagAttribute("href", element.Url),
-		WithTagAttribute("style", element.Style.String()),
+		WithAttribute("class", "button"),
+		WithAttribute("href", element.Url),
+		WithAttribute("style", element.Style.String()),
 	).String()
 }
 

+ 42 - 0
element/element.go

@@ -2,14 +2,24 @@ package element
 
 import (
 	"git.nspix.com/golang/pgenr/internal/pool"
+	"golang.org/x/net/html/atom"
+	"html"
 )
 
 type (
+	Option func(t *element)
+
 	Element interface {
 		Html() string
 	}
 
 	Attrs map[string]string
+
+	element struct {
+		Name    atom.Atom
+		Attr    Attrs
+		Content string
+	}
 )
 
 func renderTag(tag string, attrs Attrs, content string) string {
@@ -18,6 +28,9 @@ func renderTag(tag string, attrs Attrs, content string) string {
 	br.WriteString("<" + tag)
 	if len(attrs) > 0 {
 		for k, v := range attrs {
+			if atom.Lookup([]byte(k)) == 0 {
+				continue
+			}
 			br.WriteString(" " + k + "=\"" + v + "\" ")
 		}
 	}
@@ -33,6 +46,9 @@ func beginTag(tag string, attrs Attrs) string {
 	br.WriteString("<" + tag)
 	if len(attrs) > 0 {
 		for k, v := range attrs {
+			if atom.Lookup([]byte(k)) == 0 {
+				continue
+			}
 			br.WriteString(" " + k + "=\"" + v + "\" ")
 		}
 	}
@@ -46,3 +62,29 @@ func endTag(tag string) string {
 	br.WriteString("</" + tag + ">")
 	return br.String()
 }
+
+func WithAttribute(label string, value string) Option {
+
+	return func(t *element) {
+		if t.Attr == nil {
+			t.Attr = make(map[string]string)
+		}
+		t.Attr[label] = value
+	}
+}
+
+func (element *element) Html() string {
+	return element.String()
+}
+
+func (element *element) String() string {
+	return renderTag(element.Name.String(), element.Attr, html.EscapeString(element.Content))
+}
+
+func NewElement(atom atom.Atom, content string, opts ...Option) *element {
+	e := &element{Name: atom, Content: content}
+	for _, cb := range opts {
+		cb(e)
+	}
+	return e
+}

+ 0 - 40
element/interface.go

@@ -1,40 +0,0 @@
-package element
-
-import (
-	"golang.org/x/net/html/atom"
-	"html"
-)
-
-type (
-	TagOption func(t *element)
-	element   struct {
-		Name    atom.Atom
-		Attr    Attrs
-		Content string
-	}
-)
-
-func WithTagAttribute(label string, value string) TagOption {
-	return func(t *element) {
-		if t.Attr == nil {
-			t.Attr = make(map[string]string)
-		}
-		t.Attr[label] = value
-	}
-}
-
-func (tag *element) Html() string {
-	return tag.String()
-}
-
-func (tag *element) String() string {
-	return renderTag(tag.Name.String(), tag.Attr, html.EscapeString(tag.Content))
-}
-
-func NewElement(atom atom.Atom, content string, opts ...TagOption) *element {
-	e := &element{Name: atom, Content: content}
-	for _, cb := range opts {
-		cb(e)
-	}
-	return e
-}

+ 2 - 2
element/text.go

@@ -60,8 +60,8 @@ func (text *Text) String() string {
 	return NewElement(
 		text.Tag,
 		text.Content,
-		WithTagAttribute("class", className),
-		WithTagAttribute("style", text.Style.String()),
+		WithAttribute("class", className),
+		WithAttribute("style", text.Style.String()),
 	).String()
 }
 

+ 2 - 1
render_test.go

@@ -3,13 +3,14 @@ package pgenr
 import (
 	"fmt"
 	"git.nspix.com/golang/pgenr/element"
+	"golang.org/x/net/html/atom"
 	"io/ioutil"
 	"testing"
 	"time"
 )
 
 func TestAtom(t *testing.T) {
-	fmt.Println(time.Now().Add(-2 * time.Hour).Unix())
+	fmt.Println(atom.Lookup([]byte("zzz")).String())
 }
 
 func TestRender(t *testing.T) {