shithub: opossum

Download patch

ref: 8423f45cf12438346a1aea3cf4cd4f834d55cce9
parent: 9d37d6e32a89ccfc17a18b0d25165b0713a74dc5
author: Philip Silva <philip.silva@protonmail.com>
date: Fri May 14 05:39:50 EDT 2021

More DOM APIs, separated ES6 support

- polyfills for more DOM APIs
- very experimental ES6 support through cmd/6to5

--- a/cmd/6to5/main.go	Thu May 13 06:02:35 2021
+++ b/cmd/6to5/main.go	Fri May 14 05:39:50 2021
@@ -15,7 +15,9 @@
 	babel.Init(1) // Setup 1 transformer (can be any number > 0)
 	r, err := babel.Transform(os.Stdin, map[string]interface{}{
 		"plugins": []string{
+			"transform-arrow-functions",
 			"transform-block-scoping",
+			"transform-classes",
 			"transform-destructuring",
 			"transform-spread",
 			"transform-parameters",
--- a/domino/domino.go	Thu May 13 06:02:35 2021
+++ b/domino/domino.go	Fri May 14 05:39:50 2021
@@ -1,6 +1,7 @@
 package domino
 
 import (
+	"bytes"
 	"embed"
 	"errors"
 	"fmt"
@@ -9,7 +10,6 @@
 	"github.com/dop251/goja_nodejs/console"
 	"github.com/dop251/goja_nodejs/eventloop"
 	"github.com/dop251/goja_nodejs/require"
-	"github.com/jvatic/goja-babel"
 	"golang.org/x/net/html"
 	"io/ioutil"
 	"github.com/psilva261/opossum"
@@ -17,6 +17,7 @@
 	"github.com/psilva261/opossum/nodes"
 	"net/http"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"regexp"
 	"strconv"
@@ -258,23 +259,14 @@
 }
 
 func (d *Domino) Exec6(script string, initial bool) (res string, err error) {
-	babel.Init(2) // Setup 4 transformers (can be any number > 0)
-	r, err := babel.Transform(strings.NewReader(script), map[string]interface{}{
-		"plugins": []string{
-			"transform-block-scoping",
-			"transform-destructuring",
-			"transform-spread",
-			"transform-parameters",
-		},
-	})
-	if err != nil {
-		return "", fmt.Errorf("babel: %v", err)
-	}
-	buf, err := ioutil.ReadAll(r)
-	if err != nil {
-		return "", fmt.Errorf("read all: %v", err)
+	cmd := exec.Command("6to5")
+	cmd.Stdin = strings.NewReader(script)
+	var out bytes.Buffer
+	cmd.Stdout = &out
+	if err = cmd.Run(); err != nil {
+		return "", fmt.Errorf("6to5: %w", err)
 	}
-	return d.Exec(string(buf), initial)
+	return d.Exec(out.String(), initial)
 }
 
 // CloseDoc fires DOMContentLoaded to trigger $(document).ready(..)
--- a/domino/domino_test.go	Thu May 13 06:02:35 2021
+++ b/domino/domino_test.go	Fri May 14 05:39:50 2021
@@ -73,7 +73,6 @@
 	script := `
 	$(document).ready(function() {
 		gfgf
-		console.log('yolo');
 	});
 	setTimeout(function() {
 		console.log("ok");
@@ -239,26 +238,17 @@
 	if err != nil {
 		t.Fatalf("%v", err)
 	}
-	//t.Parallel()
 	SCRIPT := string(jQuery) + `
     ;;;
 	setTimeout(function() {
-		console.log("ok :-)");
-		console.log(s.buf);
 		var h = document.querySelector('html');
     	console.log(h.innerHTML);
 	}, 1000);
-	console.log("Started");
 	Object.assign(this, window);
     $(document).ready(function() {
     	console.log('READDDYYYYY!!!!!!!');
     });
-    console.log('$:');
-    console.log($);
-    console.log('$h1:');
-    console.log($('h1').html());
 
-    //elem.dispatchEvent(event);
     console.log(window.location.href);
 	`
 	d := NewDomino(simpleHTML, nil, nil)
@@ -338,23 +328,13 @@
 	SCRIPT := string(jQuery) + `
     ;;;
 	setTimeout(function() {
-		console.log("ok :-)");
-		console.log(s.buf);
 		var h = document.querySelector('html');
     	console.log(h.innerHTML);
 	}, 1000);
-	console.log("Started");
 	Object.assign(this, window);
     $(document).ready(function() {
     	console.log('READDDYYYYY!!!!!!!');
     });
-    console.log('$:');
-    console.log($);
-    console.log('$h1:');
-    console.log($('h1').html());
-
-    //elem.dispatchEvent(event);
-    console.log(window.location.href);
 	`
 	d := NewDomino(simpleHTML, nil, nil)
 	d.Start()
@@ -367,9 +347,6 @@
 	if err != nil {
 		t.Fatalf(err.Error())
 	}
-	/*if res != "Hello" {
-		t.Fatalf(res)
-	}*/
 	_=res
 	res, err = d.Exec("$('h1').html('minor updates :-)'); $('h1').html();", false)
 	if err != nil {
@@ -587,15 +564,10 @@
 		var oReq = new XMLHttpRequest();
 		var loaded = false;
 		oReq.addEventListener("load", function() {
-			console.log('loaded!!!!! !!! 11!!!1!!elf!!!1!');
 			loaded = true;
 		});
-		console.log(oReq.open);
-		console.log('open:');
 		oReq.open("GET", "http://www.example.org/example.txt");
-		console.log('send:');
 		oReq.send();
-		console.log('return:');
 	`
 	_, err := d.Exec(script, true)
 	if err != nil {
@@ -628,11 +600,9 @@
 	$.ajax({
 		url: '/',
 		success: function() {
-			console.log('success!!!');
 			res = 'success';
 		},
 		error: function() {
-			console.log('error!!!');
 			res = 'err';
 		}
 	});
@@ -671,11 +641,9 @@
 	$.ajax({
 		url: '/',
 		success: function() {
-			console.log('success!!!');
 			res = 'success';
 		},
 		error: function() {
-			console.log('error!!!');
 			res = 'err';
 		}
 	});
--- a/domino/domintf.js	Thu May 13 06:02:35 2021
+++ b/domino/domintf.js	Fri May 14 05:39:50 2021
@@ -103,3 +103,93 @@
 		return el.tagName;
 	}
 };
+
+// https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
+// CC0
+if (typeof TextEncoder === "undefined") {
+    TextEncoder=function TextEncoder(){};
+    TextEncoder.prototype.encode = function encode(str) {
+        "use strict";
+        var Len = str.length, resPos = -1;
+        // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
+        //  takes up the equivelent space of 3 UTF-8 characters to encode it properly. However, Array's
+        //  have an auto expanding length and 1.5x should be just the right balance for most uses.
+        var resArr = typeof Uint8Array === "undefined" ? new Array(Len * 1.5) : new Uint8Array(Len * 3);
+        for (var point=0, nextcode=0, i = 0; i !== Len; ) {
+            point = str.charCodeAt(i), i += 1;
+            if (point >= 0xD800 && point <= 0xDBFF) {
+                if (i === Len) {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; break;
+                }
+                // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+                nextcode = str.charCodeAt(i);
+                if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
+                    point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
+                    i += 1;
+                    if (point > 0xffff) {
+                        resArr[resPos += 1] = (0x1e/*0b11110*/<<3) | (point>>>18);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>12)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
+                        continue;
+                    }
+                } else {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; continue;
+                }
+            }
+            if (point <= 0x007f) {
+                resArr[resPos += 1] = (0x0/*0b0*/<<7) | point;
+            } else if (point <= 0x07ff) {
+                resArr[resPos += 1] = (0x6/*0b110*/<<5) | (point>>>6);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)  | (point&0x3f/*0b00111111*/);
+            } else {
+                resArr[resPos += 1] = (0xe/*0b1110*/<<4) | (point>>>12);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | ((point>>>6)&0x3f/*0b00111111*/);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | (point&0x3f/*0b00111111*/);
+            }
+        }
+        if (typeof Uint8Array !== "undefined") return resArr.subarray(0, resPos + 1);
+        // else // IE 6-9
+        resArr.length = resPos + 1; // trim off extra weight
+        return resArr;
+    };
+    TextEncoder.prototype.toString = function(){return "[object TextEncoder]"};
+    try { // Object.defineProperty only works on DOM prototypes in IE8
+        Object.defineProperty(TextEncoder.prototype,"encoding",{
+            get:function(){if(TextEncoder.prototype.isPrototypeOf(this)) return"utf-8";
+                           else throw TypeError("Illegal invocation");}
+        });
+    } catch(e) { /*IE6-8 fallback*/ TextEncoder.prototype.encoding = "utf-8"; }
+    if(typeof Symbol!=="undefined")TextEncoder.prototype[Symbol.toStringTag]="TextEncoder";
+}
+
+function LocalStorage() {
+        var data = {};
+        this.setItem = function(id, val) {
+                return data[id] = String(val);
+        };
+        this.getItem = function(id) {
+                return data.hasOwnProperty(id) ? data[id] : undefined;
+        };
+        this.removeItem = function(id) {
+                return delete data[id];
+        };
+        this.clear = function() {
+                return data = {};
+        };
+}
+window.localStorage = new LocalStorage();
+
+const imageHandler = {
+  construct(target, args) {
+    var el = document.createElement('img');
+
+    if (args.length >= 1) el.width = args[0];
+    if (args.length >= 2) el.height = args[1];
+
+    return el;
+  }
+};
+const Image = new Proxy(HTMLImageElement, imageHandler);