Esempio n. 1
0
func BenchmarkCSSRelative1(b *testing.B) {
	bs := bytes.NewBuffer([]byte(`body{background: url("../test")}`))
	o := &bytes.Buffer{}
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		_ = urlrw.CSSRelative(root, o, bs)
	}
}
Esempio n. 2
0
func Example() {
	// File data: body{background: url('../img/test.jpg')}
	f, err := os.Open("test_ref/css.css")
	if err != nil {
		fmt.Println(err)
	}
	buf := &bytes.Buffer{}

	err = urlrw.CSSRelative("/root/sub", buf, f)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(string(buf.Bytes()))
	// Output: body{background: url('/root/img/test.jpg')}
}
Esempio n. 3
0
func TestCSSRelative(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	ts := []struct {
		css  string
		root string
		rw   string
	}{
		{
			`body{background: url(../test)}`, root,
			`body{background: url(` + path.Clean(root+`/../test`) + `)}`,
		},
		{
			`body{background: url('../test')}`, root,
			`body{background: url('` + path.Clean(root+`/../test`) + `')}`,
		},
		{
			`body{background: url('../test')}`, root + `/`,
			`body{background: url('` + path.Clean(root+`/../test`) + `')}`,
		},
		{
			`body{background: url("./test")}`, root,
			`body{background: url("` + path.Clean(root+`/./test`) + `")}`,
		},
		{
			`body{background: url("/test")}`, root,
			`body{background: url("/test")}`,
		},
		{
			`body{background: url("/test")}`, root + `/`,
			`body{background: url("/test")}`,
		},
		{
			`body{background: url(data:test)}`, root,
			`body{background: url(data:test)}`,
		},
		{
			`body{background: url("../test")}div{background:url('../another')}`,
			root,
			`body{background: url("` + path.Clean(root+`/../test`) + `")}div{background:url('` + path.Clean(root+`/../another`) + `')}`,
		},
	}

	for _, v := range ts {
		b := bytes.NewBuffer([]byte(v.css))
		o := &bytes.Buffer{}
		err := urlrw.CSSRelative(v.root, o, b)
		if err != nil {
			t.Fatal(err)
		}
		data, err := ioutil.ReadAll(o)
		if err != nil {
			t.Fatal(err)
		}

		want := v.rw
		got := string(data)
		if got != want {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}