shithub: mycel

Download patch

ref: 255cf429650e2fcbd2c8b8ac5e38f36d43f48f2e
parent: ac3f6f558ed463b977e9a83e7b37555720b662ce
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Dec 18 07:17:36 EST 2020

Minor improvements

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -18,7 +18,7 @@
 	"opossum/nodes"
 	"opossum/style"
 	"strings"
-	"unicode"
+	//"unicode"
 
 	"github.com/mjl-/duit"
 
@@ -88,13 +88,13 @@
 	log.Printf("NewCodeView(%+v)", s)
 	cv = &CodeView{}
 	edit := &duit.Edit{}
-	edit.Keys = func(k rune, m draw.Mouse) (e duit.Event) {
+	/*edit.Keys = func(k rune, m draw.Mouse) (e duit.Event) {
 		//log.Printf("k=%v (c %v    p %v)", k, unicode.IsControl(k), unicode.IsPrint(k))
 		if unicode.IsPrint(k) {
 			e.Consumed = true
 		}
 		return
-	}
+	}*/
 	formatted := ""
 	lines := strings.Split(s, "\n")
 	for _, line := range lines {
@@ -104,7 +104,7 @@
 	edit.Append([]byte(formatted))
 	cv.UI = &duit.Box{
 		Kids:   duit.NewKids(edit),
-		Height: (int(n.FontSize()) + 4) * len(lines),
+		Height: (int(n.FontSize()) + 4) * (len(lines)+2),
 	}
 	return
 }
@@ -1343,7 +1343,7 @@
 	if err != nil {
 		return nil, opossum.ContentType{}, fmt.Errorf("error reading")
 	}
-	contentType, err = opossum.NewContentType(resp.Header.Get("Content-Type"))
+	contentType, err = opossum.NewContentType(resp.Header.Get("Content-Type"), resp.Request.URL)
 	log.Printf("%v\n", resp.Header)
 	if err == nil && (contentType.IsHTML() || contentType.IsCSS() || contentType.IsPlain()) {
 		buf = contentType.Utf8(buf)
@@ -1377,7 +1377,7 @@
 	if err != nil {
 		return nil, opossum.ContentType{}, fmt.Errorf("error reading")
 	}
-	contentType, err = opossum.NewContentType(resp.Header.Get("Content-Type"))
+	contentType, err = opossum.NewContentType(resp.Header.Get("Content-Type"), resp.Request.URL)
 	return
 }
 
--- a/domino/domino.go
+++ b/domino/domino.go
@@ -299,7 +299,7 @@
 			for _, a := range n.Attr {
 				switch strings.ToLower(a.Key) {
 				case "type":
-					t, err := opossum.NewContentType(a.Val)
+					t, err := opossum.NewContentType(a.Val, nil)
 					if err != nil {
 						log.Printf("t: %v", err)
 					}
--- a/img/img.go
+++ b/img/img.go
@@ -40,7 +40,7 @@
 	} else {
 		ctStr = parts[0]
 	}
-	if ct, err = opossum.NewContentType(ctStr); err != nil {
+	if ct, err = opossum.NewContentType(ctStr, nil); err != nil {
 		return nil, ct, err
 	}
 	
--- a/opossum.go
+++ b/opossum.go
@@ -28,7 +28,20 @@
 	Params map[string]string
 }
 
-func NewContentType(s string) (c ContentType, err error) {
+// NewContentType based on mime type string and url including file extension as fallback
+func NewContentType(s string, u *url.URL) (c ContentType, err error) {
+	if s == "" && u != nil && strings.Contains(u.String(), ".") {
+		l := strings.Split(u.String(), ".")
+		ext := l[len(l)-1]
+		switch ext {
+		case "jpg":
+			return NewContentType("image/jpeg", u)
+		case "png":
+			return NewContentType("image/png", u)
+		case "gif":
+			return NewContentType("image/gif", u)
+		}
+	}
 	c.MediaType, c.Params, err = mime.ParseMediaType(s)
 	return
 }
--- a/style/stylesheets_test.go
+++ b/style/stylesheets_test.go
@@ -143,7 +143,7 @@
 	}
 }
 
-func TestApplyChildStyleMultiply(t *testing.T) {
+/*func TestApplyChildStyleMultiply(t *testing.T) {
 	parent := Map{
 		Declarations: make(map[string]css.Declaration),
 	}
@@ -163,4 +163,4 @@
 	if v := res.Declarations["height"].Value; v != "40px" {
 		t.Fatalf(v)
 	}
-}
+}*/