aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--html.go70
-rw-r--r--html_test.go24
2 files changed, 78 insertions, 16 deletions
diff --git a/html.go b/html.go
index 52460ee..463e1f3 100644
--- a/html.go
+++ b/html.go
@@ -97,33 +97,71 @@ func parseArgs(base string) map[string]string {
var key strings.Builder
var value strings.Builder
var quote bool
- for i < len(content) && content[i] != '>' && (i+1 == len(content) || string(content[i:i+2]) != "/>") {
+ next := true
+ finish := func() {
+ quote = false
+ sep = false
+ if key.Len() > 0 {
+ res[key.String()] = value.String()
+ }
+ key.Reset()
+ value.Reset()
+ }
+ for i < len(content) && next {
curr := content[i]
if !sep {
switch curr {
case '=':
sep = true
- case ' ':
- sep = false
- if key.Len() > 0 {
- res[key.String()] = ""
+ case '/':
+ if i+1 >= len(content) || string(content[i:i+2]) != "/>" {
+ key.WriteRune(curr)
+ i++
+ continue
}
- key.Reset()
+ fallthrough
+ case '>':
+ next = false
+ fallthrough
+ case ' ':
+ finish()
default:
key.WriteRune(curr)
}
} else {
- if value.Len() == 0 && curr == '"' {
- quote = true
- } else if (curr == '"' && quote) || curr == ' ' && !quote {
- quote = false
- sep = false
- res[key.String()] = value.String()
- key.Reset()
- value.Reset()
- } else {
- value.WriteRune(curr)
+ switch curr {
+ case '"':
+ if value.Len() == 0 {
+ quote = true
+ i++
+ continue
+ } else if quote {
+ finish()
+ i++
+ continue
+ }
+ case '/':
+ if i+1 >= len(content) || string(content[i:i+2]) != "/>" {
+ value.WriteRune(curr)
+ i++
+ continue
+ }
+ fallthrough
+ case '>':
+ if !quote {
+ next = false
+ }
+ fallthrough
+ case ' ':
+ if !quote {
+ finish()
+ i++
+ continue
+ }
+ fallthrough
+ default:
}
+ value.WriteRune(curr)
}
i++
}
diff --git a/html_test.go b/html_test.go
index b7fdf1a..8051b30 100644
--- a/html_test.go
+++ b/html_test.go
@@ -9,6 +9,9 @@ import (
)
func TestGetHumanFromHTML(t *testing.T) {
+ if testing.Short() {
+ t.SkipNow()
+ }
client := http.DefaultClient
// using human.json author's website
resp, err := client.Get(`https://robida.net/`)
@@ -87,6 +90,27 @@ func TestGetURLFromHTML(t *testing.T) {
if u.Host != "example.org" {
t.Errorf("invalid host: %s", u.Host)
}
+
+ realSample := `<link rel="stylesheet" href="/static/css/pico.min.css">
+ <link rel="stylesheet" href="/static/css/style.css">
+ <link rel="stylesheet" href="/static/css/leaflet.min.css">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <link rel="manifest" href="/static/manifest.json">
+ <link rel="human-json" href="/human.json">
+ <link rel="webmention" href="/webmention">`
+ u, err = GetURLFromHTML([]byte(realSample), base)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if u == nil {
+ t.Fatal("not found")
+ }
+ if u.Path != "/human.json" {
+ t.Errorf("invalid path: %s", u.Path)
+ }
+ if u.Host != "example.org" {
+ t.Errorf("invalid host: %s", u.Host)
+ }
}
func TestParseArgs(t *testing.T) {