ref: 53e30e39a6156b3182cff49af4e9375216e9df77
parent: 20c9b3b3afc19bfc4cb218e7b0bd891f529f5a4e
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Sep 26 15:11:09 EDT 2021
go fmt
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Opossum Web Browser
-Basic portable Web browser; only needs a Go compiler to compile.
+Basic portable Web browser; only needs a Go compiler to compile. Optimized for use on 9front and 9legacy, supports plan9port and 9pi as well.
The UI is built with https://github.com/mjl-/duit
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -5,13 +5,6 @@
"bytes"
"errors"
"fmt"
- "golang.org/x/net/html"
- "golang.org/x/net/publicsuffix"
- "image"
- "io/ioutil"
- "net/http"
- "net/http/cookiejar"
- "net/url"
"github.com/psilva261/opossum"
"github.com/psilva261/opossum/browser/cache"
"github.com/psilva261/opossum/browser/duitx"
@@ -22,7 +15,14 @@
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
+ "golang.org/x/net/publicsuffix"
+ "image"
+ "io/ioutil"
"math"
+ "net/http"
+ "net/http/cookiejar"
+ "net/url"
"os"
"strconv"
"strings"
@@ -44,7 +44,7 @@
// OpenClipart SVG ID: 163773 from OCAL 0.18 release 16/11/2019
// https://freesvg.org/topo-architetto-francesc-01
// (Public Domain)
-var cursor = [16*2]uint8{
+var cursor = [16 * 2]uint8{
0b00000001, 0b11111100,
0b00000111, 0b11111110,
0b00001111, 0b11111111,
@@ -61,15 +61,15 @@
var (
ExperimentalJsInsecure bool
- EnableNoScriptTag bool
+ EnableNoScriptTag bool
)
var (
- browser *Browser
- Style = style.Map{}
- dui *duit.DUI
+ browser *Browser
+ Style = style.Map{}
+ dui *duit.DUI
scroller *duitx.Scroll
- display *draw.Display
+ display *draw.Display
colorCache = make(map[draw.Color]*draw.Image)
imageCache = make(map[string]*draw.Image)
@@ -109,7 +109,7 @@
l := &Element{
UI: NewLabel(t, n),
- n: n,
+ n: n,
}
ls = append(ls, l)
}
@@ -148,7 +148,7 @@
edit.Append([]byte(s))
cv.UI = &duitx.Box{
Kids: duit.NewKids(edit),
- Height: int(n.FontHeight()) * (lines+2),
+ Height: int(n.FontHeight()) * (lines + 2),
}
return
}
@@ -195,7 +195,7 @@
src = newPicture(n)
} else if n.Data() == "svg" {
xml, err := n.Serialized()
- if err != nil {
+ if err != nil {
return nil, fmt.Errorf("serialize: %w", err)
}
log.Printf("newImage: xml: %v", xml)
@@ -272,9 +272,9 @@
bestW := 0
idealW := n.Width()
scale := 1
-
+
u := func(wd int) int {
- return int(math.Abs(float64(wd)-float64(idealW)))
+ return int(math.Abs(float64(wd) - float64(idealW)))
}
if dui != nil {
@@ -310,8 +310,8 @@
type Element struct {
duit.UI
n *nodes.Node
- IsLink bool
- Click func() duit.Event
+ IsLink bool
+ Click func() duit.Event
Changed func(*Element)
}
@@ -335,7 +335,7 @@
return &Element{
UI: ui,
- n: n,
+ n: n,
}
}
@@ -391,7 +391,7 @@
}
// TODO: make sure input fields can be put into a box
- if n.Data() =="input" {
+ if n.Data() == "input" {
return nil, false
}
}
@@ -405,13 +405,13 @@
Kids: duit.NewKids(uis...),
Width: w,
Height: h,
- MaxWidth: mw,
+ MaxWidth: mw,
ContentBox: contentBox,
Background: i,
- Margin: m,
- Padding: p,
- Dir: duitFlexDir(n),
- Disp: duitDisplay(n),
+ Margin: m,
+ Padding: p,
+ Dir: duitFlexDir(n),
+ Disp: duitDisplay(n),
}
return box, true
@@ -510,8 +510,8 @@
}
btn := &duit.Button{
- Text: t,
- Font: n.Font(),
+ Text: t,
+ Font: n.Font(),
Click: click,
}
return NewElement(btn, n)
@@ -558,7 +558,7 @@
var l *duit.List
l = &duit.List{
Values: make([]*duit.ListValue, 0, len(n.Children)),
- Font: n.Font(),
+ Font: n.Font(),
Changed: func(i int) (e duit.Event) {
v := l.Values[i]
vv := fmt.Sprintf("%v", v.Value)
@@ -575,8 +575,8 @@
continue
}
lv := &duit.ListValue{
- Text: c.ContentString(false),
- Value: c.Attr("value"),
+ Text: c.ContentString(false),
+ Value: c.Attr("value"),
Selected: c.HasAttr("selected"),
}
l.Values = append(l.Values, lv)
@@ -585,7 +585,7 @@
n.SetCss("max-width", "200px")
}
if n.Css("height") == "" {
- n.SetCss("height", fmt.Sprintf("%vpx", 4 * n.Font().Height))
+ n.SetCss("height", fmt.Sprintf("%vpx", 4*n.Font().Height))
}
return NewElement(duit.NewScroll(l), n)
}
@@ -603,7 +603,7 @@
edit.Append([]byte(t))
if n.Css("height") == "" {
- n.SetCss("height", fmt.Sprintf("%vpx", (int(n.FontHeight()) * (lines+2))))
+ n.SetCss("height", fmt.Sprintf("%vpx", (int(n.FontHeight())*(lines+2))))
}
el := NewElement(edit, n)
@@ -844,7 +844,7 @@
uis = append(uis, a)
}
pl := &duitx.Place{
- Kids: duit.NewKids(uis...),
+ Kids: duit.NewKids(uis...),
Background: bg,
}
pl.Place = placeFunc(n.QueryRef(), pl)
@@ -862,7 +862,7 @@
return nil
}
return &Element{
- n: n,
+ n: n,
UI: ui,
}
}
@@ -942,7 +942,7 @@
uis := make([]duit.UI, 0, len(es))
colSpans := make([]int, 0, len(es))
rowSpans := make([]int, 0, len(es))
-
+
for _, e := range es {
uis = append(uis, e)
colSpans = append(colSpans, 1)
@@ -950,14 +950,14 @@
}
return &duitx.Grid{
- Columns: 1,
- Rows: len(uis),
+ Columns: 1,
+ Rows: len(uis),
ColSpans: colSpans,
RowSpans: rowSpans,
- Padding: duit.NSpace(1, duit.SpaceXY(0, 3)),
- Halign: []duit.Halign{duit.HalignLeft},
- Valign: []duit.Valign{duit.ValignTop},
- Kids: duit.NewKids(uis...),
+ Padding: duit.NSpace(1, duit.SpaceXY(0, 3)),
+ Halign: []duit.Halign{duit.HalignLeft},
+ Valign: []duit.Valign{duit.ValignTop},
+ Kids: duit.NewKids(uis...),
}
}
@@ -1054,14 +1054,14 @@
return NewElement(
&duitx.Grid{
- Columns: numCols,
- Rows: len(t.rows),
+ Columns: numCols,
+ Rows: len(t.rows),
ColSpans: colSpans,
RowSpans: rowSpans,
- Padding: duit.NSpace(numCols, duit.SpaceXY(0, 3)),
- Halign: halign,
- Valign: valign,
- Kids: duit.NewKids(uis...),
+ Padding: duit.NSpace(numCols, duit.SpaceXY(0, 3)),
+ Halign: halign,
+ Valign: valign,
+ Kids: duit.NewKids(uis...),
},
n,
)
@@ -1088,13 +1088,13 @@
}
type TableRow struct {
- n *nodes.Node
+ n *nodes.Node
columns []*nodes.Node
}
func NewTableRow(n *nodes.Node) (tr *TableRow) {
tr = &TableRow{
- n: n,
+ n: n,
columns: make([]*nodes.Node, 0, 5),
}
@@ -1389,12 +1389,12 @@
type Browser struct {
history.History
- dui *duit.DUI
- Website *Website
- loading bool
- client *http.Client
+ dui *duit.DUI
+ Website *Website
+ loading bool
+ client *http.Client
Download func(res chan *string)
- LocCh chan string
+ LocCh chan string
StatusCh chan string
}
@@ -1408,9 +1408,9 @@
client: &http.Client{
Jar: jar,
},
- dui: _dui,
- Website: &Website{},
- LocCh: make(chan string, 10),
+ dui: _dui,
+ Website: &Website{},
+ LocCh: make(chan string, 10),
StatusCh: make(chan string, 10),
}
@@ -1650,4 +1650,3 @@
contentType, err = opossum.NewContentType(resp.Header.Get("Content-Type"), resp.Request.URL)
return
}
-
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -3,16 +3,16 @@
import (
"9fans.net/go/draw"
"fmt"
- "github.com/mjl-/duit"
- "golang.org/x/net/html"
- "image"
- "net/http"
- "net/url"
"github.com/chris-ramon/douceur/css"
+ "github.com/mjl-/duit"
"github.com/psilva261/opossum/browser/duitx"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
+ "image"
+ "net/http"
+ "net/url"
"strings"
"testing"
)
@@ -118,13 +118,13 @@
expect: "https://news.ycombinator.com/news",
},
item{
- orig: "https://golang.org/pkg/",
- href: "net/http",
+ orig: "https://golang.org/pkg/",
+ href: "net/http",
expect: "https://golang.org/pkg/net/http",
},
item{
- orig: "https://example.com/",
- href: "info",
+ orig: "https://example.com/",
+ href: "info",
expect: "https://example.com/info",
},
}
@@ -302,7 +302,7 @@
t.Errorf("node: %+v", outerSpan)
}
bracket := outerSpan.Children[0]
- if /*bracket.Data() != "(" || */!bracket.IsInline() {
+ if /*bracket.Data() != "(" || */ !bracket.IsInline() {
t.Errorf("bracket, is inline: %v %+v %+v", bracket.IsInline(), bracket, bracket.Data())
}
sp1 := outerSpan.Children[1]
@@ -367,7 +367,7 @@
t.Fail()
}
}
-
+
PrintTree(boxed)
// 2. Elements are 2 rows
--- a/browser/cache/cache.go
+++ b/browser/cache/cache.go
@@ -25,7 +25,7 @@
type Item struct {
Addr string
opossum.ContentType
- Buf []byte
+ Buf []byte
Used time.Time
}
--- a/browser/duitx/box.go
+++ b/browser/duitx/box.go
@@ -68,11 +68,11 @@
// Box keeps elements on a line as long as they fit, then moves on to the next line.
type Box struct {
- Kids []*duit.Kid // Kids and UIs in this box.
+ Kids []*duit.Kid // Kids and UIs in this box.
Reverse bool // Lay out children from bottom to top. First kid will be at the bottom.
- Margin duit.Space // In lowDPI pixels, will be adjusted for highDPI screens.
- Padding duit.Space // Padding inside box, so children don't touch the sides; in lowDPI pixels, also adjusted for highDPI screens.
- Valign duit.Valign // How to align children on a line.
+ Margin duit.Space // In lowDPI pixels, will be adjusted for highDPI screens.
+ Padding duit.Space // Padding inside box, so children don't touch the sides; in lowDPI pixels, also adjusted for highDPI screens.
+ Valign duit.Valign // How to align children on a line.
Width int // 0 means dynamic (as much as needed), -1 means full width, >0 means that exact amount of lowDPI pixels.
Height int // 0 means dynamic (as much as needed), -1 means full height, >0 means that exact amount of lowDPI pixels.
MaxWidth int // if >0, the max number of lowDPI pixels that will be used.
@@ -120,9 +120,9 @@
}
if ui.ContentBox {
- bbw += margin.Dx()+padding.Dx()
- bbmaxw += margin.Dx()+padding.Dx()
- bbh += margin.Dy()+padding.Dy()
+ bbw += margin.Dx() + padding.Dx()
+ bbmaxw += margin.Dx() + padding.Dx()
+ bbh += margin.Dy() + padding.Dy()
}
osize := sizeAvail
--- a/browser/duitx/grid.go
+++ b/browser/duitx/grid.go
@@ -31,16 +31,16 @@
// Grid lays out other duit.UIs in a table-like grid.
type Grid struct {
- Kids []*duit.Kid // Holds UIs in the grid, per row.
- Columns int // Number of columns.
- Rows int // Number of rows.
+ Kids []*duit.Kid // Holds UIs in the grid, per row.
+ Columns int // Number of columns.
+ Rows int // Number of rows.
RowSpans []int
ColSpans []int
- Valign []duit.Valign // Vertical alignment per column.
- Halign []duit.Halign // Horizontal alignment per column.
- Padding []duit.Space // Padding in lowDPI pixels per column.
- Width int // -1 means full width, 0 means automatic width, >0 means exactly that many lowDPI pixels.
- Background *draw.Image `json:"-"` // Background color.
+ Valign []duit.Valign // Vertical alignment per column.
+ Halign []duit.Halign // Horizontal alignment per column.
+ Padding []duit.Space // Padding in lowDPI pixels per column.
+ Width int // -1 means full width, 0 means automatic width, >0 means exactly that many lowDPI pixels.
+ Background *draw.Image `json:"-"` // Background color.
widths []int
heights []int
@@ -62,7 +62,7 @@
}
}
inc := func() {
- j +=1
+ j += 1
if j >= ui.Columns {
j = 0
i += 1
@@ -70,7 +70,7 @@
}
for l := range ui.Kids {
-ij_iter:
+ ij_iter:
if ll := ui.pos[i][j]; ll >= 0 {
inc()
goto ij_iter
@@ -101,7 +101,7 @@
k.UI.Layout(dui, k, image.Pt(sizeAvail.X-space.Dx(), sizeAvail.Y-space.Dy()), true)
newDx = maximum(
newDx,
- (k.R.Dx()+space.Dx()) / ui.ColSpans[l],
+ (k.R.Dx()+space.Dx())/ui.ColSpans[l],
)
}
maxW[j] = newDx
@@ -152,7 +152,7 @@
r := float64(sizeAvail.X) / float64(width)
width = sizeAvail.X
for i := range ui.widths {
- ui.widths[i] = int(float64(ui.widths[i])*r)
+ ui.widths[i] = int(float64(ui.widths[i]) * r)
}
}
--- a/browser/duitx/grid_test.go
+++ b/browser/duitx/grid_test.go
@@ -9,9 +9,9 @@
func TestInitPos(t *testing.T) {
g := &Grid{
- Kids: make([]*duit.Kid, 2*2),
- Columns: 2,
- Rows: 2,
+ Kids: make([]*duit.Kid, 2*2),
+ Columns: 2,
+ Rows: 2,
RowSpans: []int{1, 1, 1, 1},
ColSpans: []int{1, 1, 1, 1},
}
@@ -21,9 +21,9 @@
}
g = &Grid{
- Kids: make([]*duit.Kid, 1*2),
- Columns: 2,
- Rows: 2,
+ Kids: make([]*duit.Kid, 1*2),
+ Columns: 2,
+ Rows: 2,
RowSpans: []int{1, 1},
ColSpans: []int{2, 2},
}
@@ -33,9 +33,9 @@
}
g = &Grid{
- Kids: make([]*duit.Kid, 2*1),
- Columns: 2,
- Rows: 2,
+ Kids: make([]*duit.Kid, 2*1),
+ Columns: 2,
+ Rows: 2,
RowSpans: []int{2, 2},
ColSpans: []int{1, 1},
}
@@ -50,7 +50,9 @@
Dimensions: "400x300",
}
dui, err := duit.NewDUI("scroll_test", opts)
- if err != nil { t.Fatalf("err: %v", err) }
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
g := Grid{
Kids: duit.NewKids(
&duit.Button{Text: "upper"},
@@ -57,10 +59,10 @@
&duit.Button{Text: "LL"},
&duit.Button{Text: "LR"},
),
- Columns: 2,
- Rows: 2,
- RowSpans: []int{1,1,1},
- ColSpans: []int{2,1,1},
+ Columns: 2,
+ Rows: 2,
+ RowSpans: []int{1, 1, 1},
+ ColSpans: []int{2, 1, 1},
}
g.initPos()
maxW, w, xs := g.maxWidths(dui, image.Point{X: 400, Y: 300})
--- a/browser/duitx/label.go
+++ b/browser/duitx/label.go
@@ -34,8 +34,8 @@
// cmd-c, copy text
// \n, like button1 click, calls the Click function
type Label struct {
- Text string // Text to draw, wrapped at glyph boundary.
- Font *draw.Font `json:"-"` // For drawing text.
+ Text string // Text to draw, wrapped at glyph boundary.
+ Font *draw.Font `json:"-"` // For drawing text.
Click func() (e duit.Event) `json:"-"` // Called on button1 click.
lines []string
@@ -84,7 +84,7 @@
}
func (ui *Label) lineHeight(font *draw.Font) int {
- return int(math.Ceil(float64(font.Height)*1.2))
+ return int(math.Ceil(float64(font.Height) * 1.2))
}
func (ui *Label) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
--- a/browser/duitx/place.go
+++ b/browser/duitx/place.go
@@ -33,7 +33,7 @@
// Place is called during layout. It must configure Kids, and set self.R, based on sizeAvail.
Place func(self *duit.Kid, sizeAvail image.Point) `json:"-"`
Kids []*duit.Kid // Kids to draw, set by the Place function.
- Background *draw.Image `json:"-"` // For background color.
+ Background *draw.Image `json:"-"` // For background color.
kidsReversed []*duit.Kid
size image.Point
--- a/browser/duitx/scroll.go
+++ b/browser/duitx/scroll.go
@@ -46,10 +46,10 @@
img *draw.Image // for child to draw on
scrollbarSize int
lastMouseUI duit.UI
- drawOffset int
+ drawOffset int
- tiles map[int]*draw.Image
- last map[int]time.Time
+ tiles map[int]*draw.Image
+ last map[int]time.Time
tilesChanged bool
}
@@ -59,9 +59,9 @@
func NewScroll(dui *duit.DUI, ui duit.UI) *Scroll {
s := &Scroll{
Height: -1,
- Kid: duit.Kid{UI: ui},
- tiles: make(map[int]*draw.Image),
- last: make(map[int]time.Time),
+ Kid: duit.Kid{UI: ui},
+ tiles: make(map[int]*draw.Image),
+ last: make(map[int]time.Time),
}
return s
}
@@ -101,12 +101,12 @@
log.Printf("ensure(dui, %v)", i)
last, ok := ui.last[i]
tl, _ := ui.tiles[i]
- if ok && time.Since(last) < maxAge && ui.sizeOk(tl) {
+ if ok && time.Since(last) < maxAge && ui.sizeOk(tl) {
return
}
log.Printf("ensure(dui, %v): draw", i)
- r := ui.r.Add(image.Point{X: 0, Y: i*ui.r.Dy()})
+ r := ui.r.Add(image.Point{X: 0, Y: i * ui.r.Dy()})
img, err := dui.Display.AllocImage(r, draw.ARGB32, false, dui.BackgroundColor)
if duitError(dui, err, "allocimage") {
return
@@ -139,8 +139,8 @@
func (ui *Scroll) tlR(i int) (r image.Rectangle) {
r.Min.X = ui.r.Min.X
r.Max.X = ui.r.Max.X
- r.Min.Y = ui.r.Min.Y+i*ui.r.Dy()
- r.Max.Y = r.Min.Y+ui.r.Dy()
+ r.Min.Y = ui.r.Min.Y + i*ui.r.Dy()
+ r.Max.Y = r.Min.Y + ui.r.Dy()
return
}
@@ -231,7 +231,7 @@
if ui.childR.Empty() {
return
}
-
+
var i, of int
var tl, tl1 *draw.Image
var ok, ok1, ok2, ok3, okm1, okm2 bool
@@ -243,10 +243,18 @@
i, of = ui.pos()
tl, ok = ui.tiles[i]
tl1, ok1 = ui.tiles[i+1]
- if !ok { ui.ensure(dui, i) }
- if !ok1 { ui.ensure(dui, i+1) }
- if !ok { tl, _ = ui.tiles[i] }
- if !ok1 && of > 0 { tl1, _ = ui.tiles[i+1] }
+ if !ok {
+ ui.ensure(dui, i)
+ }
+ if !ok1 {
+ ui.ensure(dui, i+1)
+ }
+ if !ok {
+ tl, _ = ui.tiles[i]
+ }
+ if !ok1 && of > 0 {
+ tl1, _ = ui.tiles[i+1]
+ }
}
predrawFut := func() {
@@ -261,11 +269,21 @@
if i > 1 {
_, okm2 = ui.tiles[i-2]
}
- if of == 0 && !ok1 { ui.ensure(dui, i+1) }
- if ok1 && !ok2 { ui.ensure(dui, i+2) }
- if ok2 && !ok3 { ui.ensure(dui, i+3) }
- if i > 0 && !okm1 { ui.ensure(dui, i-1) }
- if i > 1 && okm1 && !okm2 { ui.ensure(dui, i-2) }
+ if of == 0 && !ok1 {
+ ui.ensure(dui, i+1)
+ }
+ if ok1 && !ok2 {
+ ui.ensure(dui, i+2)
+ }
+ if ok2 && !ok3 {
+ ui.ensure(dui, i+3)
+ }
+ if i > 0 && !okm1 {
+ ui.ensure(dui, i-1)
+ }
+ if i > 1 && okm1 && !okm2 {
+ ui.ensure(dui, i-2)
+ }
}
defer predrawFut()
@@ -300,7 +318,7 @@
},
Max: ui.childR.Max,
}
- pOf := draw.Point{X: 0, Y: ui.Offset+rTop.Dy()}
+ pOf := draw.Point{X: 0, Y: ui.Offset + rTop.Dy()}
img.Draw(rTop.Add(orig), tl, nil, p)
if of > 0 {
img.Draw(rBtm.Add(orig), tl1, nil, pOf)
--- a/browser/duitx/scroll_test.go
+++ b/browser/duitx/scroll_test.go
@@ -15,15 +15,19 @@
Max: image.Point{100, 1000},
},
Offset: 1,
- tiles: make(map[int]*draw.Image),
- last: make(map[int]time.Time),
+ tiles: make(map[int]*draw.Image),
+ last: make(map[int]time.Time),
}
dui, err := duit.NewDUI("scroll_test", nil)
- if err != nil { t.Fatalf("err: %v", err) }
- r := rect(draw.Point{100,100})
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
+ r := rect(draw.Point{100, 100})
for i := 0; i < 10; i++ {
ui.tiles[i], err = dui.Display.AllocImage(r, draw.ARGB32, false, 0xff00ff00)
- if err != nil { t.Fatalf("err: %v", err) }
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
}
ui.freeCur()
if len(ui.tiles) != 8 {
--- a/browser/experimental.go
+++ b/browser/experimental.go
@@ -1,13 +1,13 @@
package browser
import (
+ "9fans.net/go/draw"
"fmt"
- "image"
+ "github.com/mjl-/duit"
"github.com/psilva261/opossum/browser/duitx"
"github.com/psilva261/opossum/js"
"github.com/psilva261/opossum/logger"
- "9fans.net/go/draw"
- "github.com/mjl-/duit"
+ "image"
)
type AtomBox struct {
@@ -20,18 +20,18 @@
// BackgroundImgSrc to read image from provided cache
// it's okay when the pointer is empty -> defered loading
BackgroundImgSrc string
- BackgroundColor draw.Color
- BorderWidths AtomBox
- Color draw.Color
- Margin AtomBox
- Padding AtomBox
- Wrap bool
+ BackgroundColor draw.Color
+ BorderWidths AtomBox
+ Color draw.Color
+ Margin AtomBox
+ Padding AtomBox
+ Wrap bool
// Children []*Atom TODO: future; at the same time rething where
- // to put Draw functions etc./if to rely on
- // type Kid
- Text string // Text to draw, wrapped at glyph boundary.
- Font *draw.Font `json:"-"` // For drawing text.
+ // to put Draw functions etc./if to rely on
+ // type Kid
+ Text string // Text to draw, wrapped at glyph boundary.
+ Font *draw.Font `json:"-"` // For drawing text.
Click func()
lines []string
@@ -58,7 +58,7 @@
if ui == nil {
return true
}
- switch /*v := */ui.(type) {
+ switch /*v := */ ui.(type) {
case nil:
return true
case *duit.Scroll:
--- a/browser/experimental_test.go
+++ b/browser/experimental_test.go
@@ -1,11 +1,11 @@
package browser
import (
- "golang.org/x/net/html"
"github.com/psilva261/opossum/browser/fs"
"github.com/psilva261/opossum/js"
"github.com/psilva261/opossum/nodes"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
"io/ioutil"
"strings"
"testing"
@@ -29,7 +29,9 @@
`
buf := strings.NewReader(h)
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
jq, err := ioutil.ReadFile("../js/jquery-3.5.1.js")
if err != nil {
@@ -47,7 +49,9 @@
js.NewJS(h, nil, nt)
js.Start()
h, _, err = processJS2()
- if err != nil { t.Errorf(err.Error()) }
+ if err != nil {
+ t.Errorf(err.Error())
+ }
t.Logf("h = %+v", h)
if !strings.Contains(h, `<body style="display: none;">`) {
t.Fail()
--- a/browser/fs/fs.go
+++ b/browser/fs/fs.go
@@ -17,8 +17,8 @@
)
var (
- mu *sync.RWMutex
- c *sync.Cond
+ mu *sync.RWMutex
+ c *sync.Cond
oFS *fs.FS
un string
--- a/browser/fs/fs_unix.go
+++ b/browser/fs/fs_unix.go
@@ -1,3 +1,4 @@
+//go:build !plan9
// +build !plan9
package fs
--- a/browser/website.go
+++ b/browser/website.go
@@ -2,9 +2,6 @@
import (
"github.com/mjl-/duit"
- "golang.org/x/net/html"
- "golang.org/x/text/encoding"
- "net/url"
"github.com/psilva261/opossum"
"github.com/psilva261/opossum/browser/duitx"
"github.com/psilva261/opossum/browser/fs"
@@ -12,6 +9,9 @@
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
+ "golang.org/x/text/encoding"
+ "net/url"
"strings"
)
--- a/browser/website_test.go
+++ b/browser/website_test.go
@@ -59,13 +59,13 @@
},
}
res := escapeValues(ct, q).Encode()
- if res != "a=%E3%83%84" {
+ if res != "a=%E3%83%84" {
t.Errorf("%v", res)
}
ct.Params["charset"] = "ISO-8859-1"
res = escapeValues(ct, q).Encode()
- if res != "a=%26%2312484%3B" {
+ if res != "a=%26%2312484%3B" {
t.Errorf("%v", res)
}
}
--- a/cmd/gojafs/domino/domino.go
+++ b/cmd/gojafs/domino/domino.go
@@ -10,8 +10,8 @@
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/require"
- "io/ioutil"
"github.com/psilva261/opossum/logger"
+ "io/ioutil"
"net/http"
"os"
"os/exec"
@@ -23,7 +23,7 @@
"time"
)
-var timeout = 60*time.Second
+var timeout = 60 * time.Second
//go:embed domino-lib/*js
var lib embed.FS
@@ -44,7 +44,7 @@
type Mutation struct {
time.Time
Type int
- Sel string
+ Sel string
}
type Domino struct {
@@ -51,9 +51,9 @@
loop *eventloop.EventLoop
html string
outputHtml string
- domChange chan Mutation
- query func(sel, prop string) (val string, err error)
- xhrq func(req *http.Request) (resp *http.Response, err error)
+ domChange chan Mutation
+ query func(sel, prop string) (val string, err error)
+ xhrq func(req *http.Request) (resp *http.Response, err error)
}
func NewDomino(
@@ -62,10 +62,10 @@
query func(sel, prop string) (val string, err error),
) (d *Domino) {
d = &Domino{
- html: html,
- xhrq: xhr,
+ html: html,
+ xhrq: xhr,
domChange: make(chan Mutation, 100),
- query: query,
+ query: query,
}
return
}
@@ -94,7 +94,7 @@
x, _ := strconv.Atoi(yx[1])
lines := strings.Split(script, "\n")
- if y - 1 > len(lines) - 1 {
+ if y-1 > len(lines)-1 {
y = len(lines)
}
@@ -177,18 +177,18 @@
registry.Enable(vm)
type S struct {
- Buf string `json:"buf"`
- HTML string `json:"html"`
- Referrer func() string `json:"referrer"`
- Style func(string, string, string, string) string `json:"style"`
- XHR func(string, string, map[string]string, string, func(string, string)) `json:"xhr"`
- Mutated func(int, string) `json:"mutated"`
+ Buf string `json:"buf"`
+ HTML string `json:"html"`
+ Referrer func() string `json:"referrer"`
+ Style func(string, string, string, string) string `json:"style"`
+ XHR func(string, string, map[string]string, string, func(string, string)) `json:"xhr"`
+ Mutated func(int, string) `json:"mutated"`
}
vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
vm.Set("opossum", S{
- HTML: d.html,
- Buf: "yolo",
+ HTML: d.html,
+ Buf: "yolo",
Referrer: func() string { return "https://example.com" },
Style: func(sel, pseudo, prop, prop2 string) string {
v, err := d.query(sel, prop)
@@ -198,7 +198,7 @@
}
return v
},
- XHR: d.xhr,
+ XHR: d.xhr,
Mutated: d.mutated,
})
}
@@ -221,21 +221,21 @@
for {
select {
- case v := <-ready:
- log.Infof("ready")
- <-time.After(10 * time.Millisecond)
- if v != nil {
- res = v.String()
- }
- goto cleanup
- case er := <- errCh:
- log.Infof("err")
- <-time.After(10 * time.Millisecond)
- err = fmt.Errorf("event loop: %w", er)
- goto cleanup
- case <-time.After(timeout):
- log.Errorf("Interrupt JS after %v", timeout)
- intCh <- 1
+ case v := <-ready:
+ log.Infof("ready")
+ <-time.After(10 * time.Millisecond)
+ if v != nil {
+ res = v.String()
+ }
+ goto cleanup
+ case er := <-errCh:
+ log.Infof("err")
+ <-time.After(10 * time.Millisecond)
+ err = fmt.Errorf("event loop: %w", er)
+ goto cleanup
+ case <-time.After(timeout):
+ log.Errorf("Interrupt JS after %v", timeout)
+ intCh <- 1
}
}
@@ -269,7 +269,7 @@
// (probably faster and cleaner than anything else)
func (d *Domino) TriggerClick(selector string) (newHTML string, ok bool, err error) {
res, err := d.Exec(`
- var sel = '` + selector + `';
+ var sel = '`+selector+`';
var el = document.querySelector(sel);
console.log('query ' + sel);
@@ -303,9 +303,9 @@
// Put change into html (e.g. from input field mutation)
func (d *Domino) PutAttr(selector, attr, val string) (ok bool, err error) {
res, err := d.Exec(`
- var sel = '` + selector + `';
+ var sel = '`+selector+`';
var el = document.querySelector(sel);
- el.attr('` + attr + `', '` + val + `');
+ el.attr('`+attr+`', '`+val+`');
!!el;
`, false)
@@ -315,7 +315,7 @@
}
func (d *Domino) TrackChanges() (html string, changed bool, err error) {
- outer:
+outer:
for {
// TODO: either add other change types like ajax begin/end or
// just have one channel for all events worth waiting for.
@@ -338,7 +338,7 @@
}
func (d *Domino) xhr(method, uri string, h map[string]string, data string, cb func(data string, err string)) {
- req, err := http.NewRequest(method, /*u.String()*/uri, strings.NewReader(data))
+ req, err := http.NewRequest(method /*u.String()*/, uri, strings.NewReader(data))
if err != nil {
cb("", err.Error())
return
@@ -373,7 +373,7 @@
m := Mutation{
Time: time.Now(),
Type: t,
- Sel: q,
+ Sel: q,
}
select {
--- a/cmd/gojafs/domino/domino_test.go
+++ b/cmd/gojafs/domino/domino_test.go
@@ -147,7 +147,7 @@
}*/
func TestES6(t *testing.T) {
- d := NewDomino(simpleHTML, nil, nil)
+ d := NewDomino(simpleHTML, nil, nil)
d.Start()
script := `
var foo = function(data={}) {}
@@ -268,7 +268,7 @@
}
});
`
- _, err = d.Exec(string(buf) + ";" + script, true)
+ _, err = d.Exec(string(buf)+";"+script, true)
if err != nil {
t.Fatalf("%v", err)
}
@@ -306,7 +306,7 @@
}
});
`
- _, err = d.Exec(string(buf) + ";" + script, true)
+ _, err = d.Exec(string(buf)+";"+script, true)
if err != nil {
t.Fatalf("%v", err)
}
@@ -313,7 +313,7 @@
if err = d.CloseDoc(); err != nil {
t.Fatalf("%v", err)
}
- <-time.After(5*time.Second)
+ <-time.After(5 * time.Second)
res, err := d.Exec("res;", false)
if err != nil {
t.Fatalf("%v", err)
@@ -364,7 +364,7 @@
}, 1000);
var a = 1;
`
- _, err = d.Exec(string(buf) + ";" + script, true)
+ _, err = d.Exec(string(buf)+";"+script, true)
if err != nil {
t.Fatalf("%v", err)
}
@@ -425,7 +425,7 @@
d := NewDomino(string(buf), nil, nil)
d.Start()
for i, fn := range []string{"initfuncs.js", "jquery-1.8.2.js", "goversion.js", "godocs.js"} {
- buf, err := ioutil.ReadFile("../../../js/godoc/"+fn)
+ buf, err := ioutil.ReadFile("../../../js/godoc/" + fn)
if err != nil {
t.Fatalf("%v", err)
}
@@ -445,7 +445,7 @@
d := NewDomino(string(buf), nil, nil)
d.Start()
for i, fn := range []string{"initfuncs.js", "jquery-1.8.2.js", "playground.js", "goversion.js", "godocs.js", "golang.js"} {
- buf, err := ioutil.ReadFile("../../../js/godoc/"+fn)
+ buf, err := ioutil.ReadFile("../../../js/godoc/" + fn)
if err != nil {
t.Fatalf("%v", err)
}
@@ -458,7 +458,7 @@
if err != nil {
t.Fatalf("%v", err)
}
- if !strings.Contains(res, "function playground(opts) {") {
+ if !strings.Contains(res, "function playground(opts) {") {
t.Fatalf("%v", res)
}
@@ -477,7 +477,7 @@
`
_ = script
for i, fn := range []string{"jquery-1.12.4.js", "jquery-ui.js", "tabs.js"} {
- buf, err := ioutil.ReadFile("../../../js/jqueryui/"+fn)
+ buf, err := ioutil.ReadFile("../../../js/jqueryui/" + fn)
if err != nil {
t.Fatalf("%v", err)
}
@@ -535,7 +535,7 @@
});
});
`
- d := NewDomino(simpleHTML, nil, nil)
+ d := NewDomino(simpleHTML, nil, nil)
d.Start()
_, err = d.Exec(SCRIPT, true)
if err != nil {
@@ -598,7 +598,7 @@
if err != nil {
t.Fatalf(err.Error())
}
- _=res
+ _ = res
res, err = d.Exec("$('h1').html('minor updates :-)'); $('h1').html();", false)
if err != nil {
t.Fatalf(err.Error())
@@ -627,7 +627,7 @@
$('h1').hide();
$('h1').show();
`
- _, err = d.Exec(string(buf) + ";" + script, true)
+ _, err = d.Exec(string(buf)+";"+script, true)
if err != nil {
t.Fatalf("%v", err)
}
--- a/cmd/gojafs/main.go
+++ b/cmd/gojafs/main.go
@@ -21,12 +21,12 @@
)
var (
- d *domino.Domino
+ d *domino.Domino
service string
mtpt string
htm string
js []string
- mu sync.Mutex
+ mu sync.Mutex
)
func init() {
@@ -148,7 +148,7 @@
}
defer rwc.Close()
r := json.NewDecoder(rwc)
- _, err = io.WriteString(rwc, sel + "\n")
+ _, err = io.WriteString(rwc, sel+"\n")
if err != nil {
return "", fmt.Errorf("write: %w", err)
}
--- a/cmd/gojafs/main_plan9.go
+++ b/cmd/gojafs/main_plan9.go
@@ -13,12 +13,12 @@
log.Printf("not loading htm/js from mtpt")
return
}
- bs, err := os.ReadFile(mtpt+"/html")
+ bs, err := os.ReadFile(mtpt + "/html")
if err != nil {
return
}
htm = string(bs)
- ds, err := os.ReadDir(mtpt+"/js")
+ ds, err := os.ReadDir(mtpt + "/js")
if err != nil {
return
}
--- a/cmd/gojafs/main_test.go
+++ b/cmd/gojafs/main_test.go
@@ -3,10 +3,10 @@
import (
"9fans.net/go/plan9"
"9fans.net/go/plan9/client"
+ "bufio"
"bytes"
"fmt"
"io"
- "bufio"
"net"
"os/user"
"strings"
@@ -38,15 +38,15 @@
return fsys, conn, nil
}
-func call(fsys *client.Fsys, fn, cmd string, args... string) (resp string, err error) {
+func call(fsys *client.Fsys, fn, cmd string, args ...string) (resp string, err error) {
fid, err := fsys.Open(fn, plan9.ORDWR)
if err != nil {
return
}
defer fid.Close()
- fid.Write([]byte(cmd+"\n"))
+ fid.Write([]byte(cmd + "\n"))
for _, arg := range args {
- fid.Write([]byte(arg+"\n"))
+ fid.Write([]byte(arg + "\n"))
}
r := bufio.NewReader(fid)
b := bytes.NewBuffer([]byte{})
--- a/cmd/gojafs/main_unix.go
+++ b/cmd/gojafs/main_unix.go
@@ -1,3 +1,4 @@
+//go:build !plan9
// +build !plan9
package main
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -3,30 +3,29 @@
import (
"9fans.net/go/draw"
"fmt"
- "image"
- "os"
"github.com/knusbaum/go9p"
+ "github.com/mjl-/duit"
"github.com/psilva261/opossum/browser"
"github.com/psilva261/opossum/js"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/style"
+ "image"
"net/url"
+ "os"
"os/signal"
"runtime/pprof"
"strings"
"time"
- "github.com/mjl-/duit"
)
-
var (
- dui *duit.DUI
- b *browser.Browser
+ dui *duit.DUI
+ b *browser.Browser
cpuprofile string
- loc string = "http://9p.io"
- dbg bool
- v View
- Style = style.Map{}
+ loc string = "http://9p.io"
+ dbg bool
+ v View
+ Style = style.Map{}
)
func init() {
@@ -39,7 +38,7 @@
type Nav struct {
LocationField *duit.Field
- StatusBar *duit.Label
+ StatusBar *duit.Label
}
func NewNav() (n *Nav) {
@@ -49,9 +48,9 @@
},
}
n.LocationField = &duit.Field{
- Text: loc,
- Font: Style.Font(),
- Keys: n.keys,
+ Text: loc,
+ Font: Style.Font(),
+ Keys: n.keys,
}
return
}
@@ -101,10 +100,10 @@
}
type Confirm struct {
- text string
+ text string
value string
- res chan *string
- done bool
+ res chan *string
+ done bool
}
func (c *Confirm) Render() []*duit.Kid {
@@ -119,10 +118,12 @@
Valign: []duit.Valign{duit.ValignMiddle, duit.ValignMiddle, duit.ValignMiddle},
Kids: duit.NewKids(
&duit.Button{
- Text: "Ok",
- Font: browser.Style.Font(),
+ Text: "Ok",
+ Font: browser.Style.Font(),
Click: func() (e duit.Event) {
- if c.done { return }
+ if c.done {
+ return
+ }
s := f.Text
c.res <- &s
c.done = true
@@ -133,10 +134,12 @@
},
},
&duit.Button{
- Text: "Abort",
- Font: browser.Style.Font(),
+ Text: "Abort",
+ Font: browser.Style.Font(),
Click: func() (e duit.Event) {
- if c.done { return }
+ if c.done {
+ return
+ }
close(c.res)
c.done = true
e.Consumed = true
@@ -154,7 +157,7 @@
)
}
-type Loading struct {}
+type Loading struct{}
func (l *Loading) Render() []*duit.Kid {
return nil
@@ -166,7 +169,7 @@
log.Errorf("%v", err)
}
dui.Top.UI = &duit.Box{
- Kids: v.Render(),
+ Kids: v.Render(),
Background: white,
}
if b != nil {
@@ -196,9 +199,9 @@
b = browser.NewBrowser(dui, loc)
b.Download = func(res chan *string) {
v = &Confirm{
- text: fmt.Sprintf("Download %v", b.URL()),
+ text: fmt.Sprintf("Download %v", b.URL()),
value: "/download.file",
- res: res,
+ res: res,
}
render()
return
@@ -218,7 +221,7 @@
nav.LocationField.Text = loc
}
- case msg := <- b.StatusCh:
+ case msg := <-b.StatusCh:
if nav, ok := v.(*Nav); ok {
if msg == "" {
nav.StatusBar.Text = ""
--- a/img/img.go
+++ b/img/img.go
@@ -9,8 +9,8 @@
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"golang.org/x/image/draw"
- "image/png"
"image"
+ "image/png"
"io"
"net/url"
"strings"
@@ -28,7 +28,7 @@
return nil, ct, fmt.Errorf("cannot handle charset")
}
parts := strings.Split(addr, ",")
-
+
var ctStr string
if strings.Contains(parts[0], ";") {
header := strings.Split(parts[0], ";")
@@ -80,7 +80,7 @@
j := strings.LastIndex(s[:eq], " ")
keyStarts[i] = j
}
-
+
valueEnds := make([]int, len(keyStarts))
for i, _ := range keyStarts {
if i+1 < len(keyStarts) {
@@ -88,7 +88,7 @@
} else {
off := eqs[i]
jj := strings.Index(s[off:], ">")
- valueEnds[i] = jj+off
+ valueEnds[i] = jj + off
if s[valueEnds[i]-1:valueEnds[i]] == "/" {
valueEnds[i]--
}
@@ -132,7 +132,7 @@
break
}
}
-
+
if len(tagStarts) != len(tagEnds) {
log.Errorf("quoteAttrs: len(tagStarts) != len(tagEnds)")
return s
@@ -210,7 +210,7 @@
dx := img.Bounds().Max.X
dy := img.Bounds().Max.Y
- log.Printf("dx,dy=%v,%v",dx,dy)
+ log.Printf("dx,dy=%v,%v", dx, dy)
if w == 0 && h == 0 && 0 < maxW && maxW < dx {
w = maxW
}
@@ -240,11 +240,11 @@
return oldX, oldY, true
}
if wantedX == 0 {
- newX = int(float64(oldX) * float64(wantedY)/float64(oldY))
+ newX = int(float64(oldX) * float64(wantedY) / float64(oldY))
newY = wantedY
} else {
newX = wantedX
- newY = int(float64(oldY) * float64(wantedX)/float64(oldX))
+ newY = int(float64(oldY) * float64(wantedX) / float64(oldX))
}
if newX > 2000 || newY > 2000 {
--- a/img/img_test.go
+++ b/img/img_test.go
@@ -41,36 +41,36 @@
}
func TestSvg(t *testing.T) {
- xml := `
+ xml := `
<svg fill="currentColor" height="24" viewBox="0 0 24 24" width="24">
</svg>
`
- _, err := Svg(xml, 0, 0)
- if err != nil {
- t.Fatalf(err.Error())
- }
+ _, err := Svg(xml, 0, 0)
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
}
func TestSvgUnquoted(t *testing.T) {
- xml := `
+ xml := `
<svg fill=currentColor height=24 viewBox=0 0 24 24 width=24>
<g fill=green></g>
<g fill=yellow/>
</svg>
`
- xml=`<svg xmlns=http://www.w3.org/2000/svg viewBox=0 0 37 37 fill=#000000><path class=border fill=blue stroke=green/></svg>`
+ xml = `<svg xmlns=http://www.w3.org/2000/svg viewBox=0 0 37 37 fill=#000000><path class=border fill=blue stroke=green/></svg>`
- _, err := Svg(xml, 0, 0)
- if err != nil {
- t.Fatalf(err.Error())
- }
+ _, err := Svg(xml, 0, 0)
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
}
func TestQuoteAttrsInTag(t *testing.T) {
cases := map[string]string{
`<svg xmlns=http://www.w3.org/2000/svg viewBox=0 0 37 37 fill=#000000>`: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 37 37" fill="#000000">`,
- `<path class=border fill=yellow stroke=green d=M29.2 21.3 0z/>`: `<path class="border" fill="yellow" stroke="green" d="M29.2 21.3 0z"/>`,
+ `<path class=border fill=yellow stroke=green d=M29.2 21.3 0z/>`: `<path class="border" fill="yellow" stroke="green" d="M29.2 21.3 0z"/>`,
`</svg>`: `</svg>`,
}
for c, exp := range cases {
--- a/js/js.go
+++ b/js/js.go
@@ -7,11 +7,11 @@
"bytes"
"context"
"fmt"
- "golang.org/x/net/html"
- "io"
"github.com/psilva261/opossum"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
+ "golang.org/x/net/html"
+ "io"
"os"
"os/exec"
"os/user"
@@ -19,7 +19,7 @@
"time"
)
-var timeout = 60*time.Second
+var timeout = 60 * time.Second
type ReadWriteCloser struct {
io.Reader
@@ -28,8 +28,8 @@
}
var (
- fetcher opossum.Fetcher
- nt *nodes.Node
+ fetcher opossum.Fetcher
+ nt *nodes.Node
fsys *client.Fsys
cancel context.CancelFunc
@@ -40,7 +40,7 @@
return
}
-func call(fn, cmd string, args... string) (resp string, err error) {
+func call(fn, cmd string, args ...string) (resp string, err error) {
if fsys == nil {
return "", fmt.Errorf("fsys nil")
}
@@ -49,9 +49,9 @@
return
}
defer fid.Close()
- fid.Write([]byte(cmd+"\n"))
+ fid.Write([]byte(cmd + "\n"))
for _, arg := range args {
- fid.Write([]byte(arg+"\n"))
+ fid.Write([]byte(arg + "\n"))
}
r := bufio.NewReader(fid)
b := bytes.NewBuffer([]byte{})
--- a/js/js_test.go
+++ b/js/js_test.go
@@ -1,12 +1,12 @@
package js
import (
- "io/ioutil"
"github.com/psilva261/opossum/browser/fs"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
"github.com/psilva261/opossum/style"
"golang.org/x/net/html"
+ "io/ioutil"
"strings"
"testing"
)
@@ -37,7 +37,9 @@
r := strings.NewReader(simpleHTML)
doc, err := html.Parse(r)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
fs.DOM = nt
fs.Update(simpleHTML, nil, []string{string(buf), script})
@@ -54,7 +56,9 @@
r = strings.NewReader(resHtm)
doc, err = html.Parse(r)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt = nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
if v := nt.Find("h1").Css("display"); v != "none" {
t.Fatalf("%v", v)
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -10,11 +10,11 @@
// Sink for Go's log pkg
var (
- Sink io.Writer
+ Sink io.Writer
quiet bool
- gl *goLog.Logger
+ gl *goLog.Logger
- Debug bool
+ Debug bool
mu sync.Mutex
last string
--- a/nodes/experimental_test.go
+++ b/nodes/experimental_test.go
@@ -1,8 +1,8 @@
package nodes
import (
- "golang.org/x/net/html"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
"strings"
"testing"
)
@@ -19,7 +19,9 @@
</body>
</html>`)
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
res, _ := nt.Query("b")
if len(res) != 1 || res[0].Data() != "b" {
@@ -29,4 +31,4 @@
if len(res) != 1 || res[0].Data() != "i" {
t.Errorf("%+v", res[0])
}
-}
\ No newline at end of file
+}
--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -3,21 +3,21 @@
import (
"bytes"
"fmt"
- "golang.org/x/net/html"
"github.com/chris-ramon/douceur/css"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
"strings"
)
type Node struct {
DomSubtree *html.Node `json:"-"`
- Text string
- Wrappable bool
- Attrs []html.Attribute
+ Text string
+ Wrappable bool
+ Attrs []html.Attribute
style.Map
Children []*Node
- parent *Node `json:"-"`
+ parent *Node `json:"-"`
}
// NewNodeTree propagates the cascading styles to the leaves
@@ -56,7 +56,7 @@
n.Text = filterText(doc.Data)
n.Map.Declarations["display"] = css.Declaration{
Property: "display",
- Value: "inline",
+ Value: "inline",
}
}
i := 0
@@ -168,7 +168,7 @@
func (n *Node) IsContainingBlock(position string) bool {
if position == "absolute" {
return n.Css("position") == "fixed" || n.Css("position") == "absolute" ||
- n.Css("position") == "relative" || n.Css("position") == "sticky" || n.Data() == "body"
+ n.Css("position") == "relative" || n.Css("position") == "sticky" || n.Data() == "body"
}
return false
}
@@ -188,7 +188,7 @@
ps = append(ps, c.FindNextPositions(position)...)
}
}
- return
+ return
}
// CB returns the Containing Block.
@@ -350,7 +350,7 @@
c := &html.Node{
Parent: d,
- Type: html.TextNode,
+ Type: html.TextNode,
}
d.FirstChild = c
@@ -357,8 +357,8 @@
n.Children = []*Node{
&Node{
DomSubtree: c,
- Wrappable: true,
- parent: n,
+ Wrappable: true,
+ parent: n,
},
}
}
@@ -397,6 +397,6 @@
fmt.Printf("%v\n", n.Data())
}
for _, c := range n.Children {
- c.printTree(r+1)
+ c.printTree(r + 1)
}
}
--- a/nodes/nodes_test.go
+++ b/nodes/nodes_test.go
@@ -3,8 +3,8 @@
import (
"bytes"
"encoding/json"
- "golang.org/x/net/html"
"github.com/psilva261/opossum/style"
+ "golang.org/x/net/html"
"strings"
"testing"
)
@@ -29,17 +29,23 @@
</body>
</html>`)
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
p := nt.Children[0].Children[1].Children[1]
a := p.Children[5]
- if q := a.QueryRef(); q != "p:nth-child(1) > a:nth-child(3)" { t.Fatalf("%v", q) }
+ if q := a.QueryRef(); q != "p:nth-child(1) > a:nth-child(3)" {
+ t.Fatalf("%v", q)
+ }
}
func TestSetText(t *testing.T) {
buf := strings.NewReader("<textarea>initial</textarea>")
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
n := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
if s := n.ContentString(false); s != "initial" {
t.Fatalf(s)
@@ -60,13 +66,15 @@
</body>
</html>`)
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
n := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
body := n.Find("body")
bodyW := body.Map.Css("width")
bodyH := body.Map.Css("height")
bodyF := body.Map.Css("font-size")
- if bodyW != "900px" || bodyH != "700px" || bodyF != "12px" {
+ if bodyW != "900px" || bodyH != "700px" || bodyF != "12px" {
t.Fatalf("<%v> w=%v h=%v f=%v", body.Data(), bodyW, bodyH, bodyF)
}
b := n.Find("b")
@@ -73,7 +81,7 @@
bW := b.Map.Css("width")
bH := b.Map.Css("height")
bF := b.Map.Css("font-size")
- if bW != "" || bH != "100px"/* || bF != "12px"*/ {
+ if bW != "" || bH != "100px" /* || bF != "12px"*/ {
t.Fatalf("<%v> w=%v h=%v f=%v", b.Data(), bW, bH, bF)
}
text := b.Children[0]
@@ -93,11 +101,13 @@
</body>
</html>`)
doc, err := html.Parse(buf)
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
n := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
body := n.Find("body")
- _=body
-
+ _ = body
+
b := bytes.NewBufferString("")
enc := json.NewEncoder(b)
if err := enc.Encode(n); err != nil {
@@ -139,7 +149,9 @@
}
for cbTag, htm := range tests {
doc, err := html.Parse(strings.NewReader(htm))
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
cb := nt.Find(cbTag)
a := nt.Find("a")
@@ -161,8 +173,8 @@
</html>
`: {
"body": {"a", "", "div", ""},
- "div": {"", ""},
- "a": {"link"},
+ "div": {"", ""},
+ "a": {"link"},
},
`
<html>
@@ -174,8 +186,8 @@
</html>
`: {
"body": {"", "div", ""},
- "div": {"a", "", ""},
- "a": {"link"},
+ "div": {"a", "", ""},
+ "a": {"link"},
},
`
<html>
@@ -188,15 +200,17 @@
</body>
</html>
`: {
- "body": {"", "main", ""},
- "main": {"a", "", "article", ""},
+ "body": {"", "main", ""},
+ "main": {"a", "", "article", ""},
"article": {"", ""},
- "a": {"link"},
+ "a": {"link"},
},
}
for htm, m := range tests {
doc, err := html.Parse(strings.NewReader(htm))
- if err != nil { t.Fatalf(err.Error()) }
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
for from, tos := range m {
t.Logf("from: %v", from)
--- a/opossum.go
+++ b/opossum.go
@@ -2,12 +2,12 @@
import (
"bytes"
+ "github.com/psilva261/opossum/logger"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/encoding/unicode"
"io/ioutil"
"mime"
- "github.com/psilva261/opossum/logger"
"net/url"
"strings"
)
@@ -23,7 +23,7 @@
type ContentType struct {
MediaType string
- Params map[string]string
+ Params map[string]string
}
// NewContentType based on mime type string and url including file extension as fallback
@@ -119,4 +119,4 @@
}
return string(buf)
-}
\ No newline at end of file
+}
--- a/opossum_test.go
+++ b/opossum_test.go
@@ -1,14 +1,14 @@
package opossum
import (
- "testing"
+ "testing"
- "github.com/chris-ramon/douceur/parser"
+ "github.com/chris-ramon/douceur/parser"
)
// aymerick douceur issues #6
func TestInfiniteLoop(t *testing.T) {
- parser.Parse(`
+ parser.Parse(`
@media ( __desktop ) {
background-color: red;
}
--- a/opossum_unix.go
+++ b/opossum_unix.go
@@ -1,3 +1,4 @@
+//go:build !plan9
// +build !plan9
package opossum
--- a/style/experimental.go
+++ b/style/experimental.go
@@ -2,13 +2,13 @@
import (
"9fans.net/go/draw"
- "github.com/chris-ramon/douceur/css"
"fmt"
+ "github.com/chris-ramon/douceur/css"
"github.com/mjl-/duit"
- "image"
"github.com/psilva261/opossum"
"github.com/psilva261/opossum/img"
"github.com/psilva261/opossum/logger"
+ "image"
"strings"
)
@@ -109,7 +109,7 @@
colors := make([]draw.Color, 0, 2)
- for i := 0; i < len(v); {
+ for i := 0; i < len(v); {
m := strings.Index(v[i:], ",")
op := strings.Index(v[i:], "(")
cl := strings.Index(v[i:], ")")
@@ -118,10 +118,10 @@
}
var arg string
if cl > 0 && op < m && m < cl {
- arg = v[i:i+cl+1]
+ arg = v[i : i+cl+1]
i += cl + 1
} else {
- arg = v[i:i+m]
+ arg = v[i : i+m]
i += m + 1
}
@@ -143,15 +143,15 @@
func linearGradient(from, to draw.Color, x, y, xmax float64) (c draw.Color) {
fr, fg, fb, fa := from.RGBA()
tr, tg, tb, ta := to.RGBA()
- d := x/xmax
+ d := x / xmax
r := uint32(float64(fr) + d*float64(tr-fr))
g := uint32(float64(fg) + d*float64(tg-fg))
b := uint32(float64(fb) + d*float64(tb-fb))
a := uint32(float64(fa) + d*float64(ta-fa))
- cc := (r/256) << 24
- cc = cc | ((g/256) << 16)
- cc = cc | ((b/256) << 8)
- cc = cc | (a/256)
+ cc := (r / 256) << 24
+ cc = cc | ((g / 256) << 16)
+ cc = cc | ((b / 256) << 8)
+ cc = cc | (a / 256)
return draw.Color(cc)
}
@@ -209,4 +209,4 @@
return i
}
return
-}
\ No newline at end of file
+}
--- a/style/experimental_test.go
+++ b/style/experimental_test.go
@@ -53,8 +53,8 @@
func TestBackgroundGradient(t *testing.T) {
values := map[string]uint32{
- "linear-gradient(to right,rgb(10,0,50,1),rgb(200,0,50,1))": 0x690032ff,
- "linear-gradient(to right,rgb(0,60,60,1),rgba(0,180,180,1))": 0x007878ff,
+ "linear-gradient(to right,rgb(10,0,50,1),rgb(200,0,50,1))": 0x690032ff,
+ "linear-gradient(to right,rgb(0,60,60,1),rgba(0,180,180,1))": 0x007878ff,
"linear-gradient(to bottom, rgba(40,40,40,1) 0%,rgba(40,40,40,1) 100%)": 0x282828ff,
}
for v, cc := range values {
--- a/style/fonts_plan9.go
+++ b/style/fonts_plan9.go
@@ -1,3 +1,4 @@
+//go:build plan9
// +build plan9
package style
@@ -7,13 +8,13 @@
"fmt"
"github.com/psilva261/opossum/logger"
"io/fs"
+ "os"
"regexp"
"strings"
- "os"
)
var (
- fonts map[int]*draw.Font
+ fonts map[int]*draw.Font
fontHs []int
)
--- a/style/fonts_unix.go
+++ b/style/fonts_unix.go
@@ -1,3 +1,4 @@
+//go:build darwin || freebsd || netbsd || openbsd || linux
// +build darwin freebsd netbsd openbsd linux
package style
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -8,9 +8,9 @@
"github.com/chris-ramon/douceur/css"
"github.com/chris-ramon/douceur/parser"
"github.com/mjl-/duit"
+ "github.com/psilva261/opossum/logger"
"golang.org/x/image/colornames"
"golang.org/x/net/html"
- "github.com/psilva261/opossum/logger"
"math"
"os/exec"
"regexp"
@@ -28,6 +28,7 @@
var rMaxWidth = regexp.MustCompile(`max-width: (\d+)(px|em|rem)`)
const FontBaseSize = 11.0
+
var WindowWidth = 1280
var WindowHeight = 1080
@@ -195,7 +196,7 @@
}
if rMaxWidth.MatchString(r.Prelude) {
m := rMaxWidth.FindStringSubmatch(r.Prelude)
- l := m[1]+m[2]
+ l := m[1] + m[2]
maxWidth, _, err := length(nil, l)
if err != nil {
return nil, nil, fmt.Errorf("atoi: %w", err)
@@ -206,7 +207,7 @@
}
if rMinWidth.MatchString(r.Prelude) {
m := rMinWidth.FindStringSubmatch(r.Prelude)
- l := m[1]+m[2]
+ l := m[1] + m[2]
minWidth, _, err := length(nil, l)
if err != nil {
return nil, nil, fmt.Errorf("atoi: %w", err)
@@ -226,7 +227,7 @@
type DomTree interface {
Parent() (p DomTree, ok bool)
- Style() Map
+ Style() Map
}
type Map struct {
@@ -264,12 +265,12 @@
s.Declarations[a.Key] = css.Declaration{
Property: a.Key,
- Value: v,
+ Value: v,
}
} else if a.Key == "bgcolor" {
s.Declarations["background-color"] = css.Declaration{
Property: "background-color",
- Value: a.Val,
+ Value: a.Val,
}
}
}
@@ -567,16 +568,16 @@
}
}
- if t, err := cs.CssPx(key+"-top"); err == nil {
+ if t, err := cs.CssPx(key + "-top"); err == nil {
s.Top = t
}
- if r, err := cs.CssPx(key+"-right"); err == nil {
+ if r, err := cs.CssPx(key + "-right"); err == nil {
s.Right = r
}
- if b, err := cs.CssPx(key+"-bottom"); err == nil {
+ if b, err := cs.CssPx(key + "-bottom"); err == nil {
s.Bottom = b
}
- if l, err := cs.CssPx(key+"-left"); err == nil {
+ if l, err := cs.CssPx(key + "-left"); err == nil {
s.Left = l
}
@@ -770,6 +771,6 @@
func (cs Map) SetCss(k, v string) {
cs.Declarations[k] = css.Declaration{
Property: k,
- Value: v,
+ Value: v,
}
}
--- a/style/stylesheets_test.go
+++ b/style/stylesheets_test.go
@@ -102,7 +102,7 @@
}
if w == 400 {
- _ =m[body][0]
+ _ = m[body][0]
if v := m[body][0].Declarations[0].Value; v != "lightblue" {
t.Fatalf("%v", v)
}
@@ -273,10 +273,10 @@
func TestCalc(t *testing.T) {
tests := map[string]float64{
- "calc(1px+2px)": 3.0,
- "calc(1px + 2px)": 3.0,
- "calc(1em+2px)": 13.0,
- "calc(1em+(2px-1px))": 12.0,
+ "calc(1px+2px)": 3.0,
+ "calc(1px + 2px)": 3.0,
+ "calc(1em+2px)": 13.0,
+ "calc(1em+(2px-1px))": 12.0,
"calc(1em+(2px-1.5px))": 11.5,
}
for x, px := range tests {
@@ -286,7 +286,7 @@
}
if f != px {
t.Fatalf("expected %v but got %v", px, f)
- }
+ }
}
}
@@ -309,14 +309,14 @@
func TestLength(t *testing.T) {
lpx := map[string]float64{
- "auto": 0.0,
+ "auto": 0.0,
"inherit": 0.0,
- "17px": 17.0,
- "10em": 110.0,
- "10ex": 110.0,
- "10vw": 128.0,
- "10vh": 108.0,
- "10%": 0,
+ "17px": 17.0,
+ "10em": 110.0,
+ "10ex": 110.0,
+ "10vw": 128.0,
+ "10vh": 108.0,
+ "10%": 0,
"101.6mm": 400,
}
for l, px := range lpx {
@@ -333,9 +333,9 @@
func TestTlbr(tt *testing.T) {
cases := map[string]duit.Space{
"1px 2px 3px 4px": duit.Space{1, 2, 3, 4},
- "1px 2px 3px": duit.Space{1, 2, 3, 2},
- "1px 2px": duit.Space{1, 2, 1, 2},
- "1px": duit.Space{1, 1, 1, 1},
+ "1px 2px 3px": duit.Space{1, 2, 3, 2},
+ "1px 2px": duit.Space{1, 2, 1, 2},
+ "1px": duit.Space{1, 1, 1, 1},
}
for v, exp := range cases {
m := Map{
@@ -375,7 +375,7 @@
color: var(--emph);
}
`
-
+
_, rv, err := FetchNodeRules(doc, css, 1280)
if err != nil {
t.Fail()
@@ -407,4 +407,3 @@
t.Fail()
}
}
-