Skip to content

Commit

Permalink
HTML: keep original single/double quote for attribute values as much …
Browse files Browse the repository at this point in the history
…as possible
  • Loading branch information
tdewolff committed May 22, 2022
1 parent f5fc0e5 commit 184bfb2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions html/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ var (
)

// EscapeAttrVal returns the escaped attribute value bytes with quotes. Either single or double quotes are used, whichever is shorter. If there are no quotes present in the value and the value is in HTML (not XML), it will return the value without quotes.
func EscapeAttrVal(buf *[]byte, orig, b []byte, isXML bool) []byte {
func EscapeAttrVal(buf *[]byte, b []byte, origQuote byte, mustQuote, isXML bool) []byte {
singles := 0
doubles := 0
unquoted := true
entities := false
for _, c := range b {
if charTable[c] {
unquoted = false
Expand All @@ -21,10 +20,14 @@ func EscapeAttrVal(buf *[]byte, orig, b []byte, isXML bool) []byte {
}
}
}
if unquoted && !isXML {
if unquoted && (!mustQuote || origQuote == 0) && !isXML {
return b
} else if !entities && len(orig) == len(b)+2 && (singles == 0 && orig[0] == '\'' || doubles == 0 && orig[0] == '"') {
return orig
} else if singles == 0 && origQuote == '\'' || doubles == 0 && origQuote == '"' {
t := (*buf)[:len(b)+2]
t[0] = origQuote
copy(t[1:], b)
t[1+len(b)] = origQuote
return t
}

n := len(b) + 2
Expand All @@ -34,6 +37,10 @@ func EscapeAttrVal(buf *[]byte, orig, b []byte, isXML bool) []byte {
n += doubles * 4
quote = '"'
escapedQuote = doubleQuoteEntityBytes
if singles == doubles && origQuote == '\'' {
quote = '\''
escapedQuote = singleQuoteEntityBytes
}
} else {
n += singles * 4
quote = '\''
Expand Down

0 comments on commit 184bfb2

Please sign in to comment.