From 41eb1fe8a788a751dbe61efc3a5ce4a2f244770c Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Sun, 22 Sep 2019 15:03:03 +0100 Subject: [PATCH] Backport noopener links (from commit 670777b536d38a1aef1d517f6330a77d52ceb02e) --- html.go | 7 +++++++ inline_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/html.go b/html.go index e0a6c69..fa044ca 100644 --- a/html.go +++ b/html.go @@ -32,6 +32,7 @@ const ( HTML_SAFELINK // only link to trusted protocols HTML_NOFOLLOW_LINKS // only link with rel="nofollow" HTML_NOREFERRER_LINKS // only link with rel="noreferrer" + HTML_NOOPENER_LINKS // only link with rel="noopener" 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) @@ -445,6 +446,9 @@ func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) { if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { relAttrs = append(relAttrs, "noreferrer") } + if options.flags&HTML_NOOPENER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noopener") + } if len(relAttrs) > 0 { out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) } @@ -559,6 +563,9 @@ func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { relAttrs = append(relAttrs, "noreferrer") } + if options.flags&HTML_NOOPENER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noopener") + } if len(relAttrs) > 0 { out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) } diff --git a/inline_test.go b/inline_test.go index 942c5d3..cb1dfd5 100644 --- a/inline_test.go +++ b/inline_test.go @@ -624,6 +624,25 @@ func TestRelAttrLink(t *testing.T) { } doTestsInlineParam(t, nofollownoreferrerTests, Options{}, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS, HtmlRendererParameters{}) + + var noopenerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "

foo

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

foo

\n", + } + doTestsInlineParam(t, noopenerTests, Options{}, HTML_SAFELINK|HTML_NOOPENER_LINKS, HtmlRendererParameters{}) + + var nofollownoreferrernoopenerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "

foo

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

foo

\n", + } + doTestsInlineParam(t, nofollownoreferrernoopenerTests, Options{}, + HTML_SAFELINK|HTML_NOOPENER_LINKS|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS, HtmlRendererParameters{}) } func TestHrefTargetBlank(t *testing.T) {