ref: f548ead614a4ee6fda902670100b49c17070c81b
parent: 069daee6347a509dab3c109291cfd35956229b91
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Jan 29 12:33:01 EST 2022
keep sorting of css srcs
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1520,7 +1520,7 @@
tr.MaxIdleConnsPerHost = 6
b = &Browser{ client: &http.Client{- Jar: jar,
+ Jar: jar,
Transport: tr,
},
dui: _dui,
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -72,7 +72,7 @@
}
m.Declarations["display"] = style.Declaration{Prop: "display",
- Val: d,
+ Val: d,
}
h1.Map = m
h2.Map = m
--- a/browser/website.go
+++ b/browser/website.go
@@ -163,40 +163,39 @@
log.Flush()
}
-func cssSrcs(f opossum.Fetcher, doc *html.Node) (csss []string) {- // TODO: keep order of inline and referenced files
- cssHrefs := style.Hrefs(doc)
- inlines := make([]string, 0, 3)
+func cssSrcs(f opossum.Fetcher, doc *html.Node) (srcs []string) {+ srcs = make([]string, 0, 20)
+ srcs = append(srcs, style.AddOnCSS)
ntAll := nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)- inls := ntAll.FindAll("style")-
- for _, inl := range inls {- if t := strings.ToLower(inl.Attr("type")); t == "" || t == "text/css" {- inlines = append(inlines, inl.ContentString(true))
+ ntAll.Traverse(func(r int, n *nodes.Node) {+ switch n.Data() {+ case "style":
+ if t := strings.ToLower(n.Attr("type")); t == "" || t == "text/css" {+ srcs = append(srcs, n.ContentString(true))
+ }
+ case "link":
+ isStylesheet := n.Attr("rel") == "stylesheet"+ isPrint := n.Attr("media") == "print"+ href := n.Attr("href")+ if isStylesheet && !isPrint {+ url, err := f.LinkedUrl(href)
+ if err != nil {+ log.Errorf("error parsing %v", href)+ return
+ }
+ buf, contentType, err := f.Get(url)
+ if err != nil {+ log.Errorf("error downloading %v", url)+ return
+ }
+ if contentType.IsCSS() {+ srcs = append(srcs, string(buf))
+ } else {+ log.Printf("css: unexpected %v", contentType)+ }
+ }
}
- }
- csss = make([]string, 0, len(inlines)+len(cssHrefs))
- csss = append(csss, style.AddOnCSS)
- csss = append(csss, inlines...)
- for _, href := range cssHrefs {- url, err := f.LinkedUrl(href)
- if err != nil {- log.Printf("error parsing %v", href)- continue
- }
- log.Printf("Download %v", url)- buf, contentType, err := f.Get(url)
- if err != nil {- log.Printf("error downloading %v", url)- continue
- }
- if contentType.IsCSS() {- csss = append(csss, string(buf))
- } else {- log.Printf("css: unexpected %v", contentType)- }
- }
-
+ })
return
}
--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -381,34 +381,40 @@
return
}
-func (n *Node) PrintTree() {- n.printTree(0)
+func (n *Node) Traverse(f func(r int, c *Node)) {+ n.traverse(0, f)
}
-func (n *Node) printTree(r int) {- for i := 0; i < r; i++ {- fmt.Printf(" ")+func (n *Node) traverse(rr int, f func(r int, c *Node)) {+ f(rr, n)
+ for _, c := range n.Children {+ c.traverse(rr+1, f)
}
- if n.Type() == html.ElementNode {- sty := ""
- if len(n.Map.Declarations) > 0 {- l := make([]string, 0, 2)
- for k, d := range n.Map.Declarations {- s := fmt.Sprintf("%v=%v", k, d.Val)- if d.Important {- s += "!"
+}
+
+func (n *Node) PrintTree() {+ n.Traverse(func(r int, n *Node) {+ for i := 0; i < r; i++ {+ fmt.Printf(" ")+ }
+ if n.Type() == html.ElementNode {+ sty := ""
+ if len(n.Map.Declarations) > 0 {+ l := make([]string, 0, 2)
+ for k, d := range n.Map.Declarations {+ s := fmt.Sprintf("%v=%v", k, d.Val)+ if d.Important {+ s += "!"
+ }
+ l = append(l, s)
}
- l = append(l, s)
+ sty += ` style="` + strings.Join(l, " ") + `"`
}
- sty += ` style="` + strings.Join(l, " ") + `"`
+ fmt.Printf("<%v%v>\n", n.Data(), sty)+ } else if n.Type() == html.TextNode {+ fmt.Printf("\"%v\"\n", strings.TrimSpace(n.Data()))+ } else {+ fmt.Printf("%v\n", n.Data())}
- fmt.Printf("<%v%v>\n", n.Data(), sty)- } else if n.Type() == html.TextNode {- fmt.Printf("\"%v\"\n", strings.TrimSpace(n.Data()))- } else {- fmt.Printf("%v\n", n.Data())- }
- for _, c := range n.Children {- c.printTree(r + 1)
- }
+ })
}
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -63,43 +63,6 @@
initFontserver()
}
-func Hrefs(doc *html.Node) (hrefs []string) {- hrefs = make([]string, 0, 3)
-
- var f func(n *html.Node)
- f = func(n *html.Node) {- if n.Type == html.ElementNode && n.Data == "link" {- isStylesheet := false
- isPrint := false
- href := ""
-
- for _, a := range n.Attr {- switch strings.ToLower(a.Key) {- case "rel":
- if a.Val == "stylesheet" {- isStylesheet = true
- }
- case "href":
- href = a.Val
- case "media":
- isPrint = a.Val == "print"
- }
- }
-
- if isStylesheet && !isPrint {- hrefs = append(hrefs, href)
- }
- }
- for c := n.FirstChild; c != nil; c = c.NextSibling {- f(c)
- }
- }
-
- f(doc)
-
- return
-}
-
func MergeNodeMaps(m, addOn map[*html.Node]Map) { for n, mp := range addOn {// "zero" valued Map if it doesn't exist yet
--
⑨