Add ability to set custom stylesheets for SVG renderer (#105)

* Add ability to set custom stylesheets for SVG renderer

This allow to set custom inline CSS and a optional CSP nonce. This
solves the problem mentioned in #103 and is best used with it, as seen
in the added examples. Without this one would have to write a custom
renderer.

* Add note with link to the custom_stylesheets example
This commit is contained in:
Justin Kromlinger 2018-10-12 18:43:30 +02:00 committed by Will Charczuk
parent 96acfc6a9f
commit 3cb33d48d3
5 changed files with 168 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package chart
import (
"bytes"
"fmt"
"strings"
"testing"
@ -89,3 +90,30 @@ func TestCanvasClassSVG(t *testing.T) {
as.Equal("class=\"test-class\"", canvas.styleAsSVG(set))
}
func TestCanvasCustomInlineStylesheet(t *testing.T) {
b := strings.Builder{}
canvas := &canvas{
w: &b,
css: ".background { fill: red }",
}
canvas.Start(200, 200)
assert.New(t).Contains(b.String(), fmt.Sprintf(`<style type="text/css"><![CDATA[%s]]></style>`, canvas.css))
}
func TestCanvasCustomInlineStylesheetWithNonce(t *testing.T) {
b := strings.Builder{}
canvas := &canvas{
w: &b,
css: ".background { fill: red }",
nonce: "RAND0MSTRING",
}
canvas.Start(200, 200)
assert.New(t).Contains(b.String(), fmt.Sprintf(`<style type="text/css" nonce="%s"><![CDATA[%s]]></style>`, canvas.nonce, canvas.css))
}