diff --git a/html/util_test.go b/html/util_test.go index 03dd2ca..4745b06 100644 --- a/html/util_test.go +++ b/html/util_test.go @@ -32,4 +32,5 @@ func TestAttrVal(t *testing.T) { assertAttrVal(t, "'x"z'", "'x\"z'") assertAttrVal(t, "'x\">'", "'x\">'") assertAttrVal(t, "You're encouraged to log in; however, it's not mandatory. [o]", "\"You're encouraged to log in; however, it's not mandatory. [o]\"") + assertAttrVal(t, "a'b=\"\"", "'a'b=\"\"'") } diff --git a/xml/util.go b/xml/util.go index 4442135..10192c7 100644 --- a/xml/util.go +++ b/xml/util.go @@ -29,19 +29,22 @@ func EscapeAttrVal(buf *[]byte, b []byte) []byte { } } + n := len(b) + 2 var quote byte var escapedQuote []byte if doubles > singles { + n += singles * 4 quote = '\'' escapedQuote = singleQuoteEntityBytes } else { + n += doubles * 4 quote = '"' escapedQuote = doubleQuoteEntityBytes } - if len(b)+2 > cap(*buf) { - *buf = make([]byte, 0, len(b)+2) // maximum size, not actual size + if n > cap(*buf) { + *buf = make([]byte, 0, n) // maximum size, not actual size } - t := (*buf)[:len(b)+2] // maximum size, not actual size + t := (*buf)[:n] // maximum size, not actual size t[0] = quote j := 1 start := 0 diff --git a/xml/util_test.go b/xml/util_test.go index f4664f7..7689551 100644 --- a/xml/util_test.go +++ b/xml/util_test.go @@ -19,4 +19,5 @@ func TestAttrVal(t *testing.T) { assertAttrVal(t, "x&z", "\"x&z\"") assertAttrVal(t, "x'z", "\"x'z\"") assertAttrVal(t, "x\"z", "'x\"z'") + assertAttrVal(t, "a'b=\"\"", "'a'b=\"\"'") }