Fix bug in autolink with trailing semicolon

In case the link ends with escaped html entity, the semicolon is a part
of the link and should not be interpreted as punctuation.
pull/55/head
Vytautas Šaltenis 2014-01-26 23:40:26 +02:00
parent b0bdfbec4c
commit e5937643a9
2 changed files with 17 additions and 1 deletions

View File

@ -617,6 +617,14 @@ func entity(p *parser, out *bytes.Buffer, data []byte, offset int) int {
return end
}
func linkEndsWithEntity(data []byte, linkEnd int) bool {
entityRanges := htmlEntity.FindAllIndex(data[:linkEnd], -1)
if entityRanges != nil && entityRanges[len(entityRanges)-1][1] == linkEnd {
return true
}
return false
}
func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// quick check to rule out most false hits on ':'
if p.insideLink || len(data) < offset+3 || data[offset+1] != '/' || data[offset+2] != '/' {
@ -659,7 +667,12 @@ func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
// Skip punctuation at the end of the link
if (data[linkEnd-1] == '.' || data[linkEnd-1] == ',' || data[linkEnd-1] == ';') && data[linkEnd-2] != '\\' {
if (data[linkEnd-1] == '.' || data[linkEnd-1] == ',') && data[linkEnd-2] != '\\' {
linkEnd--
}
// But don't skip semicolon if it's a part of escaped entity:
if data[linkEnd-1] == ';' && data[linkEnd-2] != '\\' && !linkEndsWithEntity(data, linkEnd) {
linkEnd--
}

View File

@ -698,6 +698,9 @@ func TestAutoLink(t *testing.T) {
"http://foo.com/viewtopic.php?param=&quot;18&quot;zz",
"<p><a href=\"http://foo.com/viewtopic.php?param=&quot;18&quot;zz\">http://foo.com/viewtopic.php?param=&quot;18&quot;zz</a></p>\n",
"http://foo.com/viewtopic.php?param=&quot;18&quot;",
"<p><a href=\"http://foo.com/viewtopic.php?param=&quot;18&quot;\">http://foo.com/viewtopic.php?param=&quot;18&quot;</a></p>\n",
}
doTestsInline(t, tests)
}