Skip to content

Instantly share code, notes, and snippets.

@andycochrane
Created March 2, 2017 16:46
Show Gist options
  • Select an option

  • Save andycochrane/aba92dc70bb2493059a74af85298624c to your computer and use it in GitHub Desktop.

Select an option

Save andycochrane/aba92dc70bb2493059a74af85298624c to your computer and use it in GitHub Desktop.
Atom auto rel="noopener"
##
# Are you looking for a front-end developer? I’m looking for a new role!
#
# andycdev@gmail.com
# andycochrane.co.uk
##
'*.html':
'Target Blank (with rel="noopener")':
'prefix': 'blank'
'body': 'target="_blank" rel="noopener"'
@andycochrane

Copy link
Copy Markdown
Author

target="_blank" has unexpected performance and security concerns[1], so should always be complemented with rel="noopener". This Atom snippet will automatically generate both for you. Simply type blank and hit <Tab>.

screen recording 2017-03-02 at 04 37 pm

  1. https://jakearchibald.com/2016/performance-benefits-of-rel-noopener/

@mendaomn

mendaomn commented Mar 3, 2017

Copy link
Copy Markdown

Great! Would love if it could also be enforced by a linter, have any knowledge in that area?

@andycochrane

Copy link
Copy Markdown
Author

Hey there @mendaomn,

You could probably create a custom linter based on the base linter which enforces this particular practice. However, I don’t think it is necessary or even appropriate for a linter to enforce this specific attribute as linters are more concerned with analysing code for general style consistency and potential errors.

I’ve created a variation on this snippet for you which will get you most of the way to autocompleting rel="noopener". To use this snippet you would use autocomplete to input your target="_blank" attribute and then use your snippets:expand key binding to append rel="noopener". Your key binding for snippets:expand will likely be the same as for autocomplete-plus:confirm so would just require an extra press. For example, I would begin typing target then hit tab 3 times and I will have a fully completed snippet like so: target="_blank" rel="noopener"

Here is the variation on the snippet (warning: it isn’t pretty)

'*.html':
    'Target Blank (with rel="noopener")':
        'prefix': '_blank'
        'body': '_blank" rel="noopener'

So, unfortunately, there is no enforcement happening here and you still have to remember the extra key press to add the rel="noopener" attribute, but we don’t have to remember the prefix name for the entire snippet.

Hope that helps.

Cheers,

Andy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment