Commit d29ed92d authored by Samuel Tan's avatar Samuel Tan Committed by Daniel Martí

html/template: fix lint errors

Change-Id: If56bd72917a9cbf5920ae8b5a36dc67f10959b94
Reviewed-on: https://go-review.googlesource.com/103175
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
parent 9364c13d
...@@ -37,15 +37,15 @@ func urlFilter(args ...interface{}) string { ...@@ -37,15 +37,15 @@ func urlFilter(args ...interface{}) string {
if t == contentTypeURL { if t == contentTypeURL {
return s return s
} }
if !isSafeUrl(s) { if !isSafeURL(s) {
return "#" + filterFailsafe return "#" + filterFailsafe
} }
return s return s
} }
// isSafeUrl is true if s is a relative URL or if URL has a protocol in // isSafeURL is true if s is a relative URL or if URL has a protocol in
// (http, https, mailto). // (http, https, mailto).
func isSafeUrl(s string) bool { func isSafeURL(s string) bool {
if i := strings.IndexRune(s, ':'); i >= 0 && !strings.ContainsRune(s[:i], '/') { if i := strings.IndexRune(s, ':'); i >= 0 && !strings.ContainsRune(s[:i], '/') {
protocol := s[:i] protocol := s[:i]
...@@ -79,15 +79,15 @@ func urlProcessor(norm bool, args ...interface{}) string { ...@@ -79,15 +79,15 @@ func urlProcessor(norm bool, args ...interface{}) string {
norm = true norm = true
} }
var b bytes.Buffer var b bytes.Buffer
if processUrlOnto(s, norm, &b) { if processURLOnto(s, norm, &b) {
return b.String() return b.String()
} }
return s return s
} }
// processUrlOnto appends a normalized URL corresponding to its input to b // processURLOnto appends a normalized URL corresponding to its input to b
// and returns true if the appended content differs from s. // and returns true if the appended content differs from s.
func processUrlOnto(s string, norm bool, b *bytes.Buffer) bool { func processURLOnto(s string, norm bool, b *bytes.Buffer) bool {
b.Grow(b.Cap() + len(s) + 16) b.Grow(b.Cap() + len(s) + 16)
written := 0 written := 0
// The byte loop below assumes that all URLs use UTF-8 as the // The byte loop below assumes that all URLs use UTF-8 as the
...@@ -152,7 +152,7 @@ func srcsetFilterAndEscaper(args ...interface{}) string { ...@@ -152,7 +152,7 @@ func srcsetFilterAndEscaper(args ...interface{}) string {
// Normalizing gets rid of all HTML whitespace // Normalizing gets rid of all HTML whitespace
// which separate the image URL from its metadata. // which separate the image URL from its metadata.
var b bytes.Buffer var b bytes.Buffer
if processUrlOnto(s, true, &b) { if processURLOnto(s, true, &b) {
s = b.String() s = b.String()
} }
// Additionally, commas separate one source from another. // Additionally, commas separate one source from another.
...@@ -173,43 +173,43 @@ func srcsetFilterAndEscaper(args ...interface{}) string { ...@@ -173,43 +173,43 @@ func srcsetFilterAndEscaper(args ...interface{}) string {
} }
// Derived from https://play.golang.org/p/Dhmj7FORT5 // Derived from https://play.golang.org/p/Dhmj7FORT5
const htmlSpaceAndAsciiAlnumBytes = "\x00\x36\x00\x00\x01\x00\xff\x03\xfe\xff\xff\x07\xfe\xff\xff\x07" const htmlSpaceAndASCIIAlnumBytes = "\x00\x36\x00\x00\x01\x00\xff\x03\xfe\xff\xff\x07\xfe\xff\xff\x07"
// isHtmlSpace is true iff c is a whitespace character per // isHTMLSpace is true iff c is a whitespace character per
// https://infra.spec.whatwg.org/#ascii-whitespace // https://infra.spec.whatwg.org/#ascii-whitespace
func isHtmlSpace(c byte) bool { func isHTMLSpace(c byte) bool {
return (c <= 0x20) && 0 != (htmlSpaceAndAsciiAlnumBytes[c>>3]&(1<<uint(c&0x7))) return (c <= 0x20) && 0 != (htmlSpaceAndASCIIAlnumBytes[c>>3]&(1<<uint(c&0x7)))
} }
func isHtmlSpaceOrAsciiAlnum(c byte) bool { func isHTMLSpaceOrAsciiAlnum(c byte) bool {
return (c < 0x80) && 0 != (htmlSpaceAndAsciiAlnumBytes[c>>3]&(1<<uint(c&0x7))) return (c < 0x80) && 0 != (htmlSpaceAndASCIIAlnumBytes[c>>3]&(1<<uint(c&0x7)))
} }
func filterSrcsetElement(s string, left int, right int, b *bytes.Buffer) { func filterSrcsetElement(s string, left int, right int, b *bytes.Buffer) {
start := left start := left
for start < right && isHtmlSpace(s[start]) { for start < right && isHTMLSpace(s[start]) {
start += 1 start++
} }
end := right end := right
for i := start; i < right; i++ { for i := start; i < right; i++ {
if isHtmlSpace(s[i]) { if isHTMLSpace(s[i]) {
end = i end = i
break break
} }
} }
if url := s[start:end]; isSafeUrl(url) { if url := s[start:end]; isSafeURL(url) {
// If image metadata is only spaces or alnums then // If image metadata is only spaces or alnums then
// we don't need to URL normalize it. // we don't need to URL normalize it.
metadataOk := true metadataOk := true
for i := end; i < right; i++ { for i := end; i < right; i++ {
if !isHtmlSpaceOrAsciiAlnum(s[i]) { if !isHTMLSpaceOrAsciiAlnum(s[i]) {
metadataOk = false metadataOk = false
break break
} }
} }
if metadataOk { if metadataOk {
b.WriteString(s[left:start]) b.WriteString(s[left:start])
processUrlOnto(url, true, b) processURLOnto(url, true, b)
b.WriteString(s[end:right]) b.WriteString(s[end:right])
return return
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment