From 670777b536d38a1aef1d517f6330a77d52ceb02e Mon Sep 17 00:00:00 2001 From: Liming Jin Date: Sat, 26 May 2018 15:57:26 +0800 Subject: [PATCH] add NoopenerLinks (#437) --- html.go | 4 ++++ inline_test.go | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/html.go b/html.go index 6a3b7ce..284c871 100644 --- a/html.go +++ b/html.go @@ -35,6 +35,7 @@ const ( Safelink // Only link to trusted protocols NofollowLinks // Only link with rel="nofollow" NoreferrerLinks // Only link with rel="noreferrer" + NoopenerLinks // Only link with rel="noopener" HrefTargetBlank // Add a blank target CompletePage // Generate a complete HTML page UseXHTML // Generate XHTML output instead of HTML @@ -286,6 +287,9 @@ func appendLinkAttrs(attrs []string, flags HTMLFlags, link []byte) []string { if flags&NoreferrerLinks != 0 { val = append(val, "noreferrer") } + if flags&NoopenerLinks != 0 { + val = append(val, "noopener") + } if flags&HrefTargetBlank != 0 { attrs = append(attrs, "target=\"_blank\"") } diff --git a/inline_test.go b/inline_test.go index 9be9c8f..69b70ae 100644 --- a/inline_test.go +++ b/inline_test.go @@ -497,15 +497,26 @@ func TestRelAttrLink(t *testing.T) { HTMLFlags: Safelink | NoreferrerLinks, }) - var nofollownoreferrerTests = []string{ + var noopenerTests = []string{ "[foo](http://bar.com/foo/)\n", - "

foo

\n", + "

foo

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

foo

\n", } - doTestsInlineParam(t, nofollownoreferrerTests, TestParams{ - HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks, + doTestsInlineParam(t, noopenerTests, TestParams{ + HTMLFlags: Safelink | NoopenerLinks, + }) + + var nofollownoreferrernoopenerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "

foo

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

foo

\n", + } + doTestsInlineParam(t, nofollownoreferrernoopenerTests, TestParams{ + HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks | NoopenerLinks, }) }