File: util_test.go

package info (click to toggle)
golang-github-tdewolff-parse 2.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 888 kB
  • sloc: makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package 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"`},
		{`a'b=""`, `'a'b=""'`},
		{`x<z`, `"x<z"`},
		{`'x"'"z'`, `'x"&#39;"z'`},
	}
	var buf []byte
	for _, tt := range escapeAttrValTests {
		t.Run(tt.attrVal, func(t *testing.T) {
			b := []byte(tt.attrVal)
			var quote byte
			if 0 < len(b) && (b[0] == '\'' || b[0] == '"') {
				quote = b[0]
			}
			if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] {
				b = b[1 : len(b)-1]
			}
			val := EscapeAttrVal(&buf, b, quote, false)
			test.String(t, string(val), tt.expected)
		})
	}
}

func TestEscapeAttrValXML(t *testing.T) {
	var escapeAttrValTests = []struct {
		attrVal  string
		expected string
	}{
		{`"xyz"`, `"xyz"`},
		{`'xyz'`, `'xyz'`},
		{`xyz`, `xyz`},
		{``, ``},
	}
	var buf []byte
	for _, tt := range escapeAttrValTests {
		t.Run(tt.attrVal, func(t *testing.T) {
			b := []byte(tt.attrVal)
			var quote byte
			if 0 < len(b) && (b[0] == '\'' || b[0] == '"') {
				quote = b[0]
			}
			if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] {
				b = b[1 : len(b)-1]
			}
			val := EscapeAttrVal(&buf, b, quote, true)
			test.String(t, string(val), tt.expected)
		})
	}
}