ref: 613e3bb3449b423ec3d96438f8b9a93aa58c731b
parent: 8bafb50f34edce70de96005a21dfb73a400852bd
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Dec 25 20:10:32 EST 2020
More terse code
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -29,6 +29,8 @@
const stashElements = true
const experimentalUseSlicedDrawing = false
+var cursor = [2 * 16]uint8{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 90, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
+
var DebugDumpCSS *bool
var ExperimentalJsInsecure *bool
var EnableNoScriptTag *bool
@@ -277,24 +279,36 @@
}
func NewSubmitButton(b *Browser, n *nodes.Node) *Element {
- t := attr(*n.DomSubtree, "value")
- if t == "" {
+ var t string
+
+ if v := attr(*n.DomSubtree, "value"); v != "" {
+ t = v
+ } else if nodes.IsPureTextContent(*n) {
+ t = strings.TrimSpace(nodes.ContentFrom(*n))
+ } else {
t = "Submit"
}
+
+ click := func() (r duit.Event) {
+ f := n.Ancestor("form")
+
+ if f == nil {
+ return
+ }
+
+ b.submit(f, n)
+
+ return duit.Event{
+ Consumed: true,
+ NeedLayout: true,
+ NeedDraw: true,
+ }
+ }
+
btn := &duit.Button{
Text: t,
Font: n.Font(),
- Click: func() (r duit.Event) {
- if f := n.Ancestor("form"); f != nil {
- b.submit(f.DomSubtree, n.DomSubtree)
- return duit.Event{
- Consumed: true,
- NeedLayout: true,
- NeedDraw: true,
- }
- }
- return
- },
+ Click: click,
}
return NewElement(btn, n)
}
@@ -315,7 +329,7 @@
},
Keys: func(k rune, m draw.Mouse) (e duit.Event) {
if k == 10 {
- browser.submit(n.Ancestor("form").DomSubtree, nil)
+ browser.submit(n.Ancestor("form"), nil)
return duit.Event{
Consumed: true,
NeedLayout: true,
@@ -335,52 +349,65 @@
if el == nil {
return
}
- if m.Buttons == 1 {
- if el.Click != nil {
- el.Click()
- } else {
- if *ExperimentalJsInsecure {
- res, changed, err := browser.Website.d.TriggerClick(el.n.QueryRef())
- if changed && err == nil {
- browser.Website.html = res
- browser.Website.layout(browser)
- dui.MarkLayout(dui.Top.UI)
- dui.MarkDraw(dui.Top.UI)
- dui.Render()
- return duit.Result{
- Consumed: true,
- }
- }
+ if m.Buttons == 1 {
+ if el.click() {
+ return duit.Result{
+ Consumed: true,
}
}
}
+
x := m.Point.X
y := m.Point.Y
maxX := self.R.Dx()
maxY := self.R.Dy()
- if 5 <= x && x <= (maxX-5) && 5 <= y && y <= (maxY-5) {
- //log.Printf("Mouse %v (m ~ %v); Kid.R.Dx/Dy=%v/%v\n", el.UI, m.Point, self.R.Dx(), self.R.Dy())
- if el.IsLink {
- yolo := [2 * 16]uint8{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 90, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
- dui.Display.SetCursor(&draw.Cursor{
- Set: yolo,
- })
- if m.Buttons == 0 {
- r.Consumed = true
- return r
- }
+ if 5 <= x && x <= (maxX-5) && 5 <= y && y <= (maxY-5) && el.IsLink {
+ dui.Display.SetCursor(&draw.Cursor{
+ Set: cursor,
+ })
+
+ if m.Buttons == 0 {
+ r.Consumed = true
+ return r
}
} else {
- if el.IsLink {
- dui.Display.SetCursor(nil)
- } else {
- dui.Display.SetCursor(nil)
- }
+ dui.Display.SetCursor(nil)
}
+
return el.UI.Mouse(dui, self, m, origM, orig)
}
+func (el *Element) click() (consumed bool) {
+ if el.Click != nil {
+ el.Click()
+ return
+ }
+
+ if !*ExperimentalJsInsecure {
+ return
+ }
+
+ q := el.n.QueryRef()
+ res, consumed, err := browser.Website.d.TriggerClick(q)
+ if err != nil {
+ log.Errorf("trigger click %v: %v", q, err)
+ return
+ }
+
+ if !consumed {
+ return
+ }
+
+ browser.Website.html = res
+ browser.Website.layout(browser)
+ dui.MarkLayout(dui.Top.UI)
+ dui.MarkDraw(dui.Top.UI)
+ dui.Render()
+
+ return
+}
+
// makeLink of el and its children
func (el *Element) makeLink(href string) {
if href == "" || strings.HasPrefix(href, "#") || strings.Contains(href, "javascript:void(0)") {
@@ -438,9 +465,9 @@
func Arrange(n *nodes.Node, elements ...*Element) *Element {
if n.IsFlex() {
if n.IsFlexDirectionRow() {
- return NewElement(horizontalSequenceOf(true, elements), n)
+ return NewElement(horizontalSeq(true, elements), n)
} else {
- return NewElement(verticalSequenceOf(elements), n)
+ return NewElement(verticalSeq(elements), n)
}
}
@@ -474,20 +501,20 @@
return rows[0][0]
}
numElements++
- return NewElement(horizontalSequenceOf(true, rows[0]), n)
+ return NewElement(horizontalSeq(true, rows[0]), n)
} else {
seqs := make([]*Element, 0, len(rows))
for _, row := range rows {
- seq := horizontalSequenceOf(true, row)
+ seq := horizontalSeq(true, row)
numElements++
seqs = append(seqs, NewElement(seq, n))
}
numElements++
- return NewElement(verticalSequenceOf(seqs), n)
+ return NewElement(verticalSeq(seqs), n)
}
}
-func horizontalSequenceOf(wrap bool, es []*Element) duit.UI {
+func horizontalSeq(wrap bool, es []*Element) duit.UI {
if len(es) == 0 {
return nil
} else if len(es) == 1 {
@@ -547,7 +574,7 @@
}
}
-func verticalSequenceOf(es []*Element) duit.UI {
+func verticalSeq(es []*Element) duit.UI {
if len(es) == 0 {
return nil
} else if len(es) == 1 {
@@ -701,13 +728,13 @@
log.Printf("len rowsEls=%v", len(rowEls))
if len(rowEls) > 0 {
- seq := horizontalSequenceOf(false, rowEls)
+ seq := horizontalSeq(false, rowEls)
numElements++
seqs = append(seqs, NewElement(seq, row.n))
}
}
numElements++
- return NewElement(verticalSequenceOf(seqs), n)
+ return NewElement(verticalSeq(seqs), n)
}
}
--- a/browser/website.go
+++ b/browser/website.go
@@ -159,17 +159,17 @@
log.Flush()
}
-func formData(n, submitBtn *html.Node) (data url.Values) {
+func formData(n, submitBtn *nodes.Node) (data url.Values) {
data = make(url.Values)
- if n.Data == "input" {
- if attr(*n, "type") == "submit" && n != submitBtn {
+ if n.Data() == "input" {
+ if n.Attr("type") == "submit" && (submitBtn == nil || n.DomSubtree != submitBtn.DomSubtree) {
return
}
- if k := attr(*n, "name"); k != "" {
- data.Set(k, attr(*n, "value"))
+ if k := n.Attr("name"); k != "" {
+ data.Set(k, n.Attr("value"))
}
}
- for c := n.FirstChild; c != nil; c = c.NextSibling {
+ for _, c := range n.Children {
for k, vs := range formData(c, submitBtn) {
data.Set(k, vs[0]) // TODO: what aboot the rest?
}
@@ -177,15 +177,18 @@
return
}
-func (b *Browser) submit(form *html.Node, submitBtn *html.Node) {
+func (b *Browser) submit(form, submitBtn *nodes.Node) {
var err error
+ var buf []byte
+ var contentType opossum.ContentType
+
method := "GET" // TODO
- if m := attr(*form, "method"); m != "" {
+ if m := form.Attr("method"); m != "" {
method = strings.ToUpper(m)
}
+
uri := b.URL()
- log.Printf("form = %+v", form)
- if action := attr(*form, "action"); action != "" {
+ if action := form.Attr("action"); action != "" {
uri, err = b.LinkedUrl(action)
if err != nil {
log.Printf("error parsing %v", action)
@@ -192,28 +195,28 @@
return
}
}
- var buf []byte
- var contentType opossum.ContentType
+
+
if method == "GET" {
q := uri.Query()
for k, vs := range formData(form, submitBtn) {
- log.Printf("add query info %v => %v", k, vs[0])
q.Set(k, vs[0]) // TODO: what is with the rest?
}
uri.RawQuery = q.Encode()
- log.Printf("uri raw query=%v", uri.RawQuery)
buf, contentType, err = b.get(uri, true)
- log.Printf("uri=%v", uri.String())
} else {
buf, contentType, err = b.PostForm(uri, formData(form, submitBtn))
}
- if err == nil {
- if contentType.IsHTML() {
- b.render(buf)
- } else {
- log.Fatalf("post: unexpected %v", contentType)
- }
- } else {
- log.Printf("post form: %v", err)
+
+ if err != nil {
+ log.Errorf("submit form: %v", err)
+ return
}
+
+ if !contentType.IsHTML() {
+ log.Errorf("post: unexpected %v", contentType)
+ return
+ }
+
+ b.render(buf)
}