63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package xml // import "github.com/tdewolff/parse/xml"
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/tdewolff/test"
|
|
)
|
|
|
|
func TestEscapeAttrVal(t *testing.T) {
|
|
var attrValTests = []struct {
|
|
attrVal string
|
|
expected string
|
|
}{
|
|
{"xyz", "\"xyz\""},
|
|
{"", "\"\""},
|
|
{"x&z", "\"x&z\""},
|
|
{"x'z", "\"x'z\""},
|
|
{"x\"z", "'x\"z'"},
|
|
{"a'b=\"\"", "'a'b=\"\"'"},
|
|
{"'x'\"'z'", "\"x'"'z\""},
|
|
{"\"x"'"z\"", "'x\"'\"z'"},
|
|
{"a'b=\"\"", "'a'b=\"\"'"},
|
|
}
|
|
var buf []byte
|
|
for _, tt := range attrValTests {
|
|
t.Run(tt.attrVal, func(t *testing.T) {
|
|
b := []byte(tt.attrVal)
|
|
if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] {
|
|
b = b[1 : len(b)-1]
|
|
}
|
|
val := EscapeAttrVal(&buf, []byte(b))
|
|
test.String(t, string(val), tt.expected)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEscapeCDATAVal(t *testing.T) {
|
|
var CDATAValTests = []struct {
|
|
CDATAVal string
|
|
expected string
|
|
}{
|
|
{"<![CDATA[<b>]]>", "<b>"},
|
|
{"<![CDATA[abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz]]>", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"},
|
|
{"<![CDATA[ <b> ]]>", " <b> "},
|
|
{"<![CDATA[<<<<<]]>", "<![CDATA[<<<<<]]>"},
|
|
{"<![CDATA[&]]>", "&"},
|
|
{"<![CDATA[&&&&]]>", "<![CDATA[&&&&]]>"},
|
|
{"<![CDATA[ a ]]>", " a "},
|
|
{"<![CDATA[]]>", ""},
|
|
}
|
|
var buf []byte
|
|
for _, tt := range CDATAValTests {
|
|
t.Run(tt.CDATAVal, func(t *testing.T) {
|
|
b := []byte(tt.CDATAVal[len("<![CDATA[") : len(tt.CDATAVal)-len("]]>")])
|
|
data, useText := EscapeCDATAVal(&buf, b)
|
|
text := string(data)
|
|
if !useText {
|
|
text = "<![CDATA[" + text + "]]>"
|
|
}
|
|
test.String(t, text, tt.expected)
|
|
})
|
|
}
|
|
}
|