shithub: mycel

Download patch

ref: aa74144236a4014954fd58c94758a4cec6fe3094
parent: 218bfd840e747fb26788ca58427892332c23d76a
author: Philip Silva <philip.silva@protonmail.com>
date: Sun May 2 09:59:10 EDT 2021

Parse also style tags

--- a/browser/website.go
+++ b/browser/website.go
@@ -73,27 +73,7 @@
 
 	log.Printf("2nd pass")
 	log.Printf("Download style...")
-	cssHrefs := style.Hrefs(doc)
-	csss := make([]string, 0, len(cssHrefs))
-	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)
-		}
-	}
-	csss = append([]string{style.AddOnCSS}, csss...)
+	csss := cssSrcs(f, doc)
 	doc, nodeMap := pass(w.html, csss...)
 
 	// 3rd pass is only needed initially to load the scripts and set the goja VM
@@ -167,6 +147,40 @@
 		w.UI = scroller
 	}
 	log.Flush()
+}
+
+func cssSrcs(f opossum.Fetcher, doc *html.Node) (csss []string) {
+	cssHrefs := style.Hrefs(doc)
+	inlines := make([]string, 0, 3)
+	ntAll := nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
+	inls := ntAll.FindAll("style")
+
+	for _, inl := range inls {
+		inlines = append(inlines, inl.ContentString(true))
+	}
+	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
 }
 
 func formData(n, submitBtn *html.Node) (data url.Values) {