Merge pull request #152 from elian0211/about_links

update about links
pull/162/head
Vytautas Šaltenis 2015-04-09 20:41:45 +03:00
commit b3137e7c8f
3 changed files with 73 additions and 2 deletions

13
html.go
View File

@ -485,7 +485,7 @@ func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
}
func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) {
if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) {
if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' {
out.WriteString(options.parameters.AbsolutePrefix)
if link[0] != '/' {
out.WriteByte('/')
@ -890,6 +890,17 @@ func isRelativeLink(link []byte) (yes bool) {
if len(link) == 1 && link[0] == '/' {
yes = true
}
// current directory : begin with "./"
if len(link) >= 2 && link[0] == '.' && link[1] == '/' {
yes = true
}
// parent directory : begin with "../"
if len(link) >= 3 && link[0] == '.' && link[1] == '.' && link[2] == '/' {
yes = true
}
return
}

View File

@ -757,9 +757,20 @@ func isEndOfLink(char byte) bool {
return isspace(char) || char == '<'
}
var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://"), []byte("/")}
var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
var validPaths = [][]byte{[]byte("/"), []byte("./"), []byte("../")}
func isSafeLink(link []byte) bool {
for _, path := range validPaths {
if len(link) >= len(path) && bytes.Equal(link[:len(path)], path) {
if len(link) == len(path) {
return true
} else if isalnum(link[len(path)]) {
return true
}
}
}
for _, prefix := range validUris {
// TODO: handle unicode here
// case-insensitive prefix test

View File

@ -72,6 +72,7 @@ func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int,
input := tests[i]
candidate = input
expected := tests[i+1]
actual := runMarkdownInline(candidate, extensions, htmlFlags, params)
if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
@ -440,6 +441,15 @@ func TestInlineLink(t *testing.T) {
"[[t]](/t)\n",
"<p><a href=\"/t\">[t]</a></p>\n",
"[link](</>)\n",
"<p><a href=\"/\">link</a></p>\n",
"[link](<./>)\n",
"<p><a href=\"./\">link</a></p>\n",
"[link](<../>)\n",
"<p><a href=\"../\">link</a></p>\n",
}
doLinkTestsInline(t, tests)
@ -452,6 +462,18 @@ func TestRelAttrLink(t *testing.T) {
"[foo](/bar/)\n",
"<p><a href=\"/bar/\">foo</a></p>\n",
"[foo](/)\n",
"<p><a href=\"/\">foo</a></p>\n",
"[foo](./)\n",
"<p><a href=\"./\">foo</a></p>\n",
"[foo](../)\n",
"<p><a href=\"../\">foo</a></p>\n",
"[foo](../bar)\n",
"<p><a href=\"../bar\">foo</a></p>\n",
}
doTestsInlineParam(t, nofollowTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS,
HtmlRendererParameters{})
@ -483,6 +505,21 @@ func TestHrefTargetBlank(t *testing.T) {
"[foo](/bar/)\n",
"<p><a href=\"/bar/\">foo</a></p>\n",
"[foo](/)\n",
"<p><a href=\"/\">foo</a></p>\n",
"[foo](./)\n",
"<p><a href=\"./\">foo</a></p>\n",
"[foo](./bar)\n",
"<p><a href=\"./bar\">foo</a></p>\n",
"[foo](../)\n",
"<p><a href=\"../\">foo</a></p>\n",
"[foo](../bar)\n",
"<p><a href=\"../bar\">foo</a></p>\n",
"[foo](http://example.com)\n",
"<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
}
@ -494,6 +531,15 @@ func TestSafeInlineLink(t *testing.T) {
"[foo](/bar/)\n",
"<p><a href=\"/bar/\">foo</a></p>\n",
"[foo](/)\n",
"<p><a href=\"/\">foo</a></p>\n",
"[foo](./)\n",
"<p><a href=\"./\">foo</a></p>\n",
"[foo](../)\n",
"<p><a href=\"../\">foo</a></p>\n",
"[foo](http://bar/)\n",
"<p><a href=\"http://bar/\">foo</a></p>\n",
@ -541,6 +587,9 @@ func TestReferenceLink(t *testing.T) {
"[ref]\n [ref]: /url/ \"title\"\n",
"<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
"[ref]\n [ref]: ../url/ \"title\"\n",
"<p><a href=\"../url/\" title=\"title\">ref</a></p>\n",
}
doLinkTestsInline(t, tests)
}