From 101d3443030a9da2c7e7dcdedb37a61764e1e42b Mon Sep 17 00:00:00 2001
From: Alexander Hudek
`, ``, 1) @@ -75,7 +67,7 @@ func (a *Asset) AsCHexData() string { urlString := strings.ReplaceAll(url.String(), "/", "%2f") // Save Data uRI string - dataString = "data:text/html;charset=utf-8," + urlString + return "data:text/html;charset=utf-8," + urlString, nil case AssetTypes.CSS: @@ -91,19 +83,28 @@ func (a *Asset) AsCHexData() string { result = strings.ReplaceAll(result, `'`, `\'`) result = strings.ReplaceAll(result, ` {`, `{`) result = strings.ReplaceAll(result, `: `, `:`) - dataString = fmt.Sprintf("window.wails._.InjectCSS(\"%s\");", result) + return fmt.Sprintf("window.wails._.InjectCSS(\"%s\");", result), nil case AssetTypes.JS: m := minify.New() m.AddFunc("application/javascript", js.Minify) var err error - dataString, err = m.String("application/javascript", a.Data+";") + result, err := m.String("application/javascript", a.Data+";") if err != nil { - log.Fatal(err) + return "", err } - a.Data = dataString + return result, nil + default: + return "", fmt.Errorf("minification for asset type %s not implemented", a.Type) } +} +// AsCHexData processes the asset data so it may be used by C +func (a *Asset) AsCHexData() string { + dataString, err := a.minifiedData() + if err != nil { + log.Fatal(err) + } // Get byte data of the string bytes := *(*[]byte)(unsafe.Pointer(&dataString)) diff --git a/v2/internal/html/asset_test.go b/v2/internal/html/asset_test.go new file mode 100644 index 000000000..f743b4ba9 --- /dev/null +++ b/v2/internal/html/asset_test.go @@ -0,0 +1,53 @@ +package html + +import "testing" + +func TestAsset_minifiedData(t *testing.T) { + type fields struct { + Type string + Path string + Data string + } + tests := []struct { + name string + fields fields + want string + wantErr bool + }{ + { + name: "multi-line tag", + fields: fields{ + Type: AssetTypes.HTML, + Path: "foo.html", + Data: "\n", + }, + want: "data:text/html;charset=utf-8,%3Clink%20rel=%22stylesheet%22%20href=%22src%2ffoo.css%22%20%3E%20", + }, + { + name: "multi-line tag no spaces", + fields: fields{ + Type: AssetTypes.HTML, + Path: "foo.html", + Data: "\n", + }, + want: "data:text/html;charset=utf-8,%3Clink%20rel=%22stylesheet%22%20href=%22src%2ffoo.css%22%20%3E%20", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Asset{ + Type: tt.fields.Type, + Path: tt.fields.Path, + Data: tt.fields.Data, + } + got, err := a.minifiedData() + if (err != nil) != tt.wantErr { + t.Errorf("Asset.minifiedData() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Asset.minifiedData() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/v2/internal/html/assetbundle.go b/v2/internal/html/assetbundle.go index cf56bb40e..0079f2b85 100644 --- a/v2/internal/html/assetbundle.go +++ b/v2/internal/html/assetbundle.go @@ -84,7 +84,7 @@ func (a *AssetBundle) processHTML(htmldata string) error { } //process the token according to the token type... - if tokenType == html.StartTagToken { + if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken { //get the token token := tokenizer.Token() diff --git a/v2/internal/html/assetbundle_test.go b/v2/internal/html/assetbundle_test.go new file mode 100644 index 000000000..bbd70eaa4 --- /dev/null +++ b/v2/internal/html/assetbundle_test.go @@ -0,0 +1,73 @@ +package html + +import ( + "testing" +) + +func TestNewAssetBundle(t *testing.T) { + tests := []struct { + name string + pathToHTML string + wantAssets []string + wantErr bool + }{ + { + name: "basic html", + pathToHTML: "testdata/basic.html", + wantAssets: []string{ + AssetTypes.HTML, + AssetTypes.FAVICON, + AssetTypes.JS, + AssetTypes.CSS, + }, + wantErr: false, + }, + { + name: "self closing tags", + pathToHTML: "testdata/self_closing.html", + wantAssets: []string{ + AssetTypes.HTML, + AssetTypes.FAVICON, + AssetTypes.JS, + AssetTypes.CSS, + }, + wantErr: false, + }, + { + name: "multi-line tags", + pathToHTML: "testdata/self_closing.html", + wantAssets: []string{ + AssetTypes.HTML, + AssetTypes.FAVICON, + AssetTypes.JS, + AssetTypes.CSS, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NewAssetBundle(tt.pathToHTML) + if (err != nil) != tt.wantErr { + t.Errorf("NewAssetBundle() error = %v, wantErr %v", err, tt.wantErr) + return + } + if len(got.assets) != len(tt.wantAssets) { + t.Errorf("NewAssetBundle() len(assets) = %d, want %d", + len(got.assets), len(tt.wantAssets)) + } + + for i := range tt.wantAssets { + if i >= len(got.assets) { + t.Errorf("NewAssetBundle() missing assets[%d].Type = %s", + i, tt.wantAssets[i]) + } else { + if got.assets[i].Type != tt.wantAssets[i] { + t.Errorf("NewAssetBundle() assets[%d].Type = %s, want %s", + i, got.assets[i].Type, tt.wantAssets[i]) + } + } + } + }) + } +} diff --git a/v2/internal/html/testdata/basic.html b/v2/internal/html/testdata/basic.html new file mode 100644 index 000000000..cdc3770b8 --- /dev/null +++ b/v2/internal/html/testdata/basic.html @@ -0,0 +1,14 @@ + + +
+ + + + + +
+ +
+