From 60b0b4024f8b427fd66ea10a7a895bc5b5f20d63 Mon Sep 17 00:00:00 2001 From: Beyang Liu Date: Sat, 14 Mar 2015 16:46:32 -0700 Subject: [PATCH] add rel="noreferrer" option --- html.go | 21 +++++++++++++++++++-- inline_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/html.go b/html.go index 666d0e3..31f786b 100644 --- a/html.go +++ b/html.go @@ -31,6 +31,7 @@ const ( HTML_SKIP_LINKS // skip all links HTML_SAFELINK // only link to trusted protocols HTML_NOFOLLOW_LINKS // only link with rel="nofollow" + HTML_NOREFERRER_LINKS // only link with rel="noreferrer" HTML_HREF_TARGET_BLANK // add a blank target HTML_TOC // generate a table of contents HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents) @@ -429,9 +430,17 @@ func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) { entityEscapeWithSkip(out, link, skipRanges) + var relAttrs []string if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { - out.WriteString("\" rel=\"nofollow") + relAttrs = append(relAttrs, "nofollow") } + if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noreferrer") + } + if len(relAttrs) > 0 { + out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) + } + // blank target only add to external link if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { out.WriteString("\" target=\"_blank") @@ -535,9 +544,17 @@ func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content out.WriteString("\" title=\"") attrEscape(out, title) } + var relAttrs []string if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { - out.WriteString("\" rel=\"nofollow") + relAttrs = append(relAttrs, "nofollow") } + if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noreferrer") + } + if len(relAttrs) > 0 { + out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) + } + // blank target only add to external link if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { out.WriteString("\" target=\"_blank") diff --git a/inline_test.go b/inline_test.go index fb3bdec..c07ecdc 100644 --- a/inline_test.go +++ b/inline_test.go @@ -445,15 +445,35 @@ func TestInlineLink(t *testing.T) { } -func TestNofollowLink(t *testing.T) { - var tests = []string{ +func TestRelAttrLink(t *testing.T) { + var nofollowTests = []string{ "[foo](http://bar.com/foo/)\n", "

foo

\n", "[foo](/bar/)\n", "

foo

\n", } - doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, + doTestsInlineParam(t, nofollowTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, + HtmlRendererParameters{}) + + var noreferrerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "

foo

\n", + + "[foo](/bar/)\n", + "

foo

\n", + } + doTestsInlineParam(t, noreferrerTests, 0, HTML_SAFELINK|HTML_NOREFERRER_LINKS, + HtmlRendererParameters{}) + + var nofollownoreferrerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "

foo

\n", + + "[foo](/bar/)\n", + "

foo

\n", + } + doTestsInlineParam(t, nofollownoreferrerTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS, HtmlRendererParameters{}) }