Skip to content

Commit

Permalink
fix font asset inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-gaitzsch committed Nov 14, 2023
1 parent 21f2a9a commit ca1b115
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 13 additions & 5 deletions utils/html-css.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils
import (
"context"
"encoding/base64"
"io/ioutil"
"io"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -42,7 +42,7 @@ func MergeCss(css ...*string) *string {
return &mergedCss
}

var urlReferenceRegex = regexp.MustCompile(` (src|href)="([^"]+)"`)
var urlReferenceRegex = regexp.MustCompile(`( src="| href="|src: *url\(")([^"]+)("\)|")`)

type HttpClientExecuter interface {
Do(req *http.Request) (*http.Response, error)
Expand All @@ -65,8 +65,9 @@ func RequestAndInlineAllHtmlResources(ctx context.Context, htmlPtr *string, base
func requestAndReturnBase64IfPossible(ctx context.Context, htmlAttribute string, baseUrl string, logger *zerolog.Logger) string {

groups := urlReferenceRegex.FindAllStringSubmatch(htmlAttribute, 2)
attribute := groups[0][1]
prefix := groups[0][1]
src := groups[0][2]
suffix := groups[0][3]

if baseUrl != "" && !strings.HasPrefix(src, "http") {
src = baseUrl + src
Expand All @@ -90,7 +91,7 @@ func requestAndReturnBase64IfPossible(ctx context.Context, htmlAttribute string,
}
defer response.Body.Close()

bytes, err := ioutil.ReadAll(response.Body)
bytes, err := io.ReadAll(response.Body)
if err != nil {
logger.Info().Str("resourceUrl", src).Err(err).Msg("cant fetch resource: cant read from response body")
return htmlAttribute
Expand All @@ -101,5 +102,12 @@ func requestAndReturnBase64IfPossible(ctx context.Context, htmlAttribute string,
mimeType := http.DetectContentType(bytes)
base64 := base64.StdEncoding.EncodeToString(bytes)

return " " + attribute + "=\"data:" + mimeType + ";base64," + base64 + "\""
isCssSrcUrl := strings.HasSuffix(prefix, `url("`)

if (isCssSrcUrl) {
prefix = strings.TrimRight(prefix,`"`)
suffix = strings.TrimLeft(suffix,`"`)
}

return prefix + "data:" + mimeType + ";base64," + base64 + suffix
}
12 changes: 8 additions & 4 deletions utils/html-css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *HttpClientMock) Do(req *http.Request) (*http.Response, error) {
}

func TestRequestAndInlineAllHtmlResources(t *testing.T) {
inputHtml := "<body>Hello World <img src=\"http://my-image.org/test1.png\"/> <img src=\"test2.png\"/> </body>"
inputHtml := "<head><style>@font-face{ src: url(\"http://my-font.org/font.eot\"); }</style></head> <body>Hello World <img src=\"http://my-image.org/test1.png\"/> <img src=\"test2.png\"/> </body>"

httpClientMock := &HttpClientMock{}
ctx := context.WithValue(context.Background(), "httpClient", httpClientMock)
Expand All @@ -85,15 +85,19 @@ func TestRequestAndInlineAllHtmlResources(t *testing.T) {
t.Fatal("Result should not be nil")
}

if httpClientMock.RequestedResources[0] != "http://my-image.org/test1.png" {
if httpClientMock.RequestedResources[0] != "http://my-font.org/font.eot" {
t.Fatal("Font was not requested")
}

if httpClientMock.RequestedResources[1] != "http://my-image.org/test1.png" {
t.Fatal("First image was not requested")
}

if httpClientMock.RequestedResources[1] != "http://localhost:1234/test2.png" {
if httpClientMock.RequestedResources[2] != "http://localhost:1234/test2.png" {
t.Fatal("Second image was not requested")
}

expectedHtml := "<body>Hello World <img src=\"data:text/plain; charset=utf-8;base64,Zm9v\"/> <img src=\"data:text/plain; charset=utf-8;base64,Zm9v\"/> </body>"
expectedHtml := "<head><style>@font-face{ src: url(data:text/plain; charset=utf-8;base64,Zm9v); }</style></head> <body>Hello World <img src=\"data:text/plain; charset=utf-8;base64,Zm9v\"/> <img src=\"data:text/plain; charset=utf-8;base64,Zm9v\"/> </body>"

if *outputPtr != expectedHtml {
t.Fatalf("Result was not expected: %s != %s", *outputPtr, expectedHtml)
Expand Down

0 comments on commit ca1b115

Please sign in to comment.