Fix fragments being AbsUrlified in final html
Found that fragments were getting the BaseURL applied creating a proper anchor url and redirecting off the page.
This commit is contained in:
parent
311e102223
commit
784077da4d
2 changed files with 25 additions and 5 deletions
|
@ -248,10 +248,14 @@ func TestSkipRender(t *testing.T) {
|
||||||
func TestAbsUrlify(t *testing.T) {
|
func TestAbsUrlify(t *testing.T) {
|
||||||
files := make(map[string][]byte)
|
files := make(map[string][]byte)
|
||||||
target := &InMemoryTarget{files: files}
|
target := &InMemoryTarget{files: files}
|
||||||
|
sources := []byteSource{
|
||||||
|
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
|
||||||
|
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
|
||||||
|
}
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: target,
|
Target: target,
|
||||||
Config: Config{BaseUrl: "http://auth/bub/"},
|
Config: Config{BaseUrl: "http://auth/bub/"},
|
||||||
Source: &inMemorySource{urlFakeSource},
|
Source: &inMemorySource{sources},
|
||||||
}
|
}
|
||||||
s.initializeSiteInfo()
|
s.initializeSiteInfo()
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
|
@ -269,13 +273,22 @@ func TestAbsUrlify(t *testing.T) {
|
||||||
t.Fatalf("Unable to render pages. %s", err)
|
t.Fatalf("Unable to render pages. %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
content, ok := target.files["content/blue/slug-doc-1.html"]
|
tests := []struct {
|
||||||
if !ok {
|
file, expected string
|
||||||
t.Fatalf("Unable to locate rendered content")
|
}{
|
||||||
|
{"content/blue/doc2.html", "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"},
|
||||||
|
{"sect/doc1.html", "<!DOCTYPE html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"
|
for _, test := range tests {
|
||||||
|
content, ok := target.files[test.file]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := test.expected
|
||||||
if string(content) != expected {
|
if string(content) != expected {
|
||||||
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ func (t *Transformer) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (
|
||||||
if inURL, err = url.Parse(in); err != nil {
|
if inURL, err = url.Parse(in); err != nil {
|
||||||
return in + "?"
|
return in + "?"
|
||||||
}
|
}
|
||||||
|
if fragmentOnly(inURL) {
|
||||||
|
return in
|
||||||
|
}
|
||||||
return baseURL.ResolveReference(inURL).String()
|
return baseURL.ResolveReference(inURL).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,3 +53,7 @@ func (t *Transformer) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fragmentOnly(u *url.URL) bool {
|
||||||
|
return u.Fragment != "" && u.Scheme == "" && u.Opaque == "" && u.User == nil && u.Host == "" && u.Path == "" && u.Path == "" && u.RawQuery == ""
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue