-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Work around Chrome bug causing sidebar to become invisible #523
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Prevent windows or tabs opened via links under `root` from accessing their | ||
* opening `Window`. | ||
* | ||
* This makes links with `target="blank"` attributes act as if they also had | ||
* the `rel="noopener"` [1] attribute set. | ||
* | ||
* In addition to preventing tab-jacking [2], this also enables multi-process | ||
* browsers to more easily use a new process for instances of Hypothesis in the | ||
* newly-opened tab and works around a bug in Chrome [3] | ||
* | ||
* [1] https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types#noopener | ||
* [2] https://mathiasbynens.github.io/rel-noopener/ | ||
* [3] https://bugs.chromium.org/p/chromium/issues/detail?id=753314 | ||
* | ||
* @param {Element} root - Root element | ||
*/ | ||
function disableOpenerForExternalLinks(root) { | ||
root.addEventListener('click', event => { | ||
if (event.target.tagName === 'A') { | ||
var linkEl = event.target; | ||
if (linkEl.target === '_blank') { | ||
linkEl.rel = 'noopener'; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
module.exports = disableOpenerForExternalLinks; |
41 changes: 41 additions & 0 deletions
41
src/sidebar/util/test/disable-opener-for-external-links-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var disableOpenerForExternalLinks = require('../disable-opener-for-external-links'); | ||
|
||
describe('sidebar.util.disable-opener-for-external-links', () => { | ||
var containerEl; | ||
var linkEl; | ||
|
||
beforeEach(() => { | ||
containerEl = document.createElement('div'); | ||
linkEl = document.createElement('a'); | ||
containerEl.appendChild(linkEl); | ||
document.body.appendChild(containerEl); | ||
}); | ||
|
||
afterEach(() => { | ||
containerEl.remove(); | ||
}); | ||
|
||
function clickLink() { | ||
linkEl.dispatchEvent(new Event('click', { | ||
bubbles: true, | ||
cancelable: true, | ||
})); | ||
} | ||
|
||
it('disables opener for external links', () => { | ||
linkEl.target = '_blank'; | ||
|
||
disableOpenerForExternalLinks(containerEl); | ||
clickLink(); | ||
|
||
assert.equal(linkEl.rel, 'noopener'); | ||
}); | ||
|
||
it('does not disable opener for internal links', () => { | ||
disableOpenerForExternalLinks(containerEl); | ||
clickLink(); | ||
assert.equal(linkEl.rel, ''); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice docstring, which is important for a kind of obscure fix like this