Fix RelURL and AbsURL when path starts with language

This commit is contained in:
ivan-meridianbanc-com 2020-12-02 13:47:01 +02:00 committed by GitHub
parent 907d9e9268
commit aebfe156fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View file

@ -181,10 +181,14 @@ func (p *PathSpec) AbsURL(in string, addLanguage bool) string {
if prefix != "" { if prefix != "" {
hasPrefix := false hasPrefix := false
// avoid adding language prefix if already present // avoid adding language prefix if already present
in2 := in
if strings.HasPrefix(in, "/") { if strings.HasPrefix(in, "/") {
hasPrefix = strings.HasPrefix(in[1:], prefix) in2 = in[1:]
}
if in2 == prefix {
hasPrefix = true
} else { } else {
hasPrefix = strings.HasPrefix(in, prefix) hasPrefix = strings.HasPrefix(in2, prefix+"/")
} }
if !hasPrefix { if !hasPrefix {
@ -230,10 +234,14 @@ func (p *PathSpec) RelURL(in string, addLanguage bool) string {
if prefix != "" { if prefix != "" {
hasPrefix := false hasPrefix := false
// avoid adding language prefix if already present // avoid adding language prefix if already present
in2 := in
if strings.HasPrefix(in, "/") { if strings.HasPrefix(in, "/") {
hasPrefix = strings.HasPrefix(in[1:], prefix) in2 = in[1:]
}
if in2 == prefix {
hasPrefix = true
} else { } else {
hasPrefix = strings.HasPrefix(in, prefix) hasPrefix = strings.HasPrefix(in2, prefix+"/")
} }
if !hasPrefix { if !hasPrefix {

View file

@ -84,6 +84,21 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"}, {"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
} }
if multilingual && addLanguage && defaultInSubDir {
newTests := []struct {
input string
baseURL string
expected string
}{
{lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
{"/" + lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
}
for _, test := range newTests {
tests = append(tests, test)
}
}
for _, test := range tests { for _, test := range tests {
v.Set("baseURL", test.baseURL) v.Set("baseURL", test.baseURL)
v.Set("contentDir", "content") v.Set("contentDir", "content")
@ -164,6 +179,22 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
{"//schemaless", "http://base/", false, "//schemaless"}, {"//schemaless", "http://base/", false, "//schemaless"},
} }
if multilingual && addLanguage && defaultInSubDir {
newTests := []struct {
input string
baseURL string
canonify bool
expected string
}{
{lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
{"/" + lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
}
for _, test := range newTests {
tests = append(tests, test)
}
}
for i, test := range tests { for i, test := range tests {
v.Set("baseURL", test.baseURL) v.Set("baseURL", test.baseURL)
v.Set("canonifyURLs", test.canonify) v.Set("canonifyURLs", test.canonify)