Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-25 19:06:20 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-25 19:06:20 +0300
commitbb232a351ac4ceb488d06f45ef230e457be26f40 (patch)
treec5ce77034d156f082892e1d141c9cfb9a7ca6190 /resources
parent3854a6fa6c323d1c09aa71a0626c9eef62709294 (diff)
resources: Improve error message on .Resize etc. on SVGs
Fixes #9875
Diffstat (limited to 'resources')
-rw-r--r--resources/integration_test.go27
-rw-r--r--resources/transform.go6
2 files changed, 32 insertions, 1 deletions
diff --git a/resources/integration_test.go b/resources/integration_test.go
index 19cf8c198..92abcb612 100644
--- a/resources/integration_test.go
+++ b/resources/integration_test.go
@@ -17,11 +17,13 @@ import (
"strings"
"testing"
+ qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
)
// Issue 8931
func TestImageCache(t *testing.T) {
+ t.Parallel()
files := `
-- config.toml --
@@ -67,3 +69,28 @@ bmp: {{ $bmp.RelPermalink }}|{{ $bmp.MediaType }}|
assertImages()
}
+
+func TestSVGError(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+-- assets/circle.svg --
+<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>
+-- layouts/index.html --
+{{ $svg := resources.Get "circle.svg" }}
+Width: {{ $svg.Width }}
+`
+
+ b, err := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ NeedsOsFS: true,
+ Running: true,
+ }).BuildE()
+
+ b.Assert(err, qt.IsNotNil)
+ b.Assert(err.Error(), qt.Contains, `error calling Width: this method is only available for raster images. To determine if an image is SVG, you can do {{ if eq .MediaType.SubType "svg" }}{{ end }}`)
+
+}
diff --git a/resources/transform.go b/resources/transform.go
index bb1608cbd..7d81f9b21 100644
--- a/resources/transform.go
+++ b/resources/transform.go
@@ -297,7 +297,11 @@ func (r *resourceAdapter) DecodeImage() (image.Image, error) {
func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
img, ok := r.target.(images.ImageResourceOps)
if !ok {
- panic(fmt.Sprintf("%T is not an image", r.target))
+ if r.MediaType().SubType == "svg" {
+ panic("this method is only available for raster images. To determine if an image is SVG, you can do {{ if eq .MediaType.SubType \"svg\" }}{{ end }}")
+ }
+ fmt.Println(r.MediaType().SubType)
+ panic("this method is only available for image resources")
}
r.init(false, false)
return img