mozilla/bleach

Possible to only allow target="_blank" but no other values?

ThiefMaster opened this issue · 3 comments

Is there a way to allow <a target="_blank" ....> but no other targets? My usecase is that people sometimes don't know what to use, and if you put target="blank" or similar, then it'll set the window name and two links with the same wrong target would open in the same new tab/window instead of separate ones.

Can you provide a test case so I know specifically what you're asking about?

<a href="https://google.com" target="_blank">test</a>
<a href="https://google.com" target="foo">test 2</a>
<a href="https://google.com">test 3</a>

->

<a href="https://google.com" target="_blank">test</a>
<a href="https://google.com">test 2</a>
<a href="https://google.com">test 3</a>
willkg commented

There's a target_blank callback that you can use which will enforce a target="_blank". Pretty sure that's not what you want, but you can write your own callback and base it on the target_blank code.

def target_blank(attrs, new=False):
href_key = (None, "href")
if href_key not in attrs:
return attrs
if attrs[href_key].startswith("mailto:"):
return attrs
attrs[(None, "target")] = "_blank"
return attrs

Hope that helps!