shithub: mycel

Download patch

ref: 314a17cf6d0deaf40f45786fe5d6db27a892c0a3
parent: 0f5bb23f370d44870c782b82d29bf87801acf33d
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Feb 27 04:20:16 EST 2021

put js glue code into separate file

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -969,23 +969,18 @@
 		case "button":
 			if t := attr(*n.DomSubtree, "type"); t == "" || t == "submit" {
 				return NewSubmitButton(b, n)
-			} else {
-				btn := &duit.Button{
-					Text: nodes.ContentFrom(*n),
-					Font: n.Font(),
-				}
-				return NewElement(
-					btn,
-					n,
-				)
 			}
+
+			btn := &duit.Button{
+				Text: nodes.ContentFrom(*n),
+				Font: n.Font(),
+			}
+
+			return NewElement(btn, n)
 		case "table":
 			return NewTable(n).Element(r+1, b, n)
 		case "img", "svg":
-			return NewElement(
-				NewImage(n),
-				n,
-			)
+			return NewElement(NewImage(n), n)
 		case "pre":
 			return NewElement(
 				NewCodeView(nodes.ContentFrom(*n), n.Map),
--- a/domino/domino.go
+++ b/domino/domino.go
@@ -31,6 +31,19 @@
 //go:embed domino-lib/*js
 var lib embed.FS
 
+//go:embed domintf.js
+var domIntfJs embed.FS
+
+var domIntf string
+
+func init() {
+	data, err := domIntfJs.ReadFile("domintf.js")
+	if err != nil {
+		panic(err.Error())
+	}
+	domIntf = string(data)
+}
+
 func SetLogger(l *logger.Logger) {
 	log = l
 }
@@ -139,104 +152,7 @@
 	script = strings.Replace(script, "const ", "var ", -1)
 	script = strings.Replace(script, "let ", "var ", -1)
 	script = strings.Replace(script, "<!--", "", -1)
-	SCRIPT := `
-		global = {};
-		//global.__domino_frozen__ = true; // Must precede any require('domino')
-		var domino = require('domino-lib/index');
-		var Element = domino.impl.Element; // etc
-
-		Object.assign(this, domino.createWindow(opossum.html, 'http://example.com'));
-		window = this;
-		window.parent = window;
-		window.top = window;
-		window.self = window;
-		addEventListener = function() {};
-		removeEventListener = function() {};
-		window.location.href = 'http://example.com';
-		var ___fq;
-		___fq = function(pre, el) {
-			var i, p;
-
-			if (!el) {
-				return undefined;
-			}
-			p = el.parentElement;
-
-			if (p) {
-				for (i = 0; i < p.children.length; i++) {
-					if (p.children[i] === el) {
-						return ___fq('', p) + ' > :nth-child(' + (i+1) + ')';
-					}
-				}
-			} else {
-				return el.tagName;
-			}
-		};
-		document._setMutationHandler(function(a) {
-			// a provides attributes type, target and node or attr
-			// (cf Object.keys(a))
-			opossum.mutated(a.type, ___fq('yolo', a.target));
-		});
-		window.getComputedStyle = function(el, pseudo) {
-			this.el = el;
-			this.getPropertyValue = function(prop) {
-				return opossum.style(___fq('', el), pseudo, prop, arguments[2]);
-			};
-			return this;
-		};
-		Element.prototype.getClientRects = function() { /* I'm a stub */ return []; }
-		window.screen = {
-			width: 1280,
-			height: 1024
-		};
-		window.screenX = 0;
-		window.screenY = 25;
-		location = window.location;
-		navigator = {
-			platform: 'plan9(port)',
-			userAgent: 'opossum'
-		};
-		HTMLElement = domino.impl.HTMLElement;
-
-		function XMLHttpRequest() {
-			var _method, _uri;
-			var h = {};
-			var ls = {};
-
-			this.readyState = 0;
-
-			var cb = function(data, err) {
-				if (data !== '') {
-					this.responseText = data;
-					this.readyState = 4;
-					this.state = 200;
-					this.status = 200;
-					if (ls['load']) ls['load'].bind(this)();
-					if (this.onload) this.onload.bind(this)();
-
-					if (this.onreadystatechange) this.onreadystatechange.bind(this)();
-				}
-			}.bind(this);
-
-			this.addEventListener = function(k, fn) {
-				ls[k] = fn;
-			};
-			this.open = function(method, uri) {
-				_method = method;
-				_uri = uri;
-			};
-			this.setRequestHeader = function(k, v) {
-				h[k] = v;
-			};
-			this.send = function(data) {
-				opossum.xhr(_method, _uri, h, data, cb);
-				this.readyState = 2;
-			};
-			this.getAllResponseHeaders = function() {
-				return '';
-			};
-		}
-	` + script
+	SCRIPT := domIntf + script
 	if !initial {
 		SCRIPT = script
 	}
--- /dev/null
+++ b/domino/domintf.js
@@ -1,0 +1,98 @@
+global = {};
+//global.__domino_frozen__ = true; // Must precede any require('domino')
+var domino = require('domino-lib/index');
+var Element = domino.impl.Element; // etc
+
+Object.assign(this, domino.createWindow(opossum.html, 'http://example.com'));
+window = this;
+window.parent = window;
+window.top = window;
+window.self = window;
+addEventListener = function() {};
+removeEventListener = function() {};
+window.location.href = 'http://example.com';
+
+var ___fq;
+___fq = function(pre, el) {
+	var i, p;
+
+	if (!el) {
+		return undefined;
+	}
+	p = el.parentElement;
+
+	if (p) {
+		for (i = 0; i < p.children.length; i++) {
+			if (p.children[i] === el) {
+				return ___fq('', p) + ' > :nth-child(' + (i+1) + ')';
+			}
+		}
+	} else {
+		return el.tagName;
+	}
+};
+
+document._setMutationHandler(function(a) {
+	// a provides attributes type, target and node or attr
+	// (cf Object.keys(a))
+	opossum.mutated(a.type, ___fq('yolo', a.target));
+});
+window.getComputedStyle = function(el, pseudo) {
+	this.el = el;
+	this.getPropertyValue = function(prop) {
+		return opossum.style(___fq('', el), pseudo, prop, arguments[2]);
+	};
+	return this;
+};
+Element.prototype.getClientRects = function() { /* I'm a stub */ return []; }
+window.screen = {
+	width: 1280,
+	height: 1024
+};
+window.screenX = 0;
+window.screenY = 25;
+location = window.location;
+navigator = {
+	platform: 'plan9(port)',
+	userAgent: 'opossum'
+};
+HTMLElement = domino.impl.HTMLElement;
+
+function XMLHttpRequest() {
+	var _method, _uri;
+	var h = {};
+	var ls = {};
+
+	this.readyState = 0;
+
+	var cb = function(data, err) {
+		if (data !== '') {
+			this.responseText = data;
+			this.readyState = 4;
+			this.state = 200;
+			this.status = 200;
+			if (ls['load']) ls['load'].bind(this)();
+			if (this.onload) this.onload.bind(this)();
+
+			if (this.onreadystatechange) this.onreadystatechange.bind(this)();
+		}
+	}.bind(this);
+
+	this.addEventListener = function(k, fn) {
+		ls[k] = fn;
+	};
+	this.open = function(method, uri) {
+		_method = method;
+		_uri = uri;
+	};
+	this.setRequestHeader = function(k, v) {
+		h[k] = v;
+	};
+	this.send = function(data) {
+		opossum.xhr(_method, _uri, h, data, cb);
+		this.readyState = 2;
+	};
+	this.getAllResponseHeaders = function() {
+		return '';
+	};
+}