shithub: mycel

Download patch

ref: df986b4853ae85c9e12bb773a6293bfcf23a8ea2
parent: 53e30e39a6156b3182cff49af4e9375216e9df77
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Oct 3 10:37:05 EDT 2021

js: track submit events

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -698,32 +698,28 @@
 }
 
 func (el *Element) click() (consumed bool) {
+	if ExperimentalJsInsecure {
+		q := el.n.QueryRef()
+		res, consumed, err := js.TriggerClick(q)
+		if err != nil {
+			log.Errorf("trigger click %v: %v", q, err)
+			return consumed
+		}
+
+		if consumed {
+			offset := scroller.Offset
+			browser.Website.layout(browser, res, ClickRelayout)
+			scroller.Offset = offset
+			dui.MarkLayout(dui.Top.UI)
+			dui.MarkDraw(dui.Top.UI)
+			dui.Render()
+		}
+	}
+
 	if el.Click != nil {
 		e := el.Click()
 		return e.Consumed
 	}
-
-	if !ExperimentalJsInsecure {
-		return
-	}
-
-	q := el.n.QueryRef()
-	res, consumed, err := js.TriggerClick(q)
-	if err != nil {
-		log.Errorf("trigger click %v: %v", q, err)
-		return
-	}
-
-	if !consumed {
-		return
-	}
-
-	offset := scroller.Offset
-	browser.Website.layout(browser, res, ClickRelayout)
-	scroller.Offset = offset
-	dui.MarkLayout(dui.Top.UI)
-	dui.MarkDraw(dui.Top.UI)
-	dui.Render()
 
 	return
 }
--- a/cmd/gojafs/domino/domino-lib/htmlelts.js
+++ b/cmd/gojafs/domino/domino-lib/htmlelts.js
@@ -144,7 +144,9 @@
         this._click_in_progress = false;
       }
     }},
-    submit: { value: utils.nyi },
+    submit: { value: function(a, b, c) {
+      ___opossumSubmit.bind(this)(a, b, c) }
+    },
   },
   attributes: {
     title: String,
--- a/cmd/gojafs/domino/domino.go
+++ b/cmd/gojafs/domino/domino.go
@@ -276,8 +276,6 @@
 
 		if (!el) {
 			console.log('el is null/undefined');
-			//console.log('html:');
-			//console.log(document.documentElement.innerHTML)
 			null;
 		} else if (el._listeners && el._listeners.click) {
 			var fn = el.click.bind(el);
@@ -288,6 +286,22 @@
 			}
 
 			!!fn;
+		} else if (el.type === 'submit' || el.type === 'button') {
+			let p;
+			let submitted = false;
+			for (p = el; p = p.parentElement; p != null) {
+				if (p.tagName && p.tagName === 'FORM') {
+					const event = new Event('submit');
+					event.cancelable = true;
+					if (p.onsubmit) p.onsubmit(event);
+					if (!event.defaultPrevented) {
+						p.submit();
+					}
+					submitted = true;
+					break;
+				}
+			}
+			submitted;
 		} else {
 			false;
 		}
--- a/cmd/gojafs/domino/domino_test.go
+++ b/cmd/gojafs/domino/domino_test.go
@@ -571,6 +571,69 @@
 	d.Stop()
 }
 
+func TestTriggerClickSubmit(t *testing.T) {
+	for _, sel := range []string{"#btn", "#submit"} {
+		jQuery, err := ioutil.ReadFile("../../../js/jquery-3.5.1.js")
+		if err != nil {
+			t.Fatalf("%v", err)
+		}
+		h := `
+		<html>
+		<body>
+		<h1 id="title" style="display: inline-block;">Hello</h1>
+		<form id="the-form">
+			<input type="text" id="info">
+			<input type="submit" id="submit">Submit</button>
+			<button type="button" id="btn">Submit</button>
+		</form>
+		</body>
+		</html>
+		`
+		SCRIPT := string(jQuery) + `
+		var clicked = false;
+		const form = document.getElementById('the-form');
+		form.onsubmit = function(event) {
+			clicked = true;
+			event.preventDefault();
+		};
+		`
+		d := NewDomino(h, nil,  nil)
+		d.Start()
+		_, err = d.Exec(SCRIPT, true)
+		if err != nil {
+			t.Fatalf(err.Error())
+		}
+		d.CloseDoc()
+
+		res, err := d.Exec("$('button').html()", false)
+		if err != nil {
+			t.Fatalf(err.Error())
+		}
+		if res != "Submit" {
+			t.Fatalf(res)
+		}
+
+		if _, _, err = d.TrackChanges(); err != nil {
+			t.Fatalf(err.Error())
+		}
+		_, changed, err := d.TriggerClick(sel)
+		if err != nil {
+			t.Fatalf(err.Error())
+		}
+		if changed {
+			t.Fatal()
+		}
+		res, err = d.Exec("clicked", false)
+		if err != nil {
+			t.Fatalf(err.Error())
+		}
+		if res != "true" {
+			t.Fatalf(res)
+		}
+		d.Stop()
+	}
+}
+
 func TestDomChanged(t *testing.T) {
 	jQuery, err := ioutil.ReadFile("../../../js/jquery-3.5.1.js")
 	if err != nil {
--- a/cmd/gojafs/domino/domintf.js
+++ b/cmd/gojafs/domino/domintf.js
@@ -42,6 +42,18 @@
 	opossum.mutated(a.type, ___fq('yolo', a.target));
 });
 
+___opossumSubmit = function(a, b, c) {
+	if (this.tagName === 'BUTTON' || this.tagName === 'INPUT') {
+		let p;
+		for (p = el; p = p.parentElement; p != null) {
+			if (p.tagName === 'FORM') {
+				if (p.onsubmit) p.onsubmit()
+				break;
+			}
+		}
+	}
+}
+
 addEventListener = function() {};
 removeEventListener = function() {};