summaryrefslogtreecommitdiff
path: root/src/html
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-09-22 10:46:32 -0400
committerRuss Cox <rsc@golang.org>2021-10-06 15:53:04 +0000
commit4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee (patch)
tree1e850efb295d4c5f0589e46bd8d9f1930d4af0b5 /src/html
parent8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff)
downloadgo-git-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.tar.gz
all: use bytes.Cut, strings.Cut
Many uses of Index/IndexByte/IndexRune/Split/SplitN can be written more clearly using the new Cut functions. Do that. Also rewrite to other functions if that's clearer. For #46336. Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448 Reviewed-on: https://go-review.googlesource.com/c/go/+/351711 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/html')
-rw-r--r--src/html/template/attr.go6
-rw-r--r--src/html/template/js.go4
-rw-r--r--src/html/template/url.go4
3 files changed, 5 insertions, 9 deletions
diff --git a/src/html/template/attr.go b/src/html/template/attr.go
index 22922e6038..6c52211fed 100644
--- a/src/html/template/attr.go
+++ b/src/html/template/attr.go
@@ -143,12 +143,12 @@ func attrType(name string) contentType {
// widely applied.
// Treat data-action as URL below.
name = name[5:]
- } else if colon := strings.IndexRune(name, ':'); colon != -1 {
- if name[:colon] == "xmlns" {
+ } else if prefix, short, ok := strings.Cut(name, ":"); ok {
+ if prefix == "xmlns" {
return contentTypeURL
}
// Treat svg:href and xlink:href as href below.
- name = name[colon+1:]
+ name = short
}
if t, ok := attrTypeMap[name]; ok {
return t
diff --git a/src/html/template/js.go b/src/html/template/js.go
index ea9c18346b..32a4fbd30a 100644
--- a/src/html/template/js.go
+++ b/src/html/template/js.go
@@ -398,9 +398,7 @@ func isJSType(mimeType string) bool {
// https://tools.ietf.org/html/rfc4329#section-3
// https://www.ietf.org/rfc/rfc4627.txt
// discard parameters
- if i := strings.Index(mimeType, ";"); i >= 0 {
- mimeType = mimeType[:i]
- }
+ mimeType, _, _ = strings.Cut(mimeType, ";")
mimeType = strings.ToLower(mimeType)
mimeType = strings.TrimSpace(mimeType)
switch mimeType {
diff --git a/src/html/template/url.go b/src/html/template/url.go
index 6f8185a4e9..4b39fddf07 100644
--- a/src/html/template/url.go
+++ b/src/html/template/url.go
@@ -46,9 +46,7 @@ func urlFilter(args ...interface{}) string {
// isSafeURL is true if s is a relative URL or if URL has a protocol in
// (http, https, mailto).
func isSafeURL(s string) bool {
- if i := strings.IndexRune(s, ':'); i >= 0 && !strings.ContainsRune(s[:i], '/') {
-
- protocol := s[:i]
+ if protocol, _, ok := strings.Cut(s, ":"); ok && !strings.Contains(protocol, "/") {
if !strings.EqualFold(protocol, "http") && !strings.EqualFold(protocol, "https") && !strings.EqualFold(protocol, "mailto") {
return false
}