aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/tdewolff/parse/html/util_test.go
blob: 3722a08870b2589cbb0dfd8428103476173ab834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package html // import "github.com/tdewolff/parse/html"

import (
	"testing"

	"github.com/tdewolff/test"
)

func TestEscapeAttrVal(t *testing.T) {
	var escapeAttrValTests = []struct {
		attrVal  string
		expected string
	}{
		{"xyz", "xyz"},
		{"", ""},
		{"x&z", "x&z"},
		{"x/z", "x/z"},
		{"x'z", "\"x'z\""},
		{"x\"z", "'x\"z'"},
		{"'x\"z'", "'x\"z'"},
		{"'x'\"'z'", "\"x'"'z\""},
		{"\"x"'"z\"", "'x\"'\"z'"},
		{"\"x'z\"", "\"x'z\""},
		{"'x"z'", "'x\"z'"},
		{"'x\">'", "'x\">'"},
		{"You're encouraged to log in; however, it's not mandatory. [o]", "\"You're encouraged to log in; however, it's not mandatory. [o]\""},
		{"a'b=\"\"", "'a'b=\"\"'"},
		{"x<z", "\"x<z\""},
		{"'x\"&#39;\"z'", "'x\"&#39;\"z'"},
	}
	var buf []byte
	for _, tt := range escapeAttrValTests {
		t.Run(tt.attrVal, func(t *testing.T) {
			b := []byte(tt.attrVal)
			orig := b
			if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] {
				b = b[1 : len(b)-1]
			}
			val := EscapeAttrVal(&buf, orig, []byte(b))
			test.String(t, string(val), tt.expected)
		})
	}
}