-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.go
52 lines (42 loc) · 1.04 KB
/
css.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func fromCss(ctx Context, res *http.Response) {
Info(ctx.Depth, "Processing CSS...")
var sourceMapUrl url.URL
str, err := ioutil.ReadAll(res.Body)
if err != nil {
Error(ctx.Depth, "Failed to read css response body:", err)
return
}
startIndex := strings.LastIndex(string(str), "/*# sourceMappingURL=")
if startIndex == -1 {
// fallback if there is no embedded source maps
sourceMapUrl = ctx.Url
sourceMapUrl.Path = sourceMapUrl.Path + ".map"
} else {
after := string(str[startIndex+len("/*# sourceMappingURL="):])
endIndex := strings.LastIndex(after, "*/")
if endIndex != -1 {
after = after[:endIndex]
}
ref, err := url.Parse(after)
if err != nil {
Error(ctx.Depth, "Failed to parse CSS source map url:", err)
return
}
sourceMapUrl = *ctx.Url.ResolveReference(ref)
}
sourceMapCtx := ctx
sourceMapCtx.Url = sourceMapUrl
sourceMapCtx.Depth++
sourceMap, ok := fetch(sourceMapCtx)
if !ok {
return
}
fromSourceMap(sourceMapCtx, sourceMap)
}